@@ -10,14 +10,25 @@ class Day10 < Day # >
10
10
# day = Day10.new(MORE_COMPLEX_INPUT)
11
11
# day.part1 #=> 8
12
12
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
16
15
end
17
16
18
17
# @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
20
29
def part2
30
+ puts @pipe_maze . to_ugly_sweater
31
+ @pipe_maze . inner_cells . length
21
32
end
22
33
23
34
EXAMPLE_INPUT = <<~INPUT
@@ -28,13 +39,51 @@ def part2
28
39
.....
29
40
INPUT
30
41
31
- MORE_COMPLEX_INPUT = <<~INPUT
42
+ MORE_COMPLEX_INPUT = <<~INPUT
32
43
..F7.
33
44
.FJ|.
34
45
SJ.L7
35
46
|F--J
36
47
LJ...
37
48
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
38
87
end
39
88
40
89
class PipeMaze
@@ -91,6 +140,27 @@ def step_from(coords, direction)
91
140
]
92
141
end
93
142
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
+
94
164
def at ( coords )
95
165
@grid . at ( coords )
96
166
end
@@ -112,7 +182,6 @@ def towards(from_coords, direction)
112
182
113
183
# [step travel direction, pipe type] => next step travel direction
114
184
PIPE_FLOWS = {
115
- # "│─└┘┐┌░╳"
116
185
# | is a vertical pipe connecting north and south.
117
186
[ :north , "│" ] => :north ,
118
187
[ :south , "│" ] => :south ,
@@ -145,9 +214,12 @@ def towards(from_coords, direction)
145
214
}
146
215
147
216
def to_ugly_sweater
217
+ "\n " +
148
218
@grid . to_s { |coords , value |
149
219
if loop_path . include? ( coords )
150
220
value . make_it_red
221
+ elsif inner_cells . include? ( coords )
222
+ "I" . make_it_green . make_it_bold
151
223
else
152
224
value . make_it_green
153
225
end
0 commit comments