Skip to content

Commit bfe30de

Browse files
committed
More solutions
1 parent f719cb2 commit bfe30de

8 files changed

Lines changed: 343 additions & 26 deletions

File tree

aoc/day11/part2.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
11
from typing import List
22
from dataclasses import dataclass, astuple
3-
from aoc.intcode import Emulator
4-
5-
6-
@dataclass
7-
class Vec:
8-
x: int
9-
y: int
10-
11-
def __hash__(self):
12-
return hash(astuple(self))
13-
14-
def __add__(self, other: 'Vec') -> 'Vec':
15-
return Vec(self.x + other.x, self.y + other.y)
163

17-
18-
dirs = [
19-
Vec(0, 1),
20-
Vec(1, 0),
21-
Vec(0, -1),
22-
Vec(-1, 0)
23-
]
4+
from aoc.intcode import Emulator
5+
from aoc.day11.part1 import Vec, dirs, parse_input
246

257

268
def solution(program: List[int]) -> str:
@@ -65,10 +47,6 @@ def solution(program: List[int]) -> str:
6547
return ret
6648

6749

68-
def parse_input(input: str) -> List[int]:
69-
return map(int, input.strip().split(','))
70-
71-
7250
def main():
7351
with open('inputs/day11.txt') as fd:
7452
print(solution(parse_input(fd.read())))

aoc/day12/part1.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from typing import List
2+
from itertools import combinations
3+
from dataclasses import dataclass, astuple
4+
5+
6+
@dataclass
7+
class Vec:
8+
x: int
9+
y: int
10+
z: int
11+
12+
def __hash__(self):
13+
return hash(astuple(self))
14+
15+
def __sub__(self, other: 'Vec') -> 'Vec':
16+
return Vec(self.x - other.x, self.y - other.y, self.z - other.z)
17+
18+
def __add__(self, other: 'Vec') -> 'Vec':
19+
return Vec(self.x + other.x, self.y + other.y, self.z + other.z)
20+
21+
def resize(self) -> 'Vec':
22+
return Vec(
23+
(int(self.x / abs(self.x))) if self.x else 0,
24+
(int(self.y / abs(self.y))) if self.y else 0,
25+
(int(self.z / abs(self.z))) if self.z else 0,
26+
)
27+
28+
def energy(self):
29+
return sum(map(abs, astuple(self)))
30+
31+
32+
@dataclass
33+
class Moon:
34+
pos: Vec
35+
vel: Vec
36+
37+
def __hash__(self):
38+
return hash(astuple(self.pos) + astuple(self.vel))
39+
40+
def apply_gravity(self, other: 'Moon'):
41+
self.vel += (other.pos - self.pos).resize()
42+
43+
def apply_velocity(self) -> None:
44+
self.pos += self.vel
45+
46+
def energy(self) -> int:
47+
return self.pos.energy() * self.vel.energy()
48+
49+
def step(moons: List[Moon]):
50+
for a, b in combinations(moons, 2):
51+
a.apply_gravity(b)
52+
b.apply_gravity(a)
53+
54+
for moon in moons:
55+
moon.apply_velocity()
56+
57+
58+
def solution(moons: List[Moon]) -> int:
59+
for _ in range(1000):
60+
step(moons)
61+
62+
return sum(moon.energy() for moon in moons)
63+
64+
65+
def parse(input: str) -> List[Moon]:
66+
ret = []
67+
for line in input.strip().split('\n'):
68+
vs = [int(v.split('=')[1]) for v in line[1:-1].split(',')]
69+
ret.append(Moon(Vec(*vs), Vec(0, 0, 0)))
70+
return ret
71+
72+
73+
def main():
74+
with open('inputs/day12.txt') as fd:
75+
print(solution(parse(fd.read())))

aoc/day12/part2.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import List
2+
from itertools import combinations
3+
from dataclasses import dataclass, astuple
4+
from copy import deepcopy
5+
from aoc.day12.part1 import Moon, parse, step
6+
7+
8+
def check_state(set1, set2):
9+
for a, b in zip(set1, set2):
10+
if a != b:
11+
return False
12+
return True
13+
14+
def solution(moons: List[Moon]) -> int:
15+
state = deepcopy(moons)
16+
steps = 0
17+
while True:
18+
step(moons)
19+
steps += 1
20+
if steps % 10000 == 0:
21+
print(steps)
22+
if check_state(state, moons):
23+
return steps
24+
25+
def main():
26+
with open('inputs/day12.txt') as fd:
27+
print(solution(parse(fd.read())))

