adventofcode

https://adventofcode.com/
Log | Files | Refs

puzzle3.py (1223B)


      1 idx = 1
      2 
      3 while idx ** 2 < 312051:
      4     idx += 2
      5 
      6 print idx - (idx / 2) + (idx ** 2 - (idx / 2) - 312051) - 1
      7 
      8 x, y, steps = 0, 0, 0
      9 x_dir, y_dir, length = 1, 1, 1
     10 vert = False
     11 next_tile = "null"
     12 spiral_dict = {"0 0" : 1, next_tile : 0}
     13 
     14 def key_check(key):
     15     return key if key in spiral_dict else "null"
     16 
     17 while spiral_dict[next_tile] < 312051:
     18     value = 0
     19     x = x + (int(not vert) * x_dir)
     20     y = y + (int(vert) * y_dir)
     21 
     22     value += spiral_dict[key_check(str(x + 1) + " " + str(y))]
     23     value += spiral_dict[key_check(str(x - 1) + " " + str(y))]
     24     value += spiral_dict[key_check(str(x) + " " + str(y + 1))]
     25     value += spiral_dict[key_check(str(x) + " " + str(y - 1))]
     26     value += spiral_dict[key_check(str(x + 1) + " " + str(y + 1))]
     27     value += spiral_dict[key_check(str(x + 1) + " " + str(y - 1))]
     28     value += spiral_dict[key_check(str(x - 1) + " " + str(y + 1))]
     29     value += spiral_dict[key_check(str(x - 1) + " " + str(y - 1))]
     30 
     31     next_tile = str(x) + " " + str(y)
     32     spiral_dict[next_tile] = value
     33     steps += 1
     34 
     35     if steps == length:
     36         if vert:
     37             length += 1
     38             x_dir *= -1
     39             y_dir *= -1
     40         vert = not vert
     41         steps = 0
     42 
     43 print spiral_dict[next_tile]