Created
December 11, 2023 16:42
-
-
Save carlwiedemann/edabec5df26a330f9a33389292553d7a to your computer and use it in GitHub Desktop.
Advent of Code 2023 day010.rb
This file contains 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 | |
GUIDES = { | |
"7" => { V.northward => V.westward, V.eastward => V.southward }, | |
"F" => { V.westward => V.southward, V.northward => V.eastward }, | |
"L" => { V.southward => V.eastward, V.westward => V.northward }, | |
"J" => { V.eastward => V.northward, V.southward => V.westward }, | |
"-" => { V.eastward => V.eastward, V.westward => V.westward }, | |
"|" => { V.southward => V.southward, V.northward => V.northward } | |
} | |
########## | |
# Part 1 # | |
########## | |
start = grid.find("S") | |
cursor = nil | |
start.four.each.with_index do |v, i| | |
value = grid.get_value(v) | |
if [ | |
i == 0 && ["7", "J", "-"].include?(value), | |
i == 1 && ["7", "F", "|"].include?(value), | |
i == 2 && ["F", "L", "-"].include?(value), | |
i == 3 && ["L", "J", "|"].include?(value), | |
].any? | |
cursor = v | |
break | |
end | |
end | |
raise 'wat' if cursor.nil? | |
last_cursor = start | |
corners = [] | |
cursors = [] | |
i = 0 | |
loop do | |
cursors.push(cursor) | |
value = grid.get_value(cursor) | |
break if value == "S" && i > 0 | |
if ["7", "F", "L", "J"].include?(value) | |
corners.push(cursor) | |
end | |
ingress = cursor - last_cursor | |
egress = GUIDES[value][ingress] | |
last_cursor = cursor | |
cursor = egress + cursor | |
i += 1 | |
end | |
answer1 = (i + 1) / 2 | |
pp answer1 | |
########## | |
# Part 2 # | |
########## | |
corners.unshift(start) | |
poly = Polygon.new(*corners) | |
c = grid.reduce(0) do |memo, v, _value| | |
memo + ((!cursors.include?(v) && poly.contains_point?(v)) ? 1 : 0) | |
end | |
answer2 = c | |
pp answer2 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment