|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os, grid, graph, dot, assembly, re, itertools, copy, functools |
| 3 | +from collections import Counter, deque, defaultdict |
| 4 | + |
| 5 | +from compass import * |
| 6 | + |
| 7 | + |
| 8 | +# This functions come from https://github.com/mcpower/adventofcode - Thanks! |
| 9 | +def lmap(func, *iterables): |
| 10 | + return list(map(func, *iterables)) |
| 11 | + |
| 12 | + |
| 13 | +def ints(s: str): |
| 14 | + return lmap(int, re.findall(r"-?\d+", s)) # thanks mserrano! |
| 15 | + |
| 16 | + |
| 17 | +def positive_ints(s: str): |
| 18 | + return lmap(int, re.findall(r"\d+", s)) # thanks mserrano! |
| 19 | + |
| 20 | + |
| 21 | +def floats(s: str): |
| 22 | + return lmap(float, re.findall(r"-?\d+(?:\.\d+)?", s)) |
| 23 | + |
| 24 | + |
| 25 | +def positive_floats(s: str): |
| 26 | + return lmap(float, re.findall(r"\d+(?:\.\d+)?", s)) |
| 27 | + |
| 28 | + |
| 29 | +def words(s: str): |
| 30 | + return re.findall(r"[a-zA-Z]+", s) |
| 31 | + |
| 32 | + |
| 33 | +test_data = {} |
| 34 | + |
| 35 | +test = 1 |
| 36 | +test_data[test] = { |
| 37 | + "input": """v...>>.vv> |
| 38 | +.vv>>.vv.. |
| 39 | +>>.>v>...v |
| 40 | +>>v>>.>.v. |
| 41 | +v>v.vv.v.. |
| 42 | +>.>>..v... |
| 43 | +.vv..>.>v. |
| 44 | +v.v..>>v.v |
| 45 | +....v..v.>""", |
| 46 | + "expected": ["Unknown", "Unknown"], |
| 47 | +} |
| 48 | + |
| 49 | +test = "real" |
| 50 | +input_file = os.path.join( |
| 51 | + os.path.dirname(__file__), |
| 52 | + "Inputs", |
| 53 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 54 | +) |
| 55 | +test_data[test] = { |
| 56 | + "input": open(input_file, "r+").read(), |
| 57 | + "expected": ["Unknown", "Unknown"], |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +# -------------------------------- Control program execution ------------------------- # |
| 62 | + |
| 63 | +case_to_test = "real" |
| 64 | +part_to_test = 1 |
| 65 | + |
| 66 | +# -------------------------------- Initialize some variables ------------------------- # |
| 67 | + |
| 68 | +puzzle_input = test_data[case_to_test]["input"] |
| 69 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 70 | +puzzle_actual_result = "Unknown" |
| 71 | + |
| 72 | + |
| 73 | +# -------------------------------- Actual code execution ----------------------------- # |
| 74 | + |
| 75 | +# Conver integer to 36-character binary |
| 76 | +# str_value = "{0:>036b}".format(value) |
| 77 | +# Convert binary string to number |
| 78 | +# value = int(str_value, 2) |
| 79 | + |
| 80 | + |
| 81 | +@functools.lru_cache |
| 82 | +def new_position(position, direction): |
| 83 | + if direction == 1: |
| 84 | + return (position.real + 1) % width + 1j * position.imag |
| 85 | + if direction == -1j: |
| 86 | + if -position.imag == height - 1: |
| 87 | + return position.real |
| 88 | + else: |
| 89 | + return position.real + 1j * (position.imag - 1) |
| 90 | + |
| 91 | + |
| 92 | +if part_to_test == 1: |
| 93 | + area = grid.Grid() |
| 94 | + area.text_to_dots(puzzle_input) |
| 95 | + |
| 96 | + east_facing = [dot.position for dot in area.dots.values() if dot.terrain == ">"] |
| 97 | + south_facing = [dot.position for dot in area.dots.values() if dot.terrain == "v"] |
| 98 | + |
| 99 | + width, height = area.get_size() |
| 100 | + |
| 101 | + for generation in range(10 ** 6): |
| 102 | + # print('Generation', generation) |
| 103 | + |
| 104 | + new_area = grid.Grid() |
| 105 | + |
| 106 | + new_east_facing = set( |
| 107 | + new_position(position, 1) |
| 108 | + if new_position(position, 1) not in east_facing |
| 109 | + and new_position(position, 1) not in south_facing |
| 110 | + else position |
| 111 | + for position in east_facing |
| 112 | + ) |
| 113 | + |
| 114 | + new_south_facing = set( |
| 115 | + new_position(position, -1j) |
| 116 | + if new_position(position, -1j) not in south_facing |
| 117 | + and new_position(position, -1j) not in new_east_facing |
| 118 | + else position |
| 119 | + for position in south_facing |
| 120 | + ) |
| 121 | + |
| 122 | + if east_facing == new_east_facing: |
| 123 | + if south_facing == new_south_facing: |
| 124 | + break |
| 125 | + |
| 126 | + east_facing = new_east_facing |
| 127 | + south_facing = new_south_facing |
| 128 | + |
| 129 | + puzzle_actual_result = generation + 1 |
| 130 | + |
| 131 | + |
| 132 | +else: |
| 133 | + for string in puzzle_input.split("\n"): |
| 134 | + if string == "": |
| 135 | + continue |
| 136 | + |
| 137 | + |
| 138 | +# -------------------------------- Outputs / results --------------------------------- # |
| 139 | + |
| 140 | +print("Case :", case_to_test, "- Part", part_to_test) |
| 141 | +print("Expected result : " + str(puzzle_expected_result)) |
| 142 | +print("Actual result : " + str(puzzle_actual_result)) |
| 143 | +# Date created: 2021-12-25 08:15:28.182606 |
| 144 | +# Part 1: 2021-12-25 08:53:05 |
0 commit comments