|
| 1 | +# -------------------------------- Input data ---------------------------------------- # |
| 2 | +import os |
| 3 | + |
| 4 | +test_data = {} |
| 5 | + |
| 6 | +test = 1 |
| 7 | +test_data[test] = { |
| 8 | + "input": """x=495, y=2..7 |
| 9 | +y=7, x=495..501 |
| 10 | +x=501, y=3..7 |
| 11 | +x=498, y=2..4 |
| 12 | +x=506, y=1..2 |
| 13 | +x=498, y=10..13 |
| 14 | +x=504, y=10..13 |
| 15 | +y=13, x=498..504""", |
| 16 | + "expected": ["Unknown", "Unknown"], |
| 17 | +} |
| 18 | + |
| 19 | +test += 1 |
| 20 | +test_data[test] = { |
| 21 | + "input": """x=496, y=2..8 |
| 22 | +x=499, y=3..5 |
| 23 | +x=501, y=3..5 |
| 24 | +y=5, x=499..501 |
| 25 | +x=505, y=2..8""", |
| 26 | + "expected": ["Unknown", "Unknown"], |
| 27 | +} |
| 28 | + |
| 29 | +test += 1 |
| 30 | +test_data[test] = { |
| 31 | + "input": """x=491, y=2..8 |
| 32 | +x=497, y=4..8 |
| 33 | +x=504, y=3..8 |
| 34 | +y=8, x=497..504 |
| 35 | +x=508, y=2..8""", |
| 36 | + "expected": ["Unknown", "Unknown"], |
| 37 | +} |
| 38 | + |
| 39 | +test = "real" |
| 40 | +input_file = os.path.join( |
| 41 | + os.path.dirname(__file__), |
| 42 | + "Inputs", |
| 43 | + os.path.basename(__file__).replace(".py", ".txt"), |
| 44 | +) |
| 45 | +test_data[test] = { |
| 46 | + "input": open(input_file, "r+").read().strip(), |
| 47 | + "expected": ["39877", "33291"], |
| 48 | +} |
| 49 | + |
| 50 | +# -------------------------------- Control program execution ------------------------- # |
| 51 | + |
| 52 | +case_to_test = "real" |
| 53 | +part_to_test = 2 |
| 54 | + |
| 55 | +# -------------------------------- Initialize some variables ------------------------- # |
| 56 | + |
| 57 | +puzzle_input = test_data[case_to_test]["input"] |
| 58 | +puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1] |
| 59 | +puzzle_actual_result = "Unknown" |
| 60 | + |
| 61 | + |
| 62 | +# -------------------------------- Actual code execution ----------------------------- # |
| 63 | + |
| 64 | + |
| 65 | +walls = [] |
| 66 | +min_y, max_y = 0, -(10 ** 6) |
| 67 | +min_x, max_x = 500, 500 |
| 68 | + |
| 69 | +for string in puzzle_input.split("\n"): |
| 70 | + a, b = string.split(", ") |
| 71 | + dim1, val1 = a.split("=") |
| 72 | + val1 = int(val1) |
| 73 | + |
| 74 | + dim2, val2 = b.split("=") |
| 75 | + val2 = val2.split("..") |
| 76 | + val2, val3 = int(val2[0]), int(val2[1]) |
| 77 | + |
| 78 | + if dim1 == "x": |
| 79 | + min_y = min(min_y, -val3) |
| 80 | + max_y = max(max_y, -val2) |
| 81 | + min_x = min(min_x, val1) |
| 82 | + max_x = max(max_x, val1) |
| 83 | + else: |
| 84 | + min_y = min(min_y, -val1) |
| 85 | + max_y = max(max_y, -val1) |
| 86 | + min_x = min(min_x, val2) |
| 87 | + max_x = max(max_x, val3) |
| 88 | + |
| 89 | + for spot in range(val2, val3 + 1): |
| 90 | + if dim1 == "x": |
| 91 | + dot = val1 - spot * 1j |
| 92 | + else: |
| 93 | + dot = spot - val1 * 1j |
| 94 | + walls.append(dot) |
| 95 | + |
| 96 | +walls = set(walls) |
| 97 | + |
| 98 | +current_position = 500 |
| 99 | +wet_positions = set() |
| 100 | +pools = set() |
| 101 | +flowing = [current_position] |
| 102 | +settled = set() |
| 103 | + |
| 104 | +i = 0 |
| 105 | +while flowing: |
| 106 | + current_position = flowing.pop() |
| 107 | + # print ('--------------') |
| 108 | + # print ('now', current_position, current_position - 1j not in walls, current_position - 1j not in pools) |
| 109 | + # print ('pools', pools) |
| 110 | + |
| 111 | + if current_position.imag <= min_y: |
| 112 | + settled.add(current_position) |
| 113 | + position = current_position + 1j |
| 114 | + |
| 115 | + while position in flowing: |
| 116 | + settled.add(position) |
| 117 | + flowing.remove(position) |
| 118 | + position += 1j |
| 119 | + continue |
| 120 | + |
| 121 | + if current_position - 1j in settled: |
| 122 | + settled.add(current_position) |
| 123 | + continue |
| 124 | + if current_position - 1j not in walls and current_position - 1j not in pools: |
| 125 | + flowing.append(current_position) |
| 126 | + flowing.append(current_position - 1j) |
| 127 | + current_position -= 1j |
| 128 | + if current_position.imag >= min_y and current_position.imag <= max_y: |
| 129 | + wet_positions.add(current_position) |
| 130 | + else: |
| 131 | + |
| 132 | + pooling = True |
| 133 | + settling = False |
| 134 | + pool = set([current_position]) |
| 135 | + # fill horizontally |
| 136 | + |
| 137 | + for direction in [-1, 1]: |
| 138 | + position = current_position |
| 139 | + while True: |
| 140 | + # Extend to the right |
| 141 | + position += direction |
| 142 | + if position in walls: |
| 143 | + break |
| 144 | + elif position in settled: |
| 145 | + settling = True |
| 146 | + break |
| 147 | + else: |
| 148 | + wet_positions.add(position) |
| 149 | + pool.add(position) |
| 150 | + if position - 1j not in walls and position - 1j not in pools: |
| 151 | + pooling = False |
| 152 | + flowing.append(position) |
| 153 | + break |
| 154 | + |
| 155 | + if settling: |
| 156 | + settled = settled.union(pool) |
| 157 | + elif pooling: |
| 158 | + pools = pools.union(pool) |
| 159 | + |
| 160 | + # print ('pools', pools) |
| 161 | + # print ('flowing', flowing) |
| 162 | + |
| 163 | + # This limit is totally arbitrary |
| 164 | + if i == 10 ** 4: |
| 165 | + print("stop") |
| 166 | + break |
| 167 | + i += 1 |
| 168 | + |
| 169 | +print("step", i) |
| 170 | +for y in range(max_y + 1, min_y - 1, -1): |
| 171 | + for x in range(min_x - 2, max_x + 3): |
| 172 | + if x + y * 1j in pools: |
| 173 | + print("~", end="") |
| 174 | + elif x + y * 1j in settled: |
| 175 | + print("S", end="") |
| 176 | + elif x + y * 1j in flowing: |
| 177 | + print("F", end="") |
| 178 | + elif x + y * 1j in pools: |
| 179 | + print("~", end="") |
| 180 | + elif x + y * 1j in wet_positions: |
| 181 | + print("|", end="") |
| 182 | + elif x + y * 1j in walls: |
| 183 | + print("#", end="") |
| 184 | + else: |
| 185 | + print(".", end="") |
| 186 | + print("") |
| 187 | + |
| 188 | + |
| 189 | +if part_to_test == 1: |
| 190 | + puzzle_actual_result = len(wet_positions) |
| 191 | +else: |
| 192 | + puzzle_actual_result = len(pools) |
| 193 | +# 33556 too high |
| 194 | + |
| 195 | + |
| 196 | +# -------------------------------- Outputs / results --------------------------------- # |
| 197 | + |
| 198 | +print("Expected result : " + str(puzzle_expected_result)) |
| 199 | +print("Actual result : " + str(puzzle_actual_result)) |
0 commit comments