-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path13.py
90 lines (70 loc) · 2.08 KB
/
13.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
INPUT_FILE = f"input/{__file__.split('.')[0].rstrip('b')}"
def getInput():
maxX = maxY = 0
with open(INPUT_FILE, 'r') as f:
inp = f.read().splitlines()
folds = []
matrix = []
inFolds = False
for x in inp:
if x == '':
break
split = list(map(int, x.split(',')))
maxX = maxX if maxX > split[0] else split[0]
maxY = maxY if maxY > split[1] else split[1]
maxX += 1
maxY += 1
for i in range(maxY):
matrix.append([])
matrix[i] = ['.'] * maxX
for x in inp:
if x == '':
inFolds = True
continue
if inFolds:
folds.append(x.split('fold along ')[1].split('='))
else:
split = list(map(int, x.split(',')))
matrix[split[1]][split[0]] = '#'
return [matrix, folds]
def partOne():
matrix, folds = getInput()
matrix = doFold(*folds[0], matrix)
visible = 0
for i in matrix:
visible += i.count('#')
return visible
def doFold(direction, num, matrix):
num = int(num)
if direction == 'x':
newMatrix = []
for i, j in enumerate(matrix):
newMatrix.append([])
newMatrix[i] = matrix[i][:num]
else:
newMatrix = matrix[:num]
for idx, i in enumerate(matrix):
if direction == 'y' and idx < num:
continue
for idx2, j in enumerate(i):
if direction == 'x' and idx2 < num:
continue
x = idx2 if direction == 'y' else num - (idx2 - num)
y = idx if direction == 'x' else num - (idx - num)
if j == '#':
newMatrix[y][x] = '#'
return newMatrix
def partTwo():
matrix, folds = getInput()
for fold in folds:
matrix = doFold(*fold, matrix)
pp(matrix)
return 0
def pp(j):
for i in j:
print(''.join(i))
if __name__ == "__main__":
one = partOne()
two = partTwo()
print(f"Part one: {one}")
print(f"Part two: {two}")