From 1853ec1de5d2f229724bc24c1c9b3d3c1572affc Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Mon, 13 Dec 2021 06:28:05 +0100 Subject: [PATCH] Day 13: more off-by-one errors. --- 25.pl | 38 ++++++++++++++++++++++++++++++++++++++ 26.pl | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100755 25.pl create mode 100755 26.pl diff --git a/25.pl b/25.pl new file mode 100755 index 0000000..ceeb16f --- /dev/null +++ b/25.pl @@ -0,0 +1,38 @@ +#!/usr/bin/perl -w + +use v5.16; + +my %dots; + +while (<>) { + chomp; + my ($x, $y) = split /,/; + last if !defined $y; + $dots{$x}{$y}++; +} + +my ($maxx, $maxy); +while (<>) { + chomp; + my ($axis, $val) = /fold along (.)=(\d+)/; + + for my $x (keys %dots) { + for my $y (keys %{$dots{$x}}) { + if ($axis eq 'x' && $x > $val) { + $dots{2*$val - $x}{$y}++; + delete $dots{$x}{$y}; + } elsif ($axis eq 'y' && $y > $val) { + $dots{$x}{2*$val - $y}++; + delete $dots{$x}{$y}; + } + } } + last; +} + +my $sum; +for my $x (keys %dots) { + $sum += keys %{$dots{$x}}; +} + +say $sum; + diff --git a/26.pl b/26.pl new file mode 100755 index 0000000..c789704 --- /dev/null +++ b/26.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl -w + +use v5.16; + +my %dots; + +while (<>) { + chomp; + my ($x, $y) = split /,/; + last if !defined $y; + $dots{$x}{$y}++; +} + +my ($maxx, $maxy); +while (<>) { + chomp; + my ($axis, $val) = /fold along (.)=(\d+)/; + + $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}}) { + if ($axis eq 'x' && $x > $val) { + $dots{2*$val - $x}{$y}++; + delete $dots{$x}{$y}; + } elsif ($axis eq 'y' && $y > $val) { + $dots{$x}{2*$val - $y}++; + delete $dots{$x}{$y}; + } + } } +} + +for my $y (0 .. $maxy) { + for my $x (0 .. 40) { + print $dots{$x}{$y} ? '#' : ' '; + } + print "\n"; +} + -- 2.43.5