Created
December 10, 2024 19:31
-
-
Save carlwiedemann/7592e12d18221bb784141ff6922fba71 to your computer and use it in GitHub Desktop.
Advent of Code 2024 day010.rb
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
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