]> www.fi.muni.cz Git - aoc.git/blobdiff - 2017/40.pl
The rest of Year 2017
[aoc.git] / 2017 / 40.pl
diff --git a/2017/40.pl b/2017/40.pl
new file mode 100755 (executable)
index 0000000..d62cd2a
--- /dev/null
@@ -0,0 +1,41 @@
+#!/usr/bin/perl
+
+use v5.30;
+use strict;
+
+my (@acc, @vel, @pos);
+while (<>) {
+       my (@l) = /(-?\d+)/g;
+       push @pos, [ splice(@l, 0, 3) ];
+       push @vel, [ splice(@l, 0, 3) ];
+       push @acc, [ splice(@l, 0, 3) ];
+}
+
+my %destroyed;
+for (1 .. 1_000) {
+       my (@nvel, @npos);
+       my %pos_now;
+       for my $i (0 .. $#acc) {
+               my @v;
+               push @v, $vel[$i][$_] + $acc[$i][$_] for 0 .. 2;
+               push @nvel, \@v;
+               my @p;
+               push @p, $pos[$i][$_] + $v[$_] for 0 .. 2;
+               push @npos, \@p;
+               if (!$destroyed{$i}) {
+                       my $key = join(',', @p);
+                       if ($pos_now{$key}) {
+                               $destroyed{$pos_now{$key}} = 1;
+                               $destroyed{$i} = 1;
+                               say "$_ destroyed $i because of $pos_now{$key} at $key";
+                       } else {
+                               $pos_now{$key} = $i;
+                       }
+               }
+       }
+       @vel = @nvel;
+       @pos = @npos;
+}
+
+say $#acc - keys %destroyed;
+