Day 8
This commit is contained in:
26
2023/day8/p1.rb
Normal file
26
2023/day8/p1.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
f = File.open('./2023/day8/data')
|
||||
directions = f.readline.strip.split('')
|
||||
|
||||
graph = {}
|
||||
f.each_line do |line|
|
||||
line.strip!
|
||||
next if line.empty?
|
||||
|
||||
graph[line.split(' ')[0]] = line.split('(')[1].split(')')[0].split(', ')
|
||||
end
|
||||
|
||||
i = 0
|
||||
res = 0
|
||||
cur_pos = 'AAA'
|
||||
while cur_pos != 'ZZZ'
|
||||
i = 0 if i == directions.length
|
||||
|
||||
cur_pos = graph[cur_pos][0] if directions[i] == 'L'
|
||||
cur_pos = graph[cur_pos][1] if directions[i] == 'R'
|
||||
i += 1
|
||||
res += 1
|
||||
end
|
||||
|
||||
puts res
|
||||
37
2023/day8/p2.rb
Normal file
37
2023/day8/p2.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
f = File.open('./2023/day8/data')
|
||||
directions = f.readline.strip.split('')
|
||||
|
||||
starting_vertices = []
|
||||
graph = {}
|
||||
f.each_line do |line|
|
||||
line.strip!
|
||||
next if line.empty?
|
||||
|
||||
vertex_label = line.split(' ')[0]
|
||||
graph[vertex_label] = line.split('(')[1].split(')')[0].split(', ')
|
||||
starting_vertices << vertex_label if vertex_label.end_with? 'A'
|
||||
end
|
||||
|
||||
res = -1
|
||||
starting_vertices.each do |vertex|
|
||||
i = 0
|
||||
curr_length = 0
|
||||
cur_pos = vertex
|
||||
until cur_pos.end_with? 'Z'
|
||||
i = 0 if i == directions.length
|
||||
|
||||
cur_pos = graph[cur_pos][0] if directions[i] == 'L'
|
||||
cur_pos = graph[cur_pos][1] if directions[i] == 'R'
|
||||
i += 1
|
||||
curr_length += 1
|
||||
end
|
||||
res = if res == -1
|
||||
curr_length
|
||||
else
|
||||
res.lcm(curr_length)
|
||||
end
|
||||
end
|
||||
|
||||
puts res
|
||||
Reference in New Issue
Block a user