From: Jan "Yenya" Kasprzak Date: Sun, 3 Dec 2023 06:21:44 +0000 (+0100) Subject: Day 3: quite ugly code X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=cca063e244ae7183e0df93985d4abcede8974522;p=aoc.git Day 3: quite ugly code --- diff --git a/2023/05.pl b/2023/05.pl new file mode 100755 index 0000000..3882c0b --- /dev/null +++ b/2023/05.pl @@ -0,0 +1,51 @@ +#!/usr/bin/perl -w + +use v5.38; +use experimental 'multidimensional', 'for_list', 'builtin'; +use builtin 'indexed'; +use List::Util; +use Y::AoC::Task; +$; = ';'; +# t; + +my @map = map { chomp; [ split // ] } <>; +my $xmax = @{ $map[0] }; +my $ymax = @map; + +my $sum; + +my %seen; + +for my $y (0 .. $ymax-1) { +for my $x (0 .. $xmax-1) { + my $n = $map[$y][$x]; + next if $n !~ /\d/ || $seen{$x,$y}++; + my $dx = 1; + my $is_neigh = 0; + if ($x > 0) { + if ($y > 0 && $map[$y-1][$x-1] !~ /\./) { $is_neigh = 1; } + if ($map[$y][$x-1] !~ /\./) { $is_neigh = 1; } + if ($y < $ymax-1 && $map[$y+1][$x-1] !~ /\./) { $is_neigh = 1; } + } + say "First $is_neigh"; + if ($y > 0 && $map[$y-1][$x] !~ /\./) { $is_neigh = 1; } + if ($y < $ymax-1 && $map[$y+1][$x] !~ /\./) { $is_neigh = 1; } + while ($x+$dx < $xmax && $map[$y][$x+$dx] =~ /\d/) { + $n*=10; $n+= $map[$y][$x+$dx]; + say "First $is_neigh at ", $x+$dx; + if ($y > 0 && $map[$y-1][$x+$dx] !~ /\./) { say $y-1, " ", $x+$dx; $is_neigh = 1; } + if ($y < $ymax-1 && $map[$y+1][$x+$dx] !~ /\./) { $is_neigh = 1; } + $seen{$x+$dx,$y} = 1; + $dx++; + } + say "Before last $is_neigh at ", $x+$dx; + if ($x + $dx < $xmax) { + if ($y > 0 && $map[$y-1][$x+$dx] !~ /\./) { say $y-1, " ", $x+$dx; $is_neigh = 1; } + if ($y < $ymax-1 && $map[$y+1][$x+$dx] !~ /\./) { $is_neigh = 1; } + if ($x+$dx < $xmax-1 && $map[$y][$x+$dx] !~ /\./) { $is_neigh = 1; } + } + $sum += $n if $is_neigh; + if ($is_neigh) { say "$n at $x,$y" } +} } + +say $sum; diff --git a/2023/06.pl b/2023/06.pl new file mode 100755 index 0000000..12992b0 --- /dev/null +++ b/2023/06.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl -w + +use v5.38; +use experimental 'multidimensional', 'for_list', 'builtin'; +use builtin 'indexed'; +use List::Util; +use Y::AoC::Task; +$; = ';'; +# t; + +my @map = map { chomp; [ split // ] } <>; +my $xmax = @{ $map[0] }; +my $ymax = @map; + +my $sum; +my %seen; +my %gear; + +sub test_sym { + my ($prev, $x, $y, @d) = @_; + for my ($dx, $dy) (@d) { + my ($nx, $ny) = ($x+$dx, $y+$dy); + next if $nx < 0 || $nx >= $xmax || $ny < 0 || $ny >= $ymax; + if ($map[$ny][$nx] eq '*') { + $$prev = [$nx, $ny]; + } + } +} + +for my $y (0 .. $ymax-1) { +for my $x (0 .. $xmax-1) { + my $n = $map[$y][$x]; + next if $n !~ /\d/ || $seen{$x,$y}++; + my $dx = 1; + while ($x+$dx < $xmax && $map[$y][$x+$dx] =~ /\d/) { + $n*=10; $n+= $map[$y][$x+$dx]; + $seen{$x+$dx,$y} = 1; + $dx++; + say "$n at $x..", $x+$dx, ", $y"; + } + my $sym; + for my $x1 (-1 .. $dx) { + test_sym(\$sym, $x, $y, $x1, -1, $x1, 0, $x1, +1); + } + if (defined $sym) { + my ($sx, $sy) = @$sym; + push @{ $gear{$sx,$sy} }, $n; + say "Star at $sx, $sy"; + } +} } + + +for my $k (keys %gear) { + my @coords = @{ $gear{$k} }; + next if @coords != 2; + $sum += $coords[0] * $coords[1]; +} + +say $sum;