|
| 1 | +""" |
| 2 | +""" |
| 3 | + |
| 4 | +from utils import Solution |
| 5 | +from typing import Any |
| 6 | +import numpy as np |
| 7 | + |
| 8 | + |
| 9 | +class DaySolution(Solution): |
| 10 | + def __init__(self, day: int = 25, year: int = 2021) -> None: |
| 11 | + super().__init__(day, year) |
| 12 | + |
| 13 | + def _parse_data(self, input_data: str) -> Any: |
| 14 | + """ |
| 15 | + """ |
| 16 | + c_to_int = {'.': 0, '>': 1, 'v': 2} |
| 17 | + seac_map = np.array([[c_to_int[x] for x in line] for line in input_data.strip().split("\n")]) |
| 18 | + return seac_map |
| 19 | + |
| 20 | + def _solve_part1(self, parsed_data: Any) -> Any: |
| 21 | + """ |
| 22 | + """ |
| 23 | + nr_turns = 0 |
| 24 | + cur_map = parsed_data.copy() |
| 25 | + map_shape = cur_map.shape |
| 26 | + while True: |
| 27 | + new_map = cur_map.copy() |
| 28 | + nr_turns += 1 |
| 29 | + e_c_row, e_c_col = np.where(cur_map == 1) |
| 30 | + to_move = [] |
| 31 | + for p in zip(e_c_row, e_c_col): |
| 32 | + to_col = (p[1] + 1) % map_shape[1] |
| 33 | + if cur_map[p[0], to_col] == 0: |
| 34 | + to_move.append([p, to_col]) |
| 35 | + for m_p in to_move: |
| 36 | + new_map[m_p[0][0], m_p[1]] = 1 |
| 37 | + new_map[m_p[0][0], m_p[0][1]] = 0 |
| 38 | + s_c_row, s_c_col = np.where(cur_map == 2) |
| 39 | + to_move = [] |
| 40 | + for p in zip(s_c_row, s_c_col): |
| 41 | + to_row = (p[0] + 1) % map_shape[0] |
| 42 | + if new_map[to_row, p[1]] == 0: |
| 43 | + to_move.append([p, to_row]) |
| 44 | + for m_p in to_move: |
| 45 | + new_map[m_p[1], m_p[0][1]] = 2 |
| 46 | + new_map[m_p[0][0], m_p[0][1]] = 0 |
| 47 | + if (new_map == cur_map).all(): |
| 48 | + break |
| 49 | + else: |
| 50 | + cur_map = new_map |
| 51 | + return nr_turns |
| 52 | + |
| 53 | + def _solve_part2(self, parsed_data: Any) -> Any: |
| 54 | + """ |
| 55 | + """ |
| 56 | + return 1 |
0 commit comments