-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay05.py
More file actions
84 lines (62 loc) · 2.32 KB
/
Day05.py
File metadata and controls
84 lines (62 loc) · 2.32 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
import Utility
filePath = 'input/day05/part1.txt'
class Line:
def __init__(self, inputLine) -> None:
start, end = inputLine.split(' -> ')
self.startX = int(start.split(',')[1])
self.startY = int(start.split(',')[0])
self.endX = int(end.split(',')[1])
self.endY = int(end.split(',')[0])
def isHorizontal(self) -> bool:
return self.startY == self.endY
def isVertical(self) -> bool:
return self.startX == self.endX
def isDiagonal(self) -> bool:
return not (self.isHorizontal() or self.isVertical())
def __repr__(self) -> str:
return self.startX + ',' + self.startY + ' -> ' + self.endX + ',' + self.endY
class Field:
size = 1000
def __init__(self) -> None:
self.heights = [[0 for i in range(Field.size)]
for i in range(Field.size)]
def loadHorizontalAndVerticalLines(self, lines: list[Line]):
for line in lines:
if (not (line.isHorizontal() or line.isVertical())):
continue
self.loadLine(line)
def loadLines(self, lines: list[Line]):
for line in lines:
self.loadLine(line)
def loadLine(self, line: Line):
lengthOfLine = max(abs(line.endY - line.startY), abs(line.endX - line.startX))
xChange = (line.endX - line.startX) // lengthOfLine
yChange = (line.endY - line.startY) // lengthOfLine
for i in range(lengthOfLine + 1):
x = line.startX + i * xChange
y = line.startY + i * yChange
self.heights[x][y] += 1
def getOverlapPointsCount(self) -> int:
sum = 0
for x in range(Field.size):
for y in range(Field.size):
if (self.heights[x][y] > 1):
sum += 1
return sum
def solvePart1():
inputLines = Utility.getLinesFromFile(filePath)
lines = list(map(Line, inputLines))
field = Field()
field.loadHorizontalAndVerticalLines(lines)
print('Solution to part1:')
print(field.getOverlapPointsCount())
def solvePart2():
inputLines = Utility.getLinesFromFile(filePath)
lines = list(map(Line, inputLines))
field = Field()
field.loadLines(lines)
print('Solution to part2:')
print(field.getOverlapPointsCount())
if(__name__ == '__main__'):
solvePart1()
solvePart2()