Created
December 1, 2025 10:46
-
-
Save mscha/11ea2dddb3028a971f789c18d74f9a09 to your computer and use it in GitHub Desktop.
Advent of Code 2025 day 1 - efficient version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env raku | |
| use v6.d; | |
| $*OUT.out-buffer = False; # Autoflush | |
| # Advent of Code 2025 day 1 -- https://adventofcode.com/2025/day/1 | |
| class Safe | |
| { | |
| has Int $.modulo = 100; | |
| has Int $.dial = $!modulo div 2; | |
| has Bool $.verbose = False; | |
| has Int $.zero-hits = $!dial == 0 ?? 1 !! 0; | |
| has Int $.zero-passes = $!dial == 0 ?? 1 !! 0; | |
| method rotate(Str $move) | |
| { | |
| if $move ~~ m{^ (<[LR]>) (\d+) $ } { | |
| my $dir = $0 eq 'L' ?? -1 !! 1; | |
| my $count = $dir * $1; | |
| my $pos = $!dial + $count; | |
| my $new-dial = $pos % $!modulo; | |
| # Keep track of the number of time the dial lands on 0. | |
| $!zero-hits++ if $new-dial == 0; | |
| # Keep track of the number of times the dial passes (or lands | |
| # on) 0. When moving left, compensate for starting position 0. | |
| given $dir { | |
| when -1 { | |
| $!zero-passes += (100-$pos) div $!modulo; | |
| $!zero-passes-- if $!dial == 0; | |
| } | |
| when 1 { | |
| $!zero-passes += $pos div $!modulo; | |
| } | |
| } | |
| $!dial = $new-dial; | |
| say "Moved $move to $!dial [$!zero-hits/$!zero-passes]" if $!verbose; | |
| } | |
| else { | |
| die "Invalid move: $move"; | |
| } | |
| } | |
| } | |
| sub MAIN(IO() $inputfile where *.f = 'aoc01.input', Bool :v($verbose) = False) | |
| { | |
| my $safe = Safe.new(:$verbose); | |
| for $inputfile.lines -> $move { | |
| $safe.rotate($move); | |
| } | |
| say "Part one: $safe.zero-hits()"; | |
| say "Part two: $safe.zero-passes()"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment