Skip to content

Commit 123483e

Browse files
author
Robb Kidd
committed
day 10, part 2
Copied more from Bill so I could move on with my life. Thanks, Bill.
1 parent a5b5363 commit 123483e

File tree

1 file changed

+78
-6
lines changed

1 file changed

+78
-6
lines changed

2023/ruby/day10.rb

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,25 @@ class Day10 < Day # >
1010
# day = Day10.new(MORE_COMPLEX_INPUT)
1111
# day.part1 #=> 8
1212
def part1
13-
pipe_maze = PipeMaze.new(input)
14-
# puts pipe_maze.to_ugly_sweater
15-
pipe_maze.loop_path.length / 2
13+
@pipe_maze = PipeMaze.new(input)
14+
@pipe_maze.loop_path.length / 2
1615
end
1716

1817
# @example
19-
# day.part2 #=> 'how are you'
18+
# day = Day10.new(PART2_INPUT)
19+
# day.part1
20+
# day.part2 #=> 4
21+
# @example larger example
22+
# day = Day10.new(PART2_LARGER_EXAMPLE)
23+
# day.part1
24+
# day.part2 #=> 8
25+
# @example junk example
26+
# day = Day10.new(PART2_JUNK_EXAMPLE)
27+
# day.part1
28+
# day.part2 #=> 10
2029
def part2
30+
puts @pipe_maze.to_ugly_sweater
31+
@pipe_maze.inner_cells.length
2132
end
2233

2334
EXAMPLE_INPUT = <<~INPUT
@@ -28,13 +39,51 @@ def part2
2839
.....
2940
INPUT
3041

31-
MORE_COMPLEX_INPUT= <<~INPUT
42+
MORE_COMPLEX_INPUT = <<~INPUT
3243
..F7.
3344
.FJ|.
3445
SJ.L7
3546
|F--J
3647
LJ...
3748
INPUT
49+
50+
PART2_INPUT = <<~INPUT
51+
...........
52+
.S-------7.
53+
.|F-----7|.
54+
.||.....||.
55+
.||.....||.
56+
.|L-7.F-J|.
57+
.|..|.|..|.
58+
.L--J.L--J.
59+
...........
60+
INPUT
61+
62+
PART2_LARGER_EXAMPLE = <<~INPUT
63+
.F----7F7F7F7F-7....
64+
.|F--7||||||||FJ....
65+
.||.FJ||||||||L7....
66+
FJL7L7LJLJ||LJ.L-7..
67+
L--J.L7...LJS7F-7L7.
68+
....F-J..F7FJ|L7L7L7
69+
....L7.F7||L7|.L7L7|
70+
.....|FJLJ|FJ|F7|.LJ
71+
....FJL-7.||.||||...
72+
....L---J.LJ.LJLJ...
73+
INPUT
74+
75+
PART2_JUNK_EXAMPLE = <<~INPUT
76+
FF7FSF7F7F7F7F7F---7
77+
L|LJ||||||||||||F--J
78+
FL-7LJLJ||||||LJL-77
79+
F--JF--7||LJLJ7F7FJ-
80+
L---JF-JLJ.||-FJLJJ7
81+
|F|F-JF---7F7-L7L|7|
82+
|FFJF7L7F-JF7|JL---7
83+
7-L-JL7||F7|L7F-7F7|
84+
L.L7LFJ|||||FJL7||LJ
85+
L7JLJL-JLJLJL--JLJ.L
86+
INPUT
3887
end
3988

4089
class PipeMaze
@@ -91,6 +140,27 @@ def step_from(coords, direction)
91140
]
92141
end
93142

143+
def inner_cells
144+
@inner_cells ||= find_inner_cells
145+
end
146+
147+
# Very much copied from Bill Mill's hack; works for junk and real input, but off-by-one for others.
148+
def find_inner_cells
149+
innercells = []
150+
@grid.row_bounds.map do |row|
151+
bars = 0
152+
@grid.column_bounds.map do |column|
153+
on_the_loop = loop_path.include?([row,column])
154+
if on_the_loop && ["│", "└", "┘", "╳"].include?(at([row, column]))
155+
bars += 1
156+
elsif bars % 2 == 1 && !on_the_loop
157+
innercells << [row,column]
158+
end
159+
end
160+
end
161+
return innercells
162+
end
163+
94164
def at(coords)
95165
@grid.at(coords)
96166
end
@@ -112,7 +182,6 @@ def towards(from_coords, direction)
112182

113183
# [step travel direction, pipe type] => next step travel direction
114184
PIPE_FLOWS = {
115-
# "│─└┘┐┌░╳"
116185
# | is a vertical pipe connecting north and south.
117186
[:north, "│"] => :north,
118187
[:south, "│"] => :south,
@@ -145,9 +214,12 @@ def towards(from_coords, direction)
145214
}
146215

147216
def to_ugly_sweater
217+
"\n" +
148218
@grid.to_s { |coords, value|
149219
if loop_path.include?(coords)
150220
value.make_it_red
221+
elsif inner_cells.include?(coords)
222+
"I".make_it_green.make_it_bold
151223
else
152224
value.make_it_green
153225
end

0 commit comments

Comments
 (0)