-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
205dd7e
commit 21efbb9
Showing
5 changed files
with
80 additions
and
3 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters