|
| 1 | +from aoc import * |
| 2 | +from statistics import median, mode |
| 3 | +from more_itertools import windowed, take, peekable, chunked, first_true, split_when, first_true |
| 4 | +from heapq import * |
| 5 | +# from functools import reduce, cache, cmp_to_key |
| 6 | +from functools import cache |
| 7 | +from math import ceil |
| 8 | +import re |
| 9 | +from blessed import BlessedList |
| 10 | +from copy import deepcopy |
| 11 | +import operator |
| 12 | +from string import ascii_letters, ascii_lowercase, ascii_uppercase, digits, hexdigits, whitespace |
| 13 | +from pprint import pprint as pp |
| 14 | +import bisect |
| 15 | +import math |
| 16 | +from itertools import pairwise, combinations |
| 17 | +import sys |
| 18 | +from collections import defaultdict, OrderedDict, deque, Counter |
| 19 | +# from ordered_set import OrderedSet |
| 20 | + |
| 21 | +S = [(-1, 0), (1, 0), (0, -1), (0, 1)] |
| 22 | + |
| 23 | + |
| 24 | +def main(input_file: str): |
| 25 | + m = load_map_dd(input_file) |
| 26 | + maxx = max(x for x, y in m) |
| 27 | + sizex = maxx + 1 |
| 28 | + sizey = maxy + 1 |
| 29 | + maxy = max(y for x, y in m) |
| 30 | + # pp(m) |
| 31 | + @cache |
| 32 | + def step(x, y): |
| 33 | + return {((x + dx), |
| 34 | + (y + dy)) for dx, dy in S |
| 35 | + if m[((x + dx) % (maxx + 1), |
| 36 | + (y + dy) % (maxy + 1))] in ['.', 'S']} |
| 37 | + |
| 38 | + places = {(x, y) for (x, y), c in m.items() if c == 'S'} |
| 39 | + c = Counter() |
| 40 | + c[next(iter(places))] = 1 |
| 41 | + steps = 26501365 |
| 42 | + size = steps * 2 + 1 |
| 43 | + suma = 0 |
| 44 | + for i in range(size): |
| 45 | + if i <= size // 2: |
| 46 | + # we are above or in the middle of the rhombus, the width is |
| 47 | + # increasing |
| 48 | + row = i % sizex |
| 49 | + width = i * 2 + 1 |
| 50 | + if width <= sizey: |
| 51 | + # take inner size of the rhomboid |
| 52 | + for col in range(sizey // 2 - i, width, 2): |
| 53 | + if m[i, col] == '.': |
| 54 | + suma += 1 |
| 55 | + else: |
| 56 | + # take outer size of the rhomboid |
| 57 | + full_widths = width // sizey |
| 58 | + remainder = width % sizey |
| 59 | + if remainder <= sizey // 2: |
| 60 | + # Count the edges |
| 61 | + # XXX.........XXX |
| 62 | + pass |
| 63 | + |
| 64 | + else: |
| 65 | + # Count the inside |
| 66 | + # XXXXXX... |
| 67 | + # ...XXXXXX |
| 68 | + # ^^^......counting just these |
| 69 | + pass |
| 70 | + pass |
| 71 | + else: |
| 72 | + # we are below rhombus, the width is decreasing |
| 73 | + pass |
| 74 | + for i in range(50): |
| 75 | + new_places = set() |
| 76 | + new_counter = Counter() |
| 77 | + for x, y in places: |
| 78 | + # steps_number = c[x, y] |
| 79 | + # c[x, y] = 0 |
| 80 | + new_steps = step(x, y) |
| 81 | + new_places.update(new_steps) |
| 82 | + # for nx, ny in new_steps: |
| 83 | + # new_counter[nx, ny] = steps_number |
| 84 | + places = new_places |
| 85 | + c = new_counter |
| 86 | + # pp(m) |
| 87 | + # for ri in range(maxx + 1): |
| 88 | + # for ci in range(maxy + 1): |
| 89 | + # if (ri, ci) in places: |
| 90 | + # print('O', end='') |
| 91 | + # else: |
| 92 | + # print(m[(ri, ci)], end='') |
| 93 | + # print() |
| 94 | + # pp(c) |
| 95 | + return sum(c.values()) |
| 96 | + |
| 97 | +DAY = 21 |
| 98 | +FILE_TEST = f"{DAY}_test.txt" |
| 99 | +FILE_EXP = f"{DAY}_expa.txt" |
| 100 | +FILE = f"{DAY}.txt" |
| 101 | +# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY) |
| 102 | +print(main(FILE_TEST)) |
| 103 | +# print(main(FILE)) |
0 commit comments