#!/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 times 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;