From 65966ded9986753c5dd03476c7a175cab5cc510b Mon Sep 17 00:00:00 2001 From: Sebastian Bugge Date: Wed, 4 Dec 2024 09:35:26 +0100 Subject: [PATCH] Generate directions instead of hardcoding. --- day04.pl | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/day04.pl b/day04.pl index 7e25a9c..ea8ed78 100644 --- a/day04.pl +++ b/day04.pl @@ -10,16 +10,13 @@ while (<>) { push @wordgrid, [ split //, $_ ]; } -my @directions = ( - [[0, 0], [1, 1], [2, 2], [3, 3]], - [[0, 0], [-1, 1], [-2, 2], [-3, 3]], - [[0, 0], [0, 1], [0, 2], [0, 3]], - [[0, 0], [1, 0], [2, 0], [3, 0]], - [[0, 0], [-1, -1], [-2, -2], [-3, -3]], - [[0, 0], [1, -1], [2, -2], [3, -3]], - [[0, 0], [-1, 0], [-2, 0], [-3, 0]], - [[0, 0], [0, -1], [0, -2], [0, -3]], -); +my @directions = (); +for my $a (-1 .. 1) { + for my $b (-1 .. 1) { + next if ($a == 0 && $b == 0); + push @directions, [map { [$a * $_, $b * $_ ] } (0 .. 3)]; + } +} my @diagonal = ([-1, -1], [0, 0], [1, 1]); my @otherdiagonal = ([1, -1], [0, 0], [-1, 1]);