aoc2015

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

commit 51a829adb30dcc4dab65ffc5b2c8bc03ef68db10
parent db99c61b388768e429d1a50b4227c6d36cb1a1b8
Author: Samir Parikh <noreply@samirparikh.com>
Date:   Fri, 28 Oct 2022 21:54:35 +0000

solve day10

Diffstat:
Aday10/day10.pl | 43+++++++++++++++++++++++++++++++++++++++++++
1 file changed, 43 insertions(+), 0 deletions(-)

diff --git a/day10/day10.pl b/day10/day10.pl @@ -0,0 +1,43 @@ +#!/usr/local/bin/perl +# day 2015-10 + +use strict; +use warnings; +use v5.32; + +my $string = 1113122113; + +# +# solution inspired by Abigail at: +# https://github.com/Abigail/AdventOfCode2015/blob/master/Day_10/look-and-say.pl +# + +sub next_string { + my $string = shift; + my $result = ""; + + while ( $string =~ /\G((.)\2*)/g) { + +# the capture variable $1 represents what is matched in the outer most set of +# parentheses: (.)\2* +# this matches for any number (.)followed by zero or more occurrences (*) of +# itself using a back reference (\2). +# the capture variable $2 represents the number. The length of $1 represents +# the number of tims that number was captured. To see how it works, uncomment +# the following line: +# say length $1, " ", $2, "s"; +# the \G anchor can be used to start the next match on the same string where the +# last match left off. For more information, please see: +# https://perldoc.perl.org/perlfaq6#What-good-is-%5CG-in-a-regular-expression? + + $result .= ( length $1 ) . $2; + } + + return $result; +} + +$string = next_string( $string ) foreach ( 1 .. 40 ); +say "part 1: ", length $string; + +$string = next_string( $string ) foreach ( 1 .. 10 ); # to reach 50 iterations +say "part 2: ", length $string;