[2023][ruby][10.x] lol

main
Alpha Chen 12 months ago
parent 95a6798f7f
commit a3706096c3
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -79,7 +79,7 @@ p loop_.size / 2
# part two # part two
y_range, x_range = *loop_.extents y_range, x_range = *loop_.extents
p y_range.map {|y| p y_range.sum {|y|
x_range.chunk_while {|x_a, x_b| x_range.chunk_while {|x_a, x_b|
a = loop_.fetch([y, x_a], nil) a = loop_.fetch([y, x_a], nil)
b = loop_.fetch([y, x_b], nil) b = loop_.fetch([y, x_b], nil)
@ -93,12 +93,8 @@ p y_range.map {|y|
# only keep pipes that cross the horizontal axis # only keep pipes that cross the horizontal axis
deltas = end_tiles.flat_map {|tile| TILE_NEIGHBOR_DELTAS.fetch(tile) } deltas = end_tiles.flat_map {|tile| TILE_NEIGHBOR_DELTAS.fetch(tile) }
[[-1,0], [1,0]].all? {|dy| deltas.include?(dy) } [[-1,0], [1,0]].all? {|dy| deltas.include?(dy) }
}.inject([false, 0]) {|(enclosed, count), chunk| }.sum {|chunk|
start_tile = loop_.fetch([y,chunk[0]], nil) tiles = chunk.map {|x| loop_.fetch([y,x], nil) }
if start_tile.nil? (tiles[0]...tiles[0]) ? tiles.count(nil) : 0 # lol
[enclosed, count + (enclosed ? chunk.size : 0)] }
else
[!enclosed, count]
end
} }
}.sum(&:last)

Loading…
Cancel
Save