|
| 1 | +# -------------------------------- Input data -------------------------------- # |
| 2 | +import os, math |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = {"input": 17, |
| 8 | + "expected": ['Unknown', 'Unknown'], |
| 9 | + } |
| 10 | + |
| 11 | +test += 1 |
| 12 | +test_data[test] = {"input": """""", |
| 13 | + "expected": ['Unknown', 'Unknown'], |
| 14 | + } |
| 15 | + |
| 16 | +test = 'real' |
| 17 | +test_data[test] = {"input": 312051, |
| 18 | + "expected": ['430', '312453'], |
| 19 | + } |
| 20 | + |
| 21 | +# -------------------------------- Control program execution -------------------------------- # |
| 22 | + |
| 23 | +case_to_test = 'real' |
| 24 | +part_to_test = 2 |
| 25 | +verbose_level = 1 |
| 26 | + |
| 27 | +# -------------------------------- Initialize some variables -------------------------------- # |
| 28 | + |
| 29 | +puzzle_input = test_data[case_to_test]['input'] |
| 30 | +puzzle_expected_result = test_data[case_to_test]['expected'][part_to_test-1] |
| 31 | +puzzle_actual_result = 'Unknown' |
| 32 | + |
| 33 | + |
| 34 | +# -------------------------------- Actual code execution -------------------------------- # |
| 35 | + |
| 36 | +if part_to_test == 1: |
| 37 | + square_size = int(math.sqrt(puzzle_input)) |
| 38 | + if square_size % 2 == 0: |
| 39 | + square_size += 1 |
| 40 | + else: |
| 41 | + square_size += 2 |
| 42 | + |
| 43 | + |
| 44 | + distance_from_square = (square_size ** 2 - puzzle_input) % (square_size-1) |
| 45 | + |
| 46 | + if distance_from_square <= square_size // 2: |
| 47 | + distance_from_square = square_size // 2 - distance_from_square |
| 48 | + else: |
| 49 | + distance_from_square -= square_size // 2 |
| 50 | + |
| 51 | + puzzle_actual_result = (square_size - 1) // 2 + distance_from_square |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +else: |
| 56 | + vals = {} |
| 57 | + direction = (1, 0) |
| 58 | + current = (1,0) |
| 59 | + vals[(0,0)] = 1 |
| 60 | + |
| 61 | + max_square = 1000 |
| 62 | + |
| 63 | + corner_SE = {x**2+1: (0, -1) for x in range(1, max_square) if x % 2 == 1} |
| 64 | + corner_SW = {x**2 - (x-1): (1, 0) for x in range(1, max_square) if x % 2 == 1} |
| 65 | + corner_NW = {x**2 - (x-1)*2: (0, 1) for x in range(2, max_square) if x % 2 == 1} |
| 66 | + corner_NE = {x**2 - (x-1)*3: (-1, 0) for x in range(2, max_square) if x % 2 == 1} |
| 67 | + corners = corner_SE.copy() |
| 68 | + corners.update(corner_SW) |
| 69 | + corners.update(corner_NW) |
| 70 | + corners.update(corner_NE) |
| 71 | + |
| 72 | + for i in range (2, max_square): |
| 73 | + value = 0 |
| 74 | + |
| 75 | + for neighbor in [(x, y) for x in (-1, 0, 1) for y in (-1, 0, 1) if not((x, y) == (0,0))]: |
| 76 | + x, y = (current[0] + neighbor[0], current[1] + neighbor[1]) |
| 77 | + if (x, y) in vals: |
| 78 | + value += vals[(x, y)] |
| 79 | + |
| 80 | + vals[current] = value |
| 81 | + |
| 82 | + # In which direction are we going? |
| 83 | + if i in corners: |
| 84 | + direction = corners[i] |
| 85 | + |
| 86 | + current = (current[0] + direction[0], current[1] + direction[1]) |
| 87 | + |
| 88 | + if value > puzzle_input: |
| 89 | + puzzle_actual_result = value |
| 90 | + break |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | +# -------------------------------- Outputs / results -------------------------------- # |
| 95 | + |
| 96 | +if verbose_level >= 3: |
| 97 | + print ('Input : ' + puzzle_input) |
| 98 | +print ('Expected result : ' + str(puzzle_expected_result)) |
| 99 | +print ('Actual result : ' + str(puzzle_actual_result)) |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | + |
0 commit comments