Last active
December 5, 2025 13:21
-
-
Save mscha/aecb6a97cf4ad6284844070610f964f7 to your computer and use it in GitHub Desktop.
Advent of Code 2025 day 5
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 5 -- https://adventofcode.com/2025/day/5 | |
| unit sub MAIN(IO() $inputfile where *.f = 'aoc05.input', Bool :v($verbose) = False); | |
| sub to-range(Str $s) { Range.new(|$s.comb(/\d+/)».Int) } | |
| sub consolidate(@ranges) | |
| { | |
| gather { | |
| my $current; | |
| for @ranges.sort -> $r { | |
| FIRST { | |
| $current = $r; | |
| next; | |
| } | |
| LAST { | |
| say 'Taking ', $current if $verbose; | |
| take $current; | |
| } | |
| if $current.max + 1 ≥ $r.min { | |
| # Overlapping (or adjoining) ranges: merge | |
| my $new = min($current.min, $r.min) .. max($current.max, $r.max); | |
| say 'Merging ', $current, ' and ', $r, ' to ', $new if $verbose; | |
| $current = $new; | |
| } | |
| else { | |
| # Non-overlapping ranges: take the first, remember the second | |
| say 'Taking ', $current if $verbose; | |
| take $current; | |
| $current = $r; | |
| } | |
| } | |
| } | |
| } | |
| my ($fresh-list, $available-list) = $inputfile.slurp.split("\n\n"); | |
| my @fresh = consolidate($fresh-list.lines».&to-range); | |
| my @available = $available-list.lines».Int; | |
| say "Part one: ", @available.grep(any(@fresh)).elems; | |
| say "Part two: ", @fresh».elems.sum; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment