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
|
||||
Reference in New Issue
Block a user