Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 10, 2024 19:31
Show Gist options
  • Save carlwiedemann/7592e12d18221bb784141ff6922fba71 to your computer and use it in GitHub Desktop.
Save carlwiedemann/7592e12d18221bb784141ff6922fba71 to your computer and use it in GitHub Desktop.
Advent of Code 2024 day010.rb
require_relative "main"
module Day010
INPUT = File.read("INPUT.txt")
grid = INPUT.to_grid(cast_int: true)
v0s = grid.each_with_object([]) { |v, h, a| a.push(v) if h == 0 }
##########
# Part 1 #
##########
answer1 = v0s.sum do |v|
endpoints = []
dfs = ->(v, h = 1) do
endpoints.push(v) if h == 10
grid.four(v).filter { grid.get_value(_1) == h }.each do
dfs.call(_1, h + 1)
end
end
dfs.call(v)
endpoints.uniq.count
end
pp answer1
##########
# Part 2 #
##########
answer2 = v0s.sum do |v|
paths = []
dfs = ->(v, h = 1, history = []) do
paths.push(history) if h == 10
grid.four(v).filter { grid.get_value(_1) == h }.each do
dfs.call(_1, h + 1, history + [_1])
end
end
dfs.call(v)
paths.uniq.count
end
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment