]> www.fi.muni.cz Git - aoc.git/commitdiff
Day 18: interesting
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 18 Dec 2022 06:11:35 +0000 (07:11 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sun, 18 Dec 2022 06:11:35 +0000 (07:11 +0100)
2022/35.pl [new file with mode: 0755]
2022/36.pl [new file with mode: 0755]

diff --git a/2022/35.pl b/2022/35.pl
new file mode 100755 (executable)
index 0000000..4a32d29
--- /dev/null
@@ -0,0 +1,19 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+
+my @cubes = map { [ /(\d+)/g ] } <>;
+my %cubes = map { join(',', @$_) => 1 } @cubes;
+
+my $common;
+for my $cube (@cubes) {
+       for my $axis (0 .. 2) {
+               for my $add (-1, 1) {
+                       my @neigh = @$cube;
+                       $neigh[$axis] += $add;
+                       $common++ if $cubes{join(',', @neigh)};
+               }
+       }
+}
+say 6*@cubes - $common;
diff --git a/2022/36.pl b/2022/36.pl
new file mode 100755 (executable)
index 0000000..8daa7df
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/perl -w
+
+use v5.36;
+use strict;
+use experimental 'multidimensional';
+
+my @cubes = sort { $a->[0] <=> $b->[0] } map { [ /(\d+)/g ] } <>;
+my %cubes = map { join(',', @$_) => 1 } @cubes;
+
+my @q = ([ @{$cubes[0]}, 0, -1 ]);
+my %seen;
+while (@q) {
+       my $face = shift @q;
+       next if $seen{join(',', @$face)}++;
+
+       for my $axis (0 .. 2) {
+               next if $axis == $face->[3];
+               for my $dir (-1, 1) {
+                       my @neigh = @{$face}[0 .. 2];
+                       $neigh[$axis] += $dir;
+                       $neigh[$face->[3]] += $face->[4];
+                       # say "upper? @neigh";
+                       if ($cubes{join(',',@neigh)}) {
+                               push @q, [ @neigh, $axis, -$dir ];
+                               # say "upper cube at @{$q[-1]}";
+                               next;
+                       }
+
+                       $neigh[$face->[3]] -= $face->[4];
+                       # say "side? @neigh";
+                       if ($cubes{join(',',@neigh)}) {
+                               push @q, [ @neigh, $face->[3], $face->[4] ];
+                               # say "side cube at @{$q[-1]}";
+                               next;
+                       }
+
+                       $neigh[$axis] -= $dir;
+                       push @q, [ @neigh, $axis, $dir ];
+                       # say "this cube at @{$q[-1]}";
+               }
+       }
+}
+
+say scalar keys %seen;