advent-of-code

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit fe379ca619c8c4d9c67508abe538037d2ae1a7e2
parent e5ae8a74f342107890cc21839ad08691e602f00e
Author: mpizzzle <michael.770211@gmail.com>
Date:   Sun, 10 Dec 2017 00:37:23 +0000

puzzle 3 complete

Diffstat:
Mpuzzle3.py | 38+++++++++++++++++++++++++++++++++-----
Mpuzzle7.py | 16++++++++--------
2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/puzzle3.py b/puzzle3.py @@ -5,11 +5,39 @@ while idx ** 2 < 312051: print idx - (idx / 2) + (idx ** 2 - (idx / 2) - 312051) - 1 -#for int i = 0 +x, y, steps = 0, 0, 0 +x_dir, y_dir, length = 1, 1, 1 +vert = False +next_tile = "null" +spiral_dict = {"0 0" : 1, next_tile : 0} -#layer = 3 -#side = 3 % 4 -#idx = 23 -#my_array = [1, 1, 2, 4, 5, 10, 11, 23, 25, 26, 54, 57, 59, 122, 133, 142, 147, 304, 330, 351, 362, 747, 806] +def key_check(key): + return key if key in spiral_dict else "null" +while spiral_dict[next_tile] < 312051: + value = 0 + x = x + (int(not vert) * x_dir) + y = y + (int(vert) * y_dir) + value += spiral_dict[key_check(str(x + 1) + " " + str(y))] + value += spiral_dict[key_check(str(x - 1) + " " + str(y))] + value += spiral_dict[key_check(str(x) + " " + str(y + 1))] + value += spiral_dict[key_check(str(x) + " " + str(y - 1))] + value += spiral_dict[key_check(str(x + 1) + " " + str(y + 1))] + value += spiral_dict[key_check(str(x + 1) + " " + str(y - 1))] + value += spiral_dict[key_check(str(x - 1) + " " + str(y + 1))] + value += spiral_dict[key_check(str(x - 1) + " " + str(y - 1))] + + next_tile = str(x) + " " + str(y) + spiral_dict[next_tile] = value + steps += 1 + + if steps == length: + if vert: + length += 1 + x_dir *= -1 + y_dir *= -1 + vert = not vert + steps = 0 + +print spiral_dict[next_tile] diff --git a/puzzle7.py b/puzzle7.py @@ -45,13 +45,13 @@ print candidate def traverse_totals(node): if node.children == None: return node.weight - else: - weights = [traverse_totals(node.children[i]) for i in range(len(node.children))] - if len(Set(weights)) != 1: - for idx, weight in enumerate(weights): - if weights.count(weight) == 1: - s = list(Set(weights)) - print node.children[idx].weight + s[0] - s[1] - return node.weight + sum(weights) + + weights = [traverse_totals(node.children[i]) for i in range(len(node.children))] + if len(Set(weights)) != 1: + for idx, weight in enumerate(weights): + if weights.count(weight) == 1: + s = list(Set(weights)) + print node.children[idx].weight + s[0] - s[1] + return node.weight + sum(weights) traverse_totals(my_dict[candidate])