Skip to content

Commit

Permalink
[2019/12] p1 solved
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlitGhost committed Dec 27, 2023
1 parent 205dd7e commit 21efbb9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 3 deletions.
Binary file added .aoc_tiles/tiles/2019/12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions 2019/12/example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<x=-1, y=0, z=2>
<x=2, y=-10, z=-7>
<x=4, y=-8, z=8>
<x=3, y=5, z=-1>
70 changes: 70 additions & 0 deletions 2019/12/script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from GhostyUtils import aoc
from GhostyUtils.vec3 import Vec3
import re
import itertools


def energy(moon: dict) -> int:
pot = sum(map(abs, moon['pos']))
kin = sum(map(abs, moon['v']))
total = pot * kin
return total


def apply_gravity(moons: list[dict]):
for pair in itertools.combinations(moons, 2):
m1, m2 = pair
for i, component in enumerate(zip(m1['pos'], m2['pos'])):
a, b = component
if a < b:
m1['v'][i] += 1
m2['v'][i] -= 1
elif a > b:
m1['v'][i] -= 1
m2['v'][i] += 1
else:
pass


def apply_velocity(moons: list[dict]):
for moon in moons:
moon['pos'] += moon['v']


def vec_str(v: Vec3) -> str:
return f"<x={v.x:3,d}, y={v.y:3,d}, z={v.z:3,d}>"


def print_moons(moons: list[dict]):
for moon in moons:
p = moon['pos']
v = moon['v']
print(f"pos={vec_str(p)}, vel={vec_str(v)} | {moon['name']}")


def main():
names = ['Io', 'Europa', 'Ganymede', 'Callisto']
moons = [
{'pos': Vec3(*map(int, re.sub(r'[<>=xyz ]', '', line).split(','))),
'v': Vec3(0, 0, 0)}
for line in aoc.read_lines()]
[moons[i].__setitem__('name', name) for i, name in enumerate(names)]

print("After 0 steps:")
print_moons(moons)
print()

steps = 1000
for step in range(1, steps+1):
apply_gravity(moons)
apply_velocity(moons)

print(f"After {step} steps:")
print_moons(moons)
print()

print('p1:', sum(map(energy, moons)))


if __name__ == "__main__":
main()
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ My solutions to the yearly Advents of Code
<img src=".aoc_tiles/tiles/2022/15.png" width="161px">
</a>
<h1 align="center">
2019 - 22
2019 - 23
</h1>
<a href="2019/1/script.py">
<img src=".aoc_tiles/tiles/2019/01.png" width="161px">
Expand Down Expand Up @@ -164,6 +164,9 @@ My solutions to the yearly Advents of Code
<a href="2019/11/script.py">
<img src=".aoc_tiles/tiles/2019/11.png" width="161px">
</a>
<a href="2019/12/script.py">
<img src=".aoc_tiles/tiles/2019/12.png" width="161px">
</a>
<h1 align="center">
2015 - 50 ⭐
</h1>
Expand Down
4 changes: 2 additions & 2 deletions utils/GhostyUtils/vec3.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ def __init__(self,
raise ValueError(f"Couldn't init {type(self).__name__} with "
f"{x} ({type(x)}), no y or z given")
elif z is None:
if type(x) is VecDataType and type(y) is VecDataType:
if type(x) in [int, float] and type(y) in [int, float]:
self._data = [x, y, 0]
else:
raise ValueError(f"Couldn't init {type(self).__name__} with "
f"{x} ({type(x)}), "
f"{y} ({type(y)}), "
f"no z given (would default to 0)")
elif type(x) is VecDataType and type(y) is VecDataType and type(z) is VecDataType:
elif type(x) in [int, float] and type(y) in [int, float] and type(z) in [int, float]:
self._data = [x, y, z]
else:
raise ValueError(f"Couldn't init {type(self).__name__} with "
Expand Down

0 comments on commit 21efbb9

Please sign in to comment.