Finally solve 2019 day 12 and others

This commit is contained in:
2024-09-08 10:21:20 -04:00
parent fa6ea1dfbe
commit bf88f13791
4 changed files with 194 additions and 12 deletions

View File

@@ -1,4 +1,5 @@
from lib import get_data, str_to_ints
from math import lcm
def freeze(coords, velos):
@@ -40,7 +41,40 @@ def part_1(data):
def part_2(data):
raise Exception("TBD")
steps = []
for i in range(3):
coords = []
velos = []
for line in data.splitlines():
coords.append(str_to_ints(line)[i])
velos.append(0)
seen = {}
for step in range(1_000_000):
state = tuple(coords) + tuple(velos)
if state in seen:
steps.append(step)
break
else:
seen[state] = step
for i in range(len(coords)):
for j in range(i + 1, len(coords)):
if coords[i] > coords[j]:
velos[i] -= 1
velos[j] += 1
elif coords[i] < coords[j]:
velos[i] += 1
velos[j] -= 1
# update coords
for i in range(len(coords)):
coords[i] += velos[i]
# Intuition: Find when position repeats on each axis and then find lowest
# common multiple for all three values.
print(lcm(*steps))
def main():

57
2020/d10.py Normal file
View File

@@ -0,0 +1,57 @@
from lib import get_data
from collections import defaultdict
from functools import lru_cache
@lru_cache
def count(xs, current, target):
delta_target = target - current
if len(xs) == 0:
if delta_target <= 3:
return 1
else:
return 0
x = xs[0]
xs = xs[1:]
total = 0
delta = x - current
if delta > 3:
return 0
if delta <= 3:
total += count(xs, x, target)
total += count(xs, current, target)
return total
def part_1(data):
xs = list(map(int, data.strip().splitlines()))
out = max(xs) + 3
ds = defaultdict(int)
c = 0
for x in sorted(xs):
d = x - c
ds[d] += 1
c = x
d = out - c
ds[d] += 1
a, b = list(ds.values())
print(a * b)
xs = tuple(sorted(xs))
print(count(xs, 0, out))
def main():
data = get_data(__file__)
part_1(data)
if __name__ == "__main__":
main()

89
2020/d11.py Normal file
View File

@@ -0,0 +1,89 @@
from lib import get_data, Grid2D, add2
data = """L.LL.LL.LL
LLLLLLL.LL
L.L.L..L..
LLLL.LL.LL
L.LL.LL.LL
L.LLLLL.LL
..L.L.....
LLLLLLLLLL
L.LLLLLL.L
L.LLLLL.LL"""
def part_1(data):
g = Grid2D(data)
seen = set()
while True:
h = g.hash()
if h in seen:
break
seen.add(h)
ng = g.clone_with_val(".")
for r in range(g.n_rows):
for c in range(g.n_cols):
p = (r, c)
s = g[p]
if s == ".":
continue
occupied = sum([1 if g[nb] == "#" else 0 for nb in g.neighbors_adj(p)])
if s == "L" and occupied == 0:
ng[p] = "#"
elif s == "#" and occupied >= 4:
ng[p] = "L"
else:
ng[p] = s
g = ng
print(len(g.find("#")))
def part_2(data):
g = Grid2D(data)
seen = set()
while True:
h = g.hash()
if h in seen:
break
seen.add(h)
ng = g.clone_with_val(".")
for r in range(g.n_rows):
for c in range(g.n_cols):
p = (r, c)
s = g[p]
if s == ".":
continue
occupied = 0
for dir in g.COORDS_ORTH + g.COORDS_DIAG:
np = p
while True:
np = add2(np, dir)
if not g.contains(np):
break
elif g[np] == "#":
occupied += 1
break
elif g[np] == "L":
break
if s == "L" and occupied == 0:
ng[p] = "#"
elif s == "#" and occupied >= 5:
ng[p] = "L"
else:
ng[p] = s
g = ng
print(len(g.find("#")))
def main():
data = get_data(__file__)
part_1(data)
part_2(data)
if __name__ == "__main__":
main()

View File

@@ -123,7 +123,7 @@ Solutions and utility script for Advent of Code challenges in Python.
- Day 9: 115:00 (Try to read next time.)
- Day 10: 76:00 (This wasn't easy for me. Fun, though.)
- Day 11: 21:04 (Too slow, but fun.)
- Day 12:
- Day 12: days (Took me a while to get the right idea.)
- Day 13: >120:00 (Just struggling so much for some reason.)
- Day 14: >120:00 (Hmm, wasn't that hard either.)
- Day 15: >120:00 (I am really weak at the moment.)
@@ -135,16 +135,18 @@ Solutions and utility script for Advent of Code challenges in Python.
## AoC 2020
- Day 1: 2:48 (people weren't able to submit because of a website outage)
- Day 2: 4:47 (no leaderboard, you can tell it's getting faster)
- Day 3: 7:06 (way too slow, lol; time to take it seriously)
- Day 4: 14:30 (yo, I am just too slow)
- Day 5: 11:53 (not competitive)
- Day 6: 4:11 (75th, finally on the leaderboard)
- Day 7: 24:39 (bad)
- Day 8: 6:26 (43th, tied George Hotz :)
- Day 9: 7:37 (choked bad)
- Day 10:
- Day 1: 2:48 (people weren't able to submit because of a website outage)
- Day 2: 4:47 (no leaderboard, you can tell it's getting faster)
- Day 3: 7:06 (way too slow, lol; time to take it seriously)
- Day 4: 14:30 (yo, I am just too slow)
- Day 5: 11:53 (not competitive)
- Day 6: 4:11 (75th, finally on the leaderboard)
- Day 7: 24:39 (bad)
- Day 8: 6:26 (43th, tied George Hotz :)
- Day 9: 7:37 (choked bad)
- Day 10: 34:27 (so weak)
- Day 11: 21:05 (hmmm, I rally have to analyze why I am so slow)
- Day 12:
## AoC 2022