Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 5, 2023 18:56
Show Gist options
  • Save henrik/5466b2f3ccffc114af1547169a1fa996 to your computer and use it in GitHub Desktop.
Save henrik/5466b2f3ccffc114af1547169a1fa996 to your computer and use it in GitHub Desktop.
Advent of Code day 5
data = DATA.read
seeds = data[/seeds: (.+)/, 1].split.map(&:to_i)
maps = data.scan(/map:\n(.+?)(?:\n\n|\z)/m).map { |(x)| x.lines.map { _1.split.map(&:to_i) } }
locations = seeds.map { |seed|
maps.reduce(seed) { |input, map|
d, s, _r = map.find { |_d, s, r| (s..(s + r)).cover?(input) }
d ? d + (input - s) : input
}
}
puts locations.min
__END__
Data goes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment