From db816fdca103cd0131c7d4858183da6e4f63e9b6 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sun, 18 Dec 2022 07:11:35 +0100 Subject: [PATCH] Day 18: interesting --- 2022/35.pl | 19 +++++++++++++++++++ 2022/36.pl | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100755 2022/35.pl create mode 100755 2022/36.pl diff --git a/2022/35.pl b/2022/35.pl new file mode 100755 index 0000000..4a32d29 --- /dev/null +++ b/2022/35.pl @@ -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 index 0000000..8daa7df --- /dev/null +++ b/2022/36.pl @@ -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; -- 2.43.0