-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday11.py
65 lines (53 loc) · 1.95 KB
/
day11.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
# Description: Advent of Code - Day 11
class Universe:
def __init__(self):
self.lines = []
self.galaxies = []
def add(self, line):
self.lines.append(line)
def finish(self):
for y, line in enumerate(self.lines):
for x, char in enumerate(line):
if char == '#':
self.galaxies.append(Galaxy(x, y))
for y, line in enumerate(self.lines):
if '#' not in line:
for galaxy in self.galaxies:
if galaxy.original_y > y:
galaxy.expand_vertical()
for x in range(len(self.lines[0])):
if '#' not in [k[x] for k in self.lines]:
for galaxy in self.galaxies:
if galaxy.original_x > x:
galaxy.expand_horizontal()
def challenge(self, increment):
ret = 0
for galaxy in self.galaxies:
for other in self.galaxies:
if galaxy == other:
continue
ret += galaxy.distance(other, increment)
return int(ret/2)
class Galaxy:
def __init__(self, x, y):
self.original_x = x
self.original_y = y
self.expansions_x = 0
self.expansions_y = 0
def expand_vertical(self):
self.expansions_y += 1
def expand_horizontal(self):
self.expansions_x += 1
def distance(self, other, increment=1):
return abs((self.original_x + increment*self.expansions_x) - (other.original_x + increment*other.expansions_x)) +\
abs((self.original_y + increment*self.expansions_y) - (other.original_y + increment*other.expansions_y))
if __name__ == '__main__':
chal1 = 0
chal2 = 0
u = Universe()
with open('day11.input', 'r') as f:
for line in f.readlines():
u.add(line.strip())
u.finish()
print(f"Challenge 1: {u.challenge(1)}")
print(f"Challenge 2: {u.challenge(999999)}")