-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday8.py
More file actions
88 lines (74 loc) · 2.71 KB
/
day8.py
File metadata and controls
88 lines (74 loc) · 2.71 KB
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
l1 = []
with open("d8.txt", "r") as fin:
lines = fin.readlines()
for x in lines:
l1.append(list(x.strip()))
# part 1
antennas = {}
for i in range(len(l1)):
for j in range(len(l1[i])):
if l1[i][j] != ".":
if (l1[i][j] in antennas):
antennas[l1[i][j]].append((i, j))
else:
antennas[l1[i][j]] = [(i, j)]
c = 0
for antenna in antennas:
positions = antennas[antenna]
for i in range(len(positions)):
for j in range(i+1, len(positions)):
yDiff = positions[j][0] - positions[i][0]
xDiff = positions[j][1] - positions[i][1]
yPos1 = positions[i][0] - yDiff
xPos1 = positions[i][1] - xDiff
yPos2 = positions[j][0] + yDiff
xPos2 = positions[j][1] + xDiff
# upper #
if (yPos1 >= 0 and yPos1 < len(l1)):
if (xPos1 >= 0 and xPos1 < len(l1[i])):
if (l1[yPos1][xPos1] != "#"):
c += 1
l1[yPos1][xPos1] = "#"
# lower #
if (yPos2 >= 0 and yPos2 < len(l1)):
if (xPos2 >= 0 and xPos2 < len(l1[i])):
if (l1[yPos2][xPos2] != "#"):
c += 1
l1[yPos2][xPos2] = "#"
print(c)
# part 2
for antenna in antennas:
positions = antennas[antenna]
for i in range(len(positions)):
for j in range(i+1, len(positions)):
yDiff = positions[j][0] - positions[i][0]
xDiff = positions[j][1] - positions[i][1]
# extend
for k in range(10000): # arb limit
yPos1 = positions[i][0] - k * yDiff
xPos1 = positions[i][1] - k * xDiff
# upper #
if (yPos1 >= 0 and yPos1 < len(l1)):
if (xPos1 >= 0 and xPos1 < len(l1[i])):
if (l1[yPos1][xPos1] != "#"):
c += 1
l1[yPos1][xPos1] = "#"
else:
break
else:
break
# extend
for k in range(10000): # arb limit
yPos2 = positions[j][0] + k * yDiff
xPos2 = positions[j][1] + k * xDiff
# lower #
if (yPos2 >= 0 and yPos2 < len(l1)):
if (xPos2 >= 0 and xPos2 < len(l1[i])):
if (l1[yPos2][xPos2] != "#"):
c += 1
l1[yPos2][xPos2] = "#"
else:
break
else:
break
print(c)