From c91c680cb48a9a3a98a10b46857378879292fffc Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 10 Dec 2024 12:44:09 -0500 Subject: [PATCH] Day ten --- 10.input | 40 +++++++++++++++++++++++++++++++ 10.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 10.input create mode 100755 10.rb diff --git a/10.input b/10.input new file mode 100644 index 0000000..fc6e37b --- /dev/null +++ b/10.input @@ -0,0 +1,40 @@ +1237651012319765432345545498010145891010 +0348943105408894001056654584321036712329 +9432342236717433145677723675490121903478 +8541051549826522238988812106789030876569 +7650760678434011032349900125699845686788 +6789898730569545601456987234016768798697 +4372123621078438780767076543223609894556 +5201014532321029892898123456104510983045 +6102306545452310121234010767040125672130 +7985430696101402100765109898234534343421 +8976521787210569011843210982129653254321 +8907823474389678123954301011018762169870 +7010910567478012335869832109800121078765 +6327823498565723546778789236789630989034 +5436910989367874695219654345676547876123 +6785403873456965784301105654565670945210 +5690342762106543287012234787654981234678 +4301201659014690196543989898943272221589 +5210112348323787017432170101032102100450 +1290010167654896528903061232589043678321 +0381201233210743434012354345672154509100 +5470300344984652143023403456983893210234 +6565415455675430032110512987870765210985 +7432326966556721243329601070121894387876 +8901457877876898358478732112434721296901 +9450962340945876569569540003965780105432 +2365871651232903478757651654875698987321 +1671010787891212349808932787034567076670 +0982107896500301256710149890123498125583 +1210212783410450901223456781210321034492 +0398347894323467814345069890323498503301 +3457656321301556030196178765432567412212 +6569845490219698123287234076781064565801 +7078780185428787654876543189899873278921 +8129698276538988140989812278734765103210 +9234567345445679031256701345625654014765 +0103216548765678120349810566010103425894 +0987607239854581234569809870987912436723 +1234568120123290107678712561296876543010 +2109879011234103238932103450345689832123 diff --git a/10.rb b/10.rb new file mode 100755 index 0000000..37a0f77 --- /dev/null +++ b/10.rb @@ -0,0 +1,72 @@ +#!/usr/bin/env ruby + +require "debug" + +problem = 10 +@input = File.readlines("#{problem}.input") +#@input = DATA.read.split("\n") + .map { _1.strip.chars.map { |c| c == "." ? "." : c.to_i }} +@debug = @input.size < 20 +#@debug = true + +def bounded?(point) + point.first >= 0 && + point.last >= 0 && + point.first < @input.size && + point.last < @input.first.size +end + +def traverse(start, height = 1) + result = [ + [start.first - 1, start.last], + [start.first + 1, start.last], + [start.first, start.last - 1], + [start.first, start.last + 1] + ].map do |point| + if !bounded?(point) || @input.dig(*point) == "." + [] + elsif @input.dig(*point) == 9 && height == 9 + if @debug + @paths[point.first][point.last] = @input.dig(*point) + end + [point] + elsif @input.dig(*point) == height + traverse(point, height + 1).flatten(1) + else # if @input.dig(*point) != height + [] + end + end + + if result.size > 0 && @debug + @paths[start.first][start.last] = @input.dig(*start) + end + + result +end + +if @debug + @paths = Array.new(@input.size) { Array.new(@input.first.size) { "." } } +end + +result = (0...@input.size).flat_map do |x| + (0...@input.first.size).map do |y| + [[x, y], traverse([x, y]).flatten(1)] if @input[x][y] == 0 + end.compact +end.to_h + +if @debug + puts @paths.map(&:join).join("\n") +end + +p result.values.map { _1.uniq.size }.sum +p result.values.map { _1.size }.sum + +__END__ +89010123 +78121874 +87430965 +96549874 +45678903 +32019012 +01329801 +10456732