Skip to content

Commit e1d012b

Browse files
author
Robb Kidd
committed
day 8, part 1
1 parent 2ab5a0f commit e1d012b

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

2023/ruby/day08.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require_relative 'day'
2+
3+
class Day08 < Day # >
4+
5+
# @example
6+
# day.part1 #=> 2
7+
def part1
8+
parse(input)
9+
steps = 0
10+
node = "AAA"
11+
@instructions
12+
.cycle do |instruction|
13+
break if node == "ZZZ"
14+
steps += 1
15+
node = @network[node][instruction]
16+
end
17+
steps
18+
end
19+
20+
# @example
21+
# day.part2 #=> 'how are you'
22+
def part2
23+
end
24+
25+
# @example
26+
# inst, net = day.parse(EXAMPLE_INPUT)
27+
# inst #=> ["R", "L"]
28+
def parse(input)
29+
stanzas = input.split("\n\n")
30+
31+
@instructions = stanzas[0].chars
32+
33+
@network = {}
34+
stanzas[1]
35+
.each_line
36+
.map { |line| line.scan(/\w+/) }
37+
.each { |node, left, right|
38+
@network[node] = { "L" => left, "R" => right }
39+
}
40+
41+
[@instructions, @network]
42+
end
43+
44+
EXAMPLE_INPUT = <<~INPUT
45+
RL
46+
47+
AAA = (BBB, CCC)
48+
BBB = (DDD, EEE)
49+
CCC = (ZZZ, GGG)
50+
DDD = (DDD, DDD)
51+
EEE = (EEE, EEE)
52+
GGG = (GGG, GGG)
53+
ZZZ = (ZZZ, ZZZ)
54+
INPUT
55+
56+
SIX_STEPS_INPUT = <<~INPUT
57+
LLR
58+
59+
AAA = (BBB, BBB)
60+
BBB = (AAA, ZZZ)
61+
ZZZ = (ZZZ, ZZZ)
62+
INPUT
63+
end

0 commit comments

Comments
 (0)