[2018][ruby][11.2]

sorbet
Alpha Chen 6 years ago
parent df293cb0bb
commit 09cbeea533

@ -1,6 +1,4 @@
# p ARGF.read.chomp.lines.map(&:chomp) INPUT = ARGF.read.chomp.to_i
INPUT = 7400
FuelCell = Struct.new(*%i[ x y ]) do FuelCell = Struct.new(*%i[ x y ]) do
def rack_id def rack_id
@ -19,27 +17,22 @@ FuelCell = Struct.new(*%i[ x y ]) do
end end
grid = Hash.new(0) grid = Hash.new(0)
(1..300).each {|x| (1..300).each do |x|
(1..300).each {|y| (1..300).each do |y|
grid[[x,y]] = FuelCell.new(x, y).power_level grid[[x,y]] = FuelCell.new(x, y).power_level
}
}
(44..48).each do |y|
(32..36).each do |x|
print " #{grid[[x,y]]} "
end end
puts
end end
squares = Hash.new(0) squares = Hash.new(0)
(2..299).each {|x| (1..300).each do |size|
(2..299).each {|y| (1..(300-size+1)).each {|x|
power_level = [[-1, -1], [0, -1], [1, -1], (1..(300-size+1)).each {|y|
[-1, 0], [0, 0], [1, 0], deltas = (0...size).flat_map {|d| [[size-1, d], [d, size-1]] }
[-1, 1], [0, 1], [1, 1]].map {|(dx,dy)| grid[[x+dx, y+dy]] }.sum squares[[x,y,size]] = squares[[x,y,size-1]] + deltas.map {|(dx,dy)| grid[[x+dx, y+dy]] }.sum - grid[[x+size-1, y+size-1]]
squares[[x,y]] = power_level }
} }
}
p squares.max_by(&:last) puts "#{size}: #{squares.max_by(&:last)}"
squares.delete_if {|(_,_,s)| s < size }
end

Loading…
Cancel
Save