summary refs log tree commit diff stats
path: root/day13.py
blob: a1f4058d0d8bd77fce20eb58af52e845db112515 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python

import numpy as np
import re

split_pattern = re.compile('(x|y)=([\d]*)')
xMax = yMax = 0
with open('day13.txt') as data:
    coords = []
    for line in data:
        if line == '\n':
            break
        else:
            x, y = map(int, line.strip().split(','))
            xMax = max(xMax, x)
            yMax = max(yMax, y)
            coords += [(x, y)]
    splits = []
    for line in data:
        splits += [split_pattern.search(line.strip()).group(1, 2)]

thermal = np.zeros((xMax+1, yMax+1)).astype(bool)
for x, y in coords:
    thermal[x, y] = True

# part 1
def fold_map(split):
    # folds always fold the map in half
    axis, val = split[0], int(split[1])
    if axis == 'x':
        folded = np.flip(thermal[val+1:], 0)
        length = folded.shape[0]
        thermal[val-length:val] = thermal[val-length:val] | folded
        return thermal[:val]
    else:
        folded = np.flip(thermal[:, val+1:], 1)
        length = folded.shape[1]
        thermal[:, val-length:val] = thermal[:, val-length:val] | folded
        return thermal[:, :val]
# part 1
print(np.count_nonzero(fold_map(splits[0])))

# part 2
for split in splits:
    thermal = fold_map(split)
thermal_str = thermal.astype(str)
thermal_str[thermal] = "*"
thermal_str[~thermal] = " "
for line in thermal_str.T:
    print("".join(line))