-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem13.py
76 lines (63 loc) · 2.06 KB
/
problem13.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
ADVENT OF CODE 2021
Contestant: Kevin Wood
Problem: 13
"""
import argparse
import re
from typing import List, Tuple
def solve(lines: List[str], part: int) -> int:
width = 0
height = 0
coords = [] # type: List[Tuple[int, int]]
for i, line in enumerate(lines):
if not line:
break
x, y = line.strip().split(",")
coords.append((int(x), int(y)))
if int(x) >= width:
width = int(x) + 1
if int(y) >= height:
height = int(y) + 1
index_of_folds = i + 1
for i in range(index_of_folds, len(lines)):
parser = re.match("fold along (x|y)=(\d+)", lines[i])
if not parser:
raise Exception("Bad input")
axis = parser.groups()[0]
fold_over_val = int(parser.groups()[1])
if axis == "x":
width = fold_over_val
for ci, (cx, cy) in enumerate(coords):
if cx > fold_over_val:
coords[ci] = (fold_over_val - (cx - fold_over_val), cy)
if axis == "y":
height = fold_over_val
for ci, (cx, cy) in enumerate(coords):
if cy > fold_over_val:
coords[ci] = (cx, fold_over_val - (cy - fold_over_val))
if part == 1:
break
uniq_coords = set()
for cx, cy in coords:
uniq_coords.add((cx, cy))
for cy in range(height):
line = ""
for cx in range(width):
if (cx, cy) in uniq_coords:
line += "#"
else:
line += "."
print(line)
return len(uniq_coords)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Advent of Code 2021, problem 13")
parser.add_argument(
"-i", "--input_file", help="path to input file", default="input/problem13.txt"
)
parser.add_argument("part", help="part (1|2)", type=int)
args = parser.parse_args()
with open(args.input_file, "r") as file:
lines = [line.strip() for line in file.readlines()]
output = solve(lines, part=args.part)
print(output)