aoc/day13/part1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import List
2+
3+
from aoc.intcode import Emulator
4+
5+
6+
7+
def solution(input: List[int]) -> int:
8+
e = Emulator(input)
9+
e.run()
10+
blocks = set()
11+
12+
while e.output:
13+
x = e.output.popleft()
14+
y = e.output.popleft()
15+
tid = e.output.popleft()
16+
if tid == 2:
17+
blocks.add((x,y))
18+
19+
20+
return len(blocks)
21+
22+
23+
def parse(input: str) -> List[int]:
24+
return list(map(int, input.strip().split(',')))
25+
26+
def main():
27+
with open('inputs/day13.txt') as fd:
28+
print(solution(parse(fd.read())))

aoc/day13/part2.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from typing import List
2+
3+
from aoc.intcode import Emulator
4+
5+
6+
7+
def solution(input: List[int]) -> int:
8+
input[0] = 2
9+
e = Emulator(input)
10+
11+
score = 0
12+
paddle_x = None
13+
ball_x = None
14+
15+
while not e.stopped:
16+
e.run()
17+
if len(e.output) >= 3:
18+
while len(e.output) > 2:
19+
x = e.output.popleft()
20+
y = e.output.popleft()
21+
tid = e.output.popleft()
22+
if (x, y) == (-1, 0):
23+
score = tid
24+
continue
25+
26+
if tid == 3:
27+
paddle_x = x
28+
29+
elif tid == 4:
30+
ball_x = x
31+
32+
else:
33+
if ball_x > paddle_x:
34+
e.input.append(1)
35+
elif ball_x < paddle_x:
36+
e.input.append(-1)
37+
else:
38+
e.input.append(0)
39+
40+
return score
41+
42+
43+
def parse(input: str) -> List[int]:
44+
return list(map(int, input.strip().split(',')))
45+
46+
def main():
47+
with open('inputs/day13.txt') as fd:
48+
print(solution(parse(fd.read())))

aoc/day8/part2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ def solution(input: str, size=(25, 6)) -> str:
2222
def main():
2323
with open('inputs/day8.txt') as fd:
2424
ret = solution(fd.read().strip())
25-
ret = ret.replace('2', ' ').replace('0', '.').replace('1', 'X')
25+
ret = ret.replace('2', ' ').replace('0', '.').replace('1', '#')
2626
print(ret)

solve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
days = sys.argv[1:]
66
if not days:
7-
days = ['day{}'.format(i) for i in range(1, 12)]
7+
days = ['day{}'.format(i) for i in range(1, 14)]
88

99
for day in days:
1010
print('#', day)

tests/test_day12.py

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
from aoc.day12 import part1
2+
3+
4+
def check_moons(moons, expected):
5+
cmp = ""
6+
for moon in moons:
7+
cmp += "pos=<x={:3d}, y={:3d}, z={:3d}>, vel=<x={:3d}, y={:3d}, z={:3d}>\n".format(
8+
moon.pos.x,
9+
moon.pos.y,
10+
moon.pos.z,
11+
moon.vel.x,
12+
moon.vel.y,
13+
moon.vel.z
14+
)
15+
16+
assert cmp.strip() == expected.strip()
17+
18+
19+
def test_part1():
20+
i = """
21+
<x=-8, y=-10, z=0>
22+
<x=5, y=5, z=10>
23+
<x=2, y=-7, z=3>
24+
<x=9, y=-8, z=-3>
25+
"""
26+
27+
moons = part1.parse(i)
28+
assert len(moons) == 4
29+
30+
t1 = """
31+
pos=<x= -8, y=-10, z= 0>, vel=<x= 0, y= 0, z= 0>
32+
pos=<x= 5, y= 5, z= 10>, vel=<x= 0, y= 0, z= 0>
33+
pos=<x= 2, y= -7, z= 3>, vel=<x= 0, y= 0, z= 0>
34+
pos=<x= 9, y= -8, z= -3>, vel=<x= 0, y= 0, z= 0>
35+
"""
36+
37+
check_moons(moons, t1)
38+
39+
for _ in range(10):
40+
part1.step(moons)
41+
42+
t2 = """
43+
pos=<x= -9, y=-10, z= 1>, vel=<x= -2, y= -2, z= -1>
44+
pos=<x= 4, y= 10, z= 9>, vel=<x= -3, y= 7, z= -2>
45+
pos=<x= 8, y=-10, z= -3>, vel=<x= 5, y= -1, z= -2>
46+
pos=<x= 5, y=-10, z= 3>, vel=<x= 0, y= -4, z= 5>
47+
"""
48+
49+
check_moons(moons, t2)
50+
51+
for _ in range(10):
52+
part1.step(moons)
53+
54+
t3 = """
55+
pos=<x=-10, y= 3, z= -4>, vel=<x= -5, y= 2, z= 0>
56+
pos=<x= 5, y=-25, z= 6>, vel=<x= 1, y= 1, z= -4>
57+
pos=<x= 13, y= 1, z= 1>, vel=<x= 5, y= -2, z= 2>
58+
pos=<x= 0, y= 1, z= 7>, vel=<x= -1, y= -1, z= 2>
59+
"""
60+
61+
check_moons(moons, t3)
62+
63+
for _ in range(10):
64+
part1.step(moons)
65+
66+
t4 = """
67+
pos=<x= 15, y= -6, z= -9>, vel=<x= -5, y= 4, z= 0>
68+
pos=<x= -4, y=-11, z= 3>, vel=<x= -3, y=-10, z= 0>
69+
pos=<x= 0, y= -1, z= 11>, vel=<x= 7, y= 4, z= 3>
70+
pos=<x= -3, y= -2, z= 5>, vel=<x= 1, y= 2, z= -3>
71+
"""
72+
73+
check_moons(moons, t4)
74+
75+
76+
for _ in range(10):
77+
part1.step(moons)
78+
79+
t5 = """
80+
pos=<x= 14, y=-12, z= -4>, vel=<x= 11, y= 3, z= 0>
81+
pos=<x= -1, y= 18, z= 8>, vel=<x= -5, y= 2, z= 3>
82+
pos=<x= -5, y=-14, z= 8>, vel=<x= 1, y= -2, z= 0>
83+
pos=<x= 0, y=-12, z= -2>, vel=<x= -7, y= -3, z= -3>
84+
"""
85+
86+
check_moons(moons, t5)
87+
88+
for _ in range(10):
89+
part1.step(moons)
90+
91+
t6 = """
92+
pos=<x=-23, y= 4, z= 1>, vel=<x= -7, y= -1, z= 2>
93+
pos=<x= 20, y=-31, z= 13>, vel=<x= 5, y= 3, z= 4>
94+
pos=<x= -4, y= 6, z= 1>, vel=<x= -1, y= 1, z= -3>
95+
pos=<x= 15, y= 1, z= -5>, vel=<x= 3, y= -3, z= -3>
96+
"""
97+
98+
check_moons(moons, t6)
99+
100+
for _ in range(10):
101+
part1.step(moons)
102+
103+
t7 = """
104+
pos=<x= 36, y=-10, z= 6>, vel=<x= 5, y= 0, z= 3>
105+
pos=<x=-18, y= 10, z= 9>, vel=<x= -3, y= -7, z= 5>
106+
pos=<x= 8, y=-12, z= -3>, vel=<x= -2, y= 1, z= -7>
107+
pos=<x=-18, y= -8, z= -2>, vel=<x= 0, y= 6, z= -1>
108+
"""
109+
110+
check_moons(moons, t7)
111+
112+
113+
for _ in range(10):
114+
part1.step(moons)
115+
116+
t8 = """
117+
pos=<x=-33, y= -6, z= 5>, vel=<x= -5, y= -4, z= 7>
118+
pos=<x= 13, y= -9, z= 2>, vel=<x= -2, y= 11, z= 3>
119+
pos=<x= 11, y= -8, z= 2>, vel=<x= 8, y= -6, z= -7>
120+
pos=<x= 17, y= 3, z= 1>, vel=<x= -1, y= -1, z= -3>
121+
"""
122+
123+
check_moons(moons, t8)
124+
125+
for _ in range(10):
126+
part1.step(moons)
127+
128+
t9 = """
129+
pos=<x= 30, y= -8, z= 3>, vel=<x= 3, y= 3, z= 0>
130+
pos=<x= -2, y= -4, z= 0>, vel=<x= 4, y=-13, z= 2>
131+
pos=<x=-18, y= -7, z= 15>, vel=<x= -8, y= 2, z= -2>
132+
pos=<x= -2, y= -1, z= -8>, vel=<x= 1, y= 8, z= 0>
133+
"""
134+
135+
check_moons(moons, t9)
136+
137+
for _ in range(10):
138+
part1.step(moons)
139+
140+
t10 = """
141+
pos=<x=-25, y= -1, z= 4>, vel=<x= 1, y= -3, z= 4>
142+
pos=<x= 2, y= -9, z= 0>, vel=<x= -3, y= 13, z= -1>
143+
pos=<x= 32, y= -8, z= 14>, vel=<x= 5, y= -4, z= 6>
144+
pos=<x= -1, y= -2, z= -8>, vel=<x= -3, y= -6, z= -9>
145+
"""
146+
147+
check_moons(moons, t10)
148+
149+
for _ in range(10):
150+
part1.step(moons)
151+
152+
t11 = """
153+
pos=<x= 8, y=-12, z= -9>, vel=<x= -7, y= 3, z= 0>
154+
pos=<x= 13, y= 16, z= -3>, vel=<x= 3, y=-11, z= -5>
155+
pos=<x=-29, y=-11, z= -1>, vel=<x= -3, y= 7, z= 4>
156+
pos=<x= 16, y=-13, z= 23>, vel=<x= 7, y= 1, z= 1>
157+
"""
158+
159+
check_moons(moons, t11)
160+
161+
assert sum(m.energy() for m in moons) == 1940

0 commit comments

Comments
 (0)