From 156c1de3608eedbe19490679d01b39f89aea53da Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 8 Dec 2023 08:29:42 +0100 Subject: [PATCH] Day 8: ugly and generally incorrect first working solution --- 2023/15.pl | 28 ++++++++++++++++++++++++++++ 2023/16.pl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 2023/15.pl create mode 100755 2023/16.pl diff --git a/2023/15.pl b/2023/15.pl new file mode 100755 index 0000000..e003111 --- /dev/null +++ b/2023/15.pl @@ -0,0 +1,28 @@ +#!/usr/bin/perl -w + +use v5.38; + +$/ = "\n\n"; +my %dirs; +my @steps = split //, scalar <>; +pop @steps; +pop @steps; +my $l = <>; +for (split /\n/, $l) { + my @x = /[A-Z]{3}/g; + $dirs{$x[0].'L'} = $x[1]; + $dirs{$x[0].'R'} = $x[2]; +} + +my %seen; +my $now = 'AAA' . $steps[0]; +my $i = 0; +my $n; +while ($now !~ /^ZZZ/) { + say $now; + $i ++; + $i = 0 if $i >= @steps; + $now = $dirs{$now}.$steps[$i]; + $n++; +} +say $n; diff --git a/2023/16.pl b/2023/16.pl new file mode 100755 index 0000000..5e7f823 --- /dev/null +++ b/2023/16.pl @@ -0,0 +1,45 @@ +#!/usr/bin/perl -w + +use v5.38; + +$/ = "\n\n"; +my %dirs; +my @steps = split //, scalar <>; +pop @steps; +pop @steps; +my $l = <>; +for (split /\n/, $l) { + my @x = /[A-Z0-9]{3}/g; + $dirs{$x[0].'L'} = $x[1]; + $dirs{$x[0].'R'} = $x[2]; +} + +my %seen; +my %now = map { substr($_, 0, 3) . $steps[0] => 1 } grep { /^..A/ } keys %dirs; +my @now = keys %now; +say join(' ', @now); + +sub walk { + my $node = shift; + my $i = 0; + my $n; + while ($node !~ /^..Z/) { + $i++; + $i = 0 if $i >= @steps; + $node = $dirs{$node} . $steps[$i]; + $n++; + } + return $n; +} + +my %f; +use List::Util qw(product); +for (@now) { + my $st = walk($_); + my $l = `factor $st`; + $l =~ s/.*://; + for my $n ($l =~ /\d+/g) { + $f{$n}++; + } +} +say product keys %f; -- 2.43.5