Skip to content

Instantly share code, notes, and snippets.

@mscha
Last active December 5, 2025 13:21
Show Gist options
  • Select an option

  • Save mscha/aecb6a97cf4ad6284844070610f964f7 to your computer and use it in GitHub Desktop.

Select an option

Save mscha/aecb6a97cf4ad6284844070610f964f7 to your computer and use it in GitHub Desktop.
Advent of Code 2025 day 5
#!/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