[2024][ruby][10.2]
change xknnuqwmkzpqpzkpwxsslszsyptloytw
commit c59e3abc447d77613d463d046e8038733c8ce83c
author Alpha Chen <alpha@kejadlen.dev>
date
parent wxmozxrz
diff --git a/2024/ruby/day_10.rb b/2024/ruby/day_10.rb
index daca967..a096c76 100644
--- a/2024/ruby/day_10.rb
+++ b/2024/ruby/day_10.rb
@@ -5,75 +5,31 @@ map = DATA.readlines(chomp: true).flat_map.with_index {|row, y|
 }.to_h
 
 trailheads = map.filter_map { _2 == 0 && _1}
-# trailends = map.select { _2 == 9 }
 
 DELTAS = [[-1, 0], [1, 0], [0, -1], [0, 1]]
 
-def trailends(map, head)
-  queue = [head]
-  visited = Set.new
-  until queue.empty?
-    current = queue.shift
-    visited << current
+def trails(map, head)
+  active = [[head]]
+  finished = []
+  until active.empty?
+    current = active.shift
 
-    queue.concat(
-      DELTAS
-        .map {|d| current.zip(d).map { _1 + _2 } }
-        .select { map.fetch(_1, nil) == map.fetch(current) + 1 }
-        .reject { visited.include?(_1) }
-    )
+    steps = DELTAS
+      .map {|d| current.last.zip(d).map { _1 + _2 } }
+      .select { map.fetch(_1, nil) == map.fetch(current.last) + 1 }
+
+    if steps.empty?
+      finished << current
+    else
+      active.concat(steps.map { current + [_1] })
+    end
   end
-  visited.select { map.fetch(_1) == 9 }
+
+  finished
+    .select { _1.length > 2 && map.fetch(_1.last) == 9 }
+    .uniq
 end
 
-pp trailheads.sum { trailends(map, _1).length }
+pp trailheads.sum { trails(map, _1).length }
 
 __END__
-101987698323656762989721011010987456780121243210
-234012567210547891078892012101072345891030984569
-142103454345432012367743423232161654322345855678
-051054965236701103456656894343450783210496764789
-962167850149896543245765765456980892108587053298
-873076701056787600130876554967821876549652142103
-954987432165634512021989478876782965678703234012
-765876541074328763145672369805493454302212547654
-896968953981019854236981054012349854211243478943
-657857762876567610765100340101238763210798545692
-765446891001498723834232215293045678189877632781
-802335432312387654923341004382167669012368701020
-911124701421019870110056905673458978975459345210
-320099876598943065232167810569898963287561276349
-456787756787854104343054321678107854196510389458
-329878941012763210454988934521256981075431343367
-019561030323678923567867141010345410987123454321
-878432321410589854543252012367894322346001067800
-965678798523470189630141043454387011055432108912
-234309657631063238745632658965296545765443297983
-103210545432154127030745667870101239812344589854
-123451236943061032121836789654320149801256678765
-018760147858978945412921078761012456789107967810
-569676546567167656503832099908901327898798856921
-678989432100016987434512187819832010145610765430
-567010498761221498729603456520743016234431254321
-154321343254330327018777895431654327652342341232
-034501656189945012310689765432343678541056540765
-127652783076876763423676543087652589230767439856
-098743692105677894534589832198101450121858921045
-769878901234786765109689101265012376543929012234
-878963210121099889278770125654321289012210430123
-987654323452016776308765210787430100101121522434
-670122014560125655419654354896508921089037611078
-549831009871234534328323763011017632176548902369
-038742789965676548933214892182126543210423098454
-125653656234987837654300321099234304321012167823
-034564540165832921020311456788765015698143456910
-321078939874981021011232321001298923789874301501
-012981543403876120102345409876387654301565217652
-565470034512985433201456912345401543214321438943
-678321127621076894398567810126918754265410521032
-099128938902345765487656787637829662178345621001
-187019878213765496567678698540134543089210782789
-236520569347874385438569187687238956745432893610
-945431450956901276329430056796549875856721894523
-876532321876101301210321149823456721949890765434
-765321012765432340343434231012545430132101256545