89 lines
1.8 KiB
Ruby
89 lines
1.8 KiB
Ruby
###
|
|
# Part 1
|
|
###
|
|
|
|
|
|
input = 325489
|
|
|
|
side_length = Math.sqrt(input).ceil ## 571
|
|
num_at_level = side_length**2 ## 326041
|
|
diff = num_at_level - input ## 552
|
|
|
|
steps_req = diff % (side_length - 1) ## 552
|
|
to_center = (side_length-1) / 2 ## 285
|
|
to_mid_of_row = ((side_length / 2) - steps_req).abs ## 267
|
|
solution = to_center + to_mid_of_row ## 552
|
|
puts "Pls correct yes: " + solution.to_s
|
|
|
|
|
|
###
|
|
# Part 2
|
|
###
|
|
# Fuck it, bruteforcing
|
|
|
|
input = 325489
|
|
|
|
def look_around(x,y,spiral)
|
|
result = 0
|
|
(y-1...y+2).each do |y_val|
|
|
(x-1...x+2).each do |x_val|
|
|
result += spiral[[x_val,y_val]]
|
|
end
|
|
end
|
|
result
|
|
end
|
|
|
|
def increment_spiral(x,y,spiral)
|
|
spiral[[x,y]] = look_around(x,y,spiral)
|
|
end
|
|
|
|
def pls(input)
|
|
result = 0
|
|
spiral = Hash.new 0
|
|
spiral[[0,0]],i,j = 1,0,0
|
|
inf = Float::INFINITY
|
|
(1..inf).step(2) do |s|
|
|
(0...s).each do
|
|
i+=1
|
|
spiral[[i,j]] = look_around(i,j,spiral)
|
|
if spiral[[i,j]] > input then
|
|
result = spiral[[i,j]]
|
|
puts result
|
|
break
|
|
end
|
|
end
|
|
if result != 0 then break end
|
|
(0...s).each do
|
|
j-=1
|
|
spiral[[i,j]] = look_around(i,j,spiral)
|
|
if spiral[[i,j]] > input then
|
|
result = spiral[[i,j]]
|
|
puts result
|
|
break
|
|
end
|
|
end
|
|
if result != 0 then break end
|
|
(0...(s+1)).each do
|
|
i-=1
|
|
spiral[[i,j]] = look_around(i,j,spiral)
|
|
if spiral[[i,j]] > input then
|
|
result = spiral[[i,j]]
|
|
puts result
|
|
break
|
|
end
|
|
end
|
|
if result != 0 then break end
|
|
(0...(s+1)).each do
|
|
j+=1
|
|
spiral[[i,j]] = look_around(i,j,spiral)
|
|
if spiral[[i,j]] > input then
|
|
result = spiral[[i,j]]
|
|
puts result
|
|
break
|
|
end
|
|
end
|
|
if result != 0 then break end
|
|
end
|
|
end
|
|
pls(325489)
|