aoc2021

Advent of Code 2021 solutions in Perl.
git clone git://git.samirparikh.com/aoc2021
Log | Files | Refs | README

commit 0105cf149e3c0b2f38b16a41ee3290d5bfd17231
parent 7b099f812ed51fb7c1f74efb91e3e71e8efc1579
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Tue,  7 Dec 2021 13:45:13 +0000

add initial files

Diffstat:
Aday04-oantolin.pl | 27+++++++++++++++++++++++++++
Aday04-smylers.pl | 20++++++++++++++++++++
Aday05-1.pl | 31+++++++++++++++++++++++++++++++
3 files changed, 78 insertions(+), 0 deletions(-)

diff --git a/day04-oantolin.pl b/day04-oantolin.pl @@ -0,0 +1,27 @@ +#!/usr/bin/env perl + +#use strict; +#use warnings; +use List::Util qw(none sum); +my @numbers = split /,/, <>; +$/ = ""; # read a paragraph at a time +my @cards = map {[map {[split " "]} split /\n/]} <>; + +my @scores; +foreach my $n (@numbers) { + foreach my $c (@cards) { + foreach my $i (0..4) { + foreach my $j (0..4) { + if ($c->[$i][$j] == $n) { + delete $c->[$i][$j]; + if (none {defined $c->[$_][$j]} 0..4 or + none {defined $c->[$i][$_]} 0..4) { + push @scores, $n * sum map {$c->[$_/5][$_%5]} (0..24); + delete $c->[$_] for 0..4; + } + } + } + } + } +} +printf "Part 1: %d. Part 2: %d.", $scores[0], $scores[-1]; diff --git a/day04-smylers.pl b/day04-smylers.pl @@ -0,0 +1,20 @@ +#!/usr/bin/env perl + +# Day 4 both parts, by Smylers + +use v5.14; +use warnings; +use List::AllUtils qw<sum>; + +$/ = ''; +my ($calls, @board, $winner) = <>; +while ($calls =~ /(\d+)/g) +{ + my $called = sprintf '%2d', $1; + @board = grep + { + s/$called\b/##/; + !/^[# ]+$|#(?:.{14}#){4}/ms + or do { say $called * sum /(\d+)/g if !$winner++ || @board == 1; 0 } + } @board; +} diff --git a/day05-1.pl b/day05-1.pl @@ -0,0 +1,31 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use v5.22; +use diagnostics; + +if (@ARGV !=1) { + die "Usage: $0 [input-filename]"; +} + +my $input_filename = $ARGV[0]; +open my $filehandle, '<', $input_filename or + die "Could not open input file $input_filename: $!"; + +chomp( my @input = ( <$filehandle> ) ); + +say foreach (@input); + +foreach (@input) { + my @captures = ( /(\d+),(\d+) -> (\d+),(\d+)/ ); + say join(":", @captures); +} + +my @matrix; + +$matrix[0][9] = 1; +$matrix[1][9] = 1; +$matrix[2][9] = 1; +$matrix[1][9]++; +say $matrix[1][9];