diff options
Diffstat (limited to 'day13.py')
-rw-r--r-- | day13.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/day13.py b/day13.py new file mode 100644 index 0000000..a1f4058 --- /dev/null +++ b/day13.py @@ -0,0 +1,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)) |