Skip to content

Instantly share code, notes, and snippets.

@smortex
Created December 4, 2022 20:11
Show Gist options
  • Save smortex/d6d866dd7b24993c5f6626696e63bb1f to your computer and use it in GitHub Desktop.
Save smortex/d6d866dd7b24993c5f6626696e63bb1f to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 4
plan aoc::day4 (
Boolean $sample = false,
) {
$file = $sample ? {
false => 'aoc/2022/day4.txt',
true => 'aoc/2022/day4.sample.txt',
}
$data = file($file).split('\n').map |$line| {
$line.split(',').map |$part| {
$part.split('-').map |$n| { Integer($n) }
}
}
$data.filter |$line| {
$line[0][0] >= $line[1][0] and $line[0][1] <= $line[1][1] or
$line[1][0] >= $line[0][0] and $line[1][1] <= $line[0][1]
}.count.out::message
$data.filter |$line| {
$max_start = [$line[0][0], $line[1][0]].max
$min_end = [$line[0][1], $line[1][1]].min
$min_end >= $max_start
}.count.out::message
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment