From fd278fdfa80dffa36bf60e8ffdf71700def02625 Mon Sep 17 00:00:00 2001 From: Alexander Munch-Hansen Date: Sun, 3 Dec 2017 14:52:42 +0100 Subject: [PATCH] Absolutely horrible solution --- spirallingdistance.rb | 92 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 78 insertions(+), 14 deletions(-) diff --git a/spirallingdistance.rb b/spirallingdistance.rb index d198059..7f9177c 100644 --- a/spirallingdistance.rb +++ b/spirallingdistance.rb @@ -3,22 +3,86 @@ ### -a = 325489 -b = a -while Math.sqrt(b) % 2 != 1 do - b += 1 -end -puts "Total: " + b.to_s -side_length = Math.sqrt(b).ceil -puts "Side length: " + side_length.to_s -diff = b - a -puts "Diff: " + diff.to_s +input = 325489 -steps_req = diff % (side_length - 1) -to_center = (side_length-1) / 2 -to_mid_of_row = ((side_length / 2) - steps_req).abs -solution = to_center + to_mid_of_row +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)