[2024][ruby][18.2]

main
Alpha Chen 5 days ago
parent fb7247cef1
commit 5126ad233a
Signed by: alpha
SSH Key Fingerprint: SHA256:3fOT8fiYQG/aK9ntivV3Bqtg8AYQ7q4nV6ZgihOA20g

@ -1,26 +1,24 @@
falling_bytes = DATA.read.scan(/\d+/).map(&:to_i).each_slice(2).to_a falling_bytes = DATA.read.scan(/\d+/).map(&:to_i).each_slice(2).to_a
corrupted = falling_bytes[0,1024] MAX = 70
DELTAS = [[-1,0], [1,0], [0,-1], [0,1]]
max = 70 def path(corrupted)
start = [0,0]
stop = [MAX,MAX]
deltas = [[-1,0], [1,0], [0,-1], [0,1]] scores = Hash.new(Float::INFINITY)
scores[start] = 0
start = [0,0] frontier = [start]
stop = [max,max] visited = Set.new
until scores.has_key?(stop)
scores = Hash.new(Float::INFINITY)
scores[start] = 0
frontier = [start]
visited = Set.new
until scores.has_key?(stop)
current = frontier.shift current = frontier.shift
visited << current visited << current
neighbors = deltas.map {|delta| neighbors = DELTAS.map {|delta|
current.zip(delta).map { _1 + _2 } current.zip(delta).map { _1 + _2 }
}.select {|xy| }.select {|xy|
xy.all? { (0..max).cover?(_1) } xy.all? { (0..MAX).cover?(_1) }
}.reject {|xy| }.reject {|xy|
corrupted.include?(xy) || visited.include?(xy) corrupted.include?(xy) || visited.include?(xy)
} }
@ -32,9 +30,19 @@ until scores.has_key?(stop)
frontier.uniq! frontier.uniq!
frontier.sort_by! { scores.fetch(_1) } frontier.sort_by! { scores.fetch(_1) }
end
scores.fetch(stop)
end end
pp scores.fetch(stop) i = (0...falling_bytes.length).bsearch {|i|
begin
path(falling_bytes[0,i]) && false
rescue
true
end
}
puts falling_bytes.fetch(i-1).join(?,)
__END__ __END__
54,47 54,47

Loading…
Cancel
Save