-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd8.rb
70 lines (59 loc) · 1.39 KB
/
d8.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def part1(file_name)
f = File.open(file_name, 'r')
directions = f.readline.chomp.chars
f.readline
map = {}
until f.eof
src, left, right = f.readline.chomp.split(/ = \(|, |\)/)[0..2]
map[src] = { 'L' => left, 'R' => right }
end
steps = 0
location = 'AAA'
until location == 'ZZZ'
direction = directions[steps % directions.length]
steps += 1
location = map[location][direction]
end
puts(steps)
ensure
f.close
end
def gcd(a, b)
return a if b == 0
gcd(b, a % b)
end
def lcm(a, b)
a * (b / gcd(a, b))
end
def part2(file_name)
f = File.open(file_name, 'r')
directions = f.readline.chomp.chars
f.readline
map = {}
until f.eof
src, left, right = f.readline.chomp.split(/ = \(|, |\)/)[0..2]
map[src] = { 'L' => left, 'R' => right }
end
locations = map.keys.select { |k| k.end_with?('A') }
# find how many steps are needed for each starting location
steps = locations.map do |location|
count = 0
until location.end_with?('Z')
direction = directions[count % directions.length]
count += 1
location = map[location][direction]
end
count
end
# find the least common multiple of all the step counts
result = steps.reduce(1) { |acc, x| lcm(acc, x) }
puts(result)
ensure
f.close
end
puts("== PART 1 ==")
part1("test/d8.txt")
part1("input/d8.txt")
puts("== PART 2 ==")
part2("test/d8.txt")
part2("input/d8.txt")