From be9f3664c99486bc2362d50fcd6c6c956ded4d3d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Sat, 4 Dec 2021 10:59:29 +0100 Subject: [PATCH] Day 4: finally regexes!!! --- 07.pl | 27 +++++++++++++++++++++++++++ 08.pl | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100755 07.pl create mode 100755 08.pl diff --git a/07.pl b/07.pl new file mode 100755 index 0000000..c304d7c --- /dev/null +++ b/07.pl @@ -0,0 +1,27 @@ +#!/usr/bin/perl -w + +use v5.16; + +my @drawn = split/[,\s]/, scalar <>; +scalar <>; + +$/ = "\n\n"; + +my @boards = <>; + +say $boards[0]; + +for my $d (@drawn) { + for my $board (@boards) { + $board =~ s/\b$d\b/' ' x length $d/e; + if ($board =~ / / + || $board =~ /\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}/xms) { + say $board; + $board =~ s/(?<=\d)\s+(?=\d)/+/gxms; + say "$board"; + say eval "$d*($board)"; + exit; + } + } +} + diff --git a/08.pl b/08.pl new file mode 100755 index 0000000..d1f74b4 --- /dev/null +++ b/08.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl -w + +use v5.16; + +my @drawn = split/[,\s]/, scalar <>; +scalar <>; + +$/ = "\n\n"; + +my @boards = <>; + +say $boards[0]; + +my $remaining = @boards; + +for my $d (@drawn) { + for my $board (@boards) { + $board =~ s/\b$d\b/' ' x length $d/e; + if ($board =~ / / + || $board =~ /\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}.{12}\s{3}/xms) { + if (--$remaining) { + $board = 'x'; + next; + } + say $board; + $board =~ s/(?<=\d)\s+(?=\d)/+/gxms; + say "$board"; + say eval "$d*($board)"; + exit; + } + } +} + -- 2.43.5