Change day 12 from part 1 to part 2 (cleanup later)

This commit is contained in:
Danila Fedorin 2022-12-12 10:15:54 -08:00
parent 5cd1a7f157
commit 76705dc6fb

View File

@ -10,7 +10,7 @@ def add_at(pos, c, x, y)
o = 'a' if o == 'S' o = 'a' if o == 'S'
o = 'z' if o == 'E' o = 'z' if o == 'E'
return if (o.ord-c.ord) > 1 return if (c.ord-o.ord) > 1
EDGES[pos] << ({x,y}); EDGES[pos] << ({x,y});
end end
@ -29,17 +29,17 @@ def add_nearby(x, y)
end end
from = {0,0} from = {0,0}
to = {0,0} to = {100,100}
INPUT.each_with_index do |row, y| INPUT.each_with_index do |row, y|
row.each_with_index do |c, x| row.each_with_index do |c, x|
pos = {x, y} pos = {x, y}
add_nearby(x, y) add_nearby(x, y)
from = pos if c == 'S' from = pos if c == 'E'
to = pos if c == 'E'
end end
end end
costs = {from => 0} costs = {from => 0}
mins = {} of Tuple(Int32, Int32) => Int32
visited = Set(Tuple(Int32, Int32)).new visited = Set(Tuple(Int32, Int32)).new
while !costs.empty? while !costs.empty?
@ -53,6 +53,7 @@ while !costs.empty?
end end
costs.delete k costs.delete k
mins[k] = v
visited << k visited << k
INPUT[k[1]][k[0]] = INPUT[k[1]][k[0]].upcase INPUT[k[1]][k[0]] = INPUT[k[1]][k[0]].upcase
puts k puts k
@ -67,9 +68,16 @@ while !costs.empty?
end end
end end
INPUT.each do |line| lengths = [] of Int32
line.each do |c| puts mins
INPUT.each_with_index do |line, y|
line.each_with_index do |c, x|
next unless c == 'A'
if amin = mins[{x,y}]?
lengths << amin
end
print c print c
end end
puts puts
end end
puts lengths.min