Posts

Raku: Advent of Code 2020 - Day Thirteen

Raku solutions to Day Thirteen of the 2020 Advent of Code. Today's puzzle involved bus routes. Part One requires you to figure out which route id will arrive closest to a timestamp. The answer to Part Two involves identifying when an alignment of departure times across route ids will occur. #!/usr/bin/env raku use v6.d ; my ( $depart , $line ) = 'input' . IO . slurp . split ( " \n " ); $depart .= Int ; say "Part One: " ~ [*] $line . split ( ',' ) . grep ( none 'x' ) ». Int . map ({ $_ , $_ - ( $depart ) mod $_ }) . min ({ . [ 1 ] }) . flat ; # index of @l is the offset from factor my @l = $line . split ( ',' ); my ( $r , $factor ) = ( 0 , @l [ 0 ] . Int ); for 1 ..^ @l -> $i { next if @l [ $i ] eq 'x' ; $r = ( $r , { $_ + $factor } ... -> $a { ( $a + $i ) mod @l [ $i ] . Int == 0 }) . tail ; $factor *= @l [ $i ]; } say "Part Two: $r " ; Part One Given the sample inpu

Raku: Advent of Code 2020 - Day Twelve

Below are my Raku solutions to Day Twelve puzzles for the 2020 Advent of Code. No effort was put into making things cleverly concise.  #!/usr/bin/env raku use v6.d ; my $face = 'E' ; my $c = <E S W N> ; # compass my %c = ( E => 0 , S => 1 , W => 2 , N => 3 ); # reverse lookup my ( $x , $y ) = ( 0 , 0 ); # ship location 'input' . IO . lines . map ({ my ( $direction , $qty ) = ( ~. substr ( 0 , 1 ) , +. substr ( 1 )); $direction = $face if $direction eq 'F' ; given $direction { when 'N' { $y += $qty } when 'S' { $y -= $qty } when 'E' { $x += $qty } when 'W' { $x -= $qty } when 'L' { my $i = %c { $face }; $i -= $qty div 90 ; $i = $i mod 4 ; $face = $c [ $i ]; } when 'R' { my $i = %c { $face }; $i += $qty div 90 ; $i = $i mod 4 ; $face = $c [ $i ]; } } }); say "Part One: { abs ( $x ) + abs ( $y )} "

Raku: Advent of Code 2020 - Day Eleven

The puzzle for Day Eleven of Advent of Code involved a cellular automaton related to where people prefer to sit in a waiting room based on whether adjacent seats are empty or occupied. My Raku solution follows. My initial solution worked, albeit slowly and while leaking memory. So instead of re-inventing a wheel poorly, I looked up the Raku example for the Game of Life on Rosetta Code... And reworked it to solve both parts of today's puzzles. [Updates to include code commentary will follow tomorrow] #!/usr/bin/env raku use v6.d ; class Automaton { subset Layout of Str where { # all rows same length and contain only . L or # . lines >>. chars . unique == 1 and m/ ^^ <[ .L# ]> +$$ / } has Int ( $.width , $.height ); has Bool $.changed is rw = False ; has Int $.tolerance is rw = 4 ; has @.a ; multi method new ( Layout $s , Int $tolerance ? = 4 ) { self . new ( :width ( . pick . chars ) , :height

Raku: Advent of Code 2020 - Day Ten

Today's Advent of Code 2020 puzzle involves getting power (measured in jolts) to Santa's computing device from an outlet that provides 0 jolts. Santa's has some very special joltage adapters which act as step-up transformers. Each adapter can increase the joltage by up to 3 jolts. The input for the puzzle is a list of numbers representing the maximum joltage of each of Santa's adapters. And Santa's computing device has its own final adapter which is 3 jolts higher than the highest adapter in the bag. Below are Raku solutions to Day Ten's puzzle. I had a lot of fun getting Part One down to a minimal (obfuscated) 66 character short form solution: say lines ». Int . sort .& {(( |@ $_ ,. max + 3 ) Z- ( 0 ,|@ $_ )) . Bag .& { . { 1 } *. { 3 }}} Other than some typical boilerplate, the full length solution which follows isn't much longer. Though it is formatted a bit better for comprehension. Part Two and its challenge to calculate the count of all

Raku: Advent of Code 2020 - Day Nine

Below is my initial Raku solution to the puzzle for Day Nine of Advent of Code. #!/usr/bin/env raku use v6.d ; unit sub MAIN ( IO () : $input where * . f = $?FILE . IO . sibling ( 'input' ), Int : $part where * == 1 | 2 = 1 , # Solve Part One or Part Two? --> Nil ); given $part { my @xmas = $input . lines . map ({ + $_ }); when 1 { say find_invalid ( @xmas , 25 ) } when 2 { say weakness ( @xmas , find_invalid ( @xmas , 25 )) } } sub find_invalid ( @xmas , Int $psize --> Int ){ my $idx = 0 ; repeat { my @p = @xmas [ $idx .. $idx + $psize - 1 ]; my @sum = @p . combinations ( 2 ) . map ({ . sum }) . unique ; last if Nil === @sum . first ( @xmas [ $idx + $psize ]); $idx ++ ; } while $idx + $psize < @xmas . end ; return + @xmas [ $idx + $psize ]; } sub weakness ( @xmas , $invalid ) { my @contig ; OUTSIDE: loop ( my Int $i = 0 ; $i < @xmas . elems ; $i ++ ) { my $j = $i ;

Raku: Advent of Code 2020 - Day Eight

Here is my Raku solution to the puzzle for Day 8 of the 2020 Advent of Code. If you enjoy the post or have improvements to offer, please leave a comment. Code commentary follows the code... [08.raku]  #!/usr/bin/env raku use v6.d ; grammar HGC { rule TOP { < inst > + } rule inst { < op > < arg > } token op { 'acc' | 'jmp' | 'nop' } token arg { <[ +- ]> \d + } } class HGCActions { has $!stack = []; method inst ( $/ ) { $!stack . push ( %( op => ~ $< op > , arg => + $< arg > ) ) } method exec { my ( $pos , $acc ) = ( 0 , 0 ); for $!stack . list { . < visit > = 0 } while $pos < $!stack . elems { my ( $op , $arg ) = $!stack [ $pos ]< op arg >; return ( 1 , $acc ) if ++ $!stack [ $pos ]< visit > > 1 ; # Err Loop given $op { when 'nop' { $pos ++ } when 'acc' { $pos