From 3825fa6f10d6debc26451812c4257673c952bcb2 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 13 Dec 2021 12:51:19 +0100 Subject: [PATCH] 26.pl - simpler solution with text-based keys "$x,$y" --- 26.pl | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/26.pl b/26.pl index c789704..adfba38 100755 --- a/26.pl +++ b/26.pl @@ -3,37 +3,34 @@ use v5.16; my %dots; - +$; = ','; while (<>) { chomp; - my ($x, $y) = split /,/; - last if !defined $y; - $dots{$x}{$y}++; + last if !length; + $dots{$_}++; } -my ($maxx, $maxy); +my %max; while (<>) { chomp; my ($axis, $val) = /fold along (.)=(\d+)/; + $max{$axis} = $val; - $maxx = $val if $axis eq 'x' && (!$maxx || $maxx > $val); - $maxy = $val if $axis eq 'y' && (!$maxy || $maxy > $val); - - for my $x (keys %dots) { - for my $y (keys %{$dots{$x}}) { + for (keys %dots) { + my ($x, $y) = split /,/; if ($axis eq 'x' && $x > $val) { - $dots{2*$val - $x}{$y}++; - delete $dots{$x}{$y}; + $dots{2*$val - $x,$y}++; + delete $dots{$x,$y}; } elsif ($axis eq 'y' && $y > $val) { - $dots{$x}{2*$val - $y}++; - delete $dots{$x}{$y}; + $dots{$x,2*$val - $y}++; + delete $dots{$x,$y}; } - } } + } } -for my $y (0 .. $maxy) { - for my $x (0 .. 40) { - print $dots{$x}{$y} ? '#' : ' '; +for my $y (0 .. $max{y}) { + for my $x (0 .. $max{x}) { + print $dots{$x,$y} ? '#' : ' '; } print "\n"; } -- 2.43.5