[2023][ruby][10.x] lol
change nqtyksmlutssytmqwmsuqyzwxqlxyznw
commit 29a4f11f27a43c9a256c346f189e18a7e33f8f5e
author Alpha Chen <alpha@kejadlen.dev>
date
parent txwqlvsp
diff --git a/2023/ruby/day_10.rb b/2023/ruby/day_10.rb
index 50c25c3..ec848d8 100644
--- a/2023/ruby/day_10.rb
+++ b/2023/ruby/day_10.rb
@@ -79,7 +79,7 @@ p loop_.size / 2
 
 # part two
 y_range, x_range = *loop_.extents
-p y_range.map {|y|
+p y_range.sum {|y|
   x_range.chunk_while {|x_a, x_b|
     a = loop_.fetch([y, x_a], 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
     deltas = end_tiles.flat_map {|tile| TILE_NEIGHBOR_DELTAS.fetch(tile) }
     [[-1,0], [1,0]].all? {|dy| deltas.include?(dy) }
-  }.inject([false, 0]) {|(enclosed, count), chunk|
-    start_tile = loop_.fetch([y,chunk[0]], nil)
-    if start_tile.nil?
-      [enclosed, count + (enclosed ? chunk.size : 0)]
-    else
-      [!enclosed, count]
-    end
+  }.sum {|chunk|
+    tiles = chunk.map {|x| loop_.fetch([y,x], nil) }
+    (tiles[0]...tiles[0]) ? tiles.count(nil) : 0 # lol
   }
-}.sum(&:last)
+}