Start with 2021

This commit is contained in:
felixm 2024-10-20 20:48:27 -04:00
parent e73fa3bae7
commit 10e1e567c8
5 changed files with 108 additions and 0 deletions

20
2021/d1.py Normal file
View File

@ -0,0 +1,20 @@
from lib import get_data
from lib import ints
data = get_data(__file__)
xs = ints(data)
t = 0
for a, b in zip(xs, xs[1:]):
if b > a:
t += 1
print(t)
t = 0
prev_sum = 10**12
for i in range(len(xs) - 2):
s = sum(xs[i : i + 3])
if s > prev_sum:
t += 1
prev_sum = s
print(t)

24
2021/d2.py Normal file
View File

@ -0,0 +1,24 @@
from lib import get_data
from lib import ints
data = get_data(__file__)
aim, h2, d2 = 0, 0, 0
h, d = 0, 0
for line in data.splitlines():
(v,) = ints(line)
if "forward" in line:
h2 += v
d2 += aim * v
h += v
elif "down" in line:
aim += v
d += v
elif "up" in line:
aim -= v
d -= v
else:
assert False
print(h * d)
print(h2 * d2)

56
2021/d3.py Normal file
View File

@ -0,0 +1,56 @@
from lib import get_data
from lib import ints
from collections import defaultdict
data = get_data(__file__)
def count_one_zero(xs, index):
ones, zeros = 0, 0
for x in xs:
if x[index] == "1":
ones += 1
elif x[index] == "0":
zeros += 1
else:
assert False
return (ones, zeros)
a = ""
b = ""
lines = list(data.strip().splitlines())
for i in range(len(lines[0])):
ones, zeros = count_one_zero(lines, i)
if ones > zeros:
a += "1"
b += "0"
elif zeros > ones:
a += "0"
b += "1"
else:
assert False
a, b = int(a, 2), int(b, 2)
print(a * b)
lines = list(data.strip().splitlines())
for i in range(len(lines[0])):
ones, zeros = count_one_zero(lines, i)
c = "1" if ones >= zeros else "0"
lines = [l for l in lines if l[i] == c]
if len(lines) == 1:
break
(line,) = lines
a = int(line, 2)
lines = list(data.strip().splitlines())
for i in range(len(lines[0])):
ones, zeros = count_one_zero(lines, i)
c = "0" if zeros <= ones else "1"
lines = [l for l in lines if l[i] == c]
if len(lines) == 1:
break
(line,) = lines
b = int(line, 2)
print(a * b)

1
2021/lib.py Symbolic link
View File

@ -0,0 +1 @@
../lib.py

View File

@ -170,6 +170,13 @@ Solutions and utility script for Advent of Code challenges in Python.
- Day 24: 15:38 (Close to leaderboard)
- Day 25: 14:40 (Way too slow)
## AoC 2021
- Day 1: 4:01 (Haha. Why am I so bad?!?!)
- Day 2: 6:36 (Okay. I might as well stop doing these.)
- Day 3: 23:46 (How long can I take when I try to take long?)
- Day 4:
## AoC 2022
Done with this. Overall everything is solvable. It's more about consistency