]> www.fi.muni.cz Git - aoc.git/blob - 2017/40.pl
The rest of Year 2017
[aoc.git] / 2017 / 40.pl
1 #!/usr/bin/perl
2
3 use v5.30;
4 use strict;
5
6 my (@acc, @vel, @pos);
7 while (<>) {
8         my (@l) = /(-?\d+)/g;
9         push @pos, [ splice(@l, 0, 3) ];
10         push @vel, [ splice(@l, 0, 3) ];
11         push @acc, [ splice(@l, 0, 3) ];
12 }
13
14 my %destroyed;
15 for (1 .. 1_000) {
16         my (@nvel, @npos);
17         my %pos_now;
18         for my $i (0 .. $#acc) {
19                 my @v;
20                 push @v, $vel[$i][$_] + $acc[$i][$_] for 0 .. 2;
21                 push @nvel, \@v;
22                 my @p;
23                 push @p, $pos[$i][$_] + $v[$_] for 0 .. 2;
24                 push @npos, \@p;
25                 if (!$destroyed{$i}) {
26                         my $key = join(',', @p);
27                         if ($pos_now{$key}) {
28                                 $destroyed{$pos_now{$key}} = 1;
29                                 $destroyed{$i} = 1;
30                                 say "$_ destroyed $i because of $pos_now{$key} at $key";
31                         } else {
32                                 $pos_now{$key} = $i;
33                         }
34                 }
35         }
36         @vel = @nvel;
37         @pos = @npos;
38 }
39
40 say $#acc - keys %destroyed;
41