Use new AoC infrastructure and clean up.
This commit is contained in:
72
d1.py
72
d1.py
@@ -1,64 +1,20 @@
|
||||
import re
|
||||
from lib import *
|
||||
|
||||
EXAMPLE = """
|
||||
1000
|
||||
2000
|
||||
3000
|
||||
|
||||
4000
|
||||
|
||||
5000
|
||||
6000
|
||||
|
||||
7000
|
||||
8000
|
||||
9000
|
||||
|
||||
10000
|
||||
"""
|
||||
|
||||
def solve(lines: list[str]):
|
||||
counter = 0
|
||||
count_max = 0
|
||||
for line in lines.splitlines():
|
||||
if line.strip() == "":
|
||||
if counter >= count_max:
|
||||
count_max = counter
|
||||
counter = 0
|
||||
else:
|
||||
counter += int(line)
|
||||
return count_max
|
||||
|
||||
def solve2(lines: list[str]):
|
||||
counter = 0
|
||||
count_max = 0
|
||||
counts = []
|
||||
for line in lines.splitlines():
|
||||
if line.strip() == "":
|
||||
if counter >= count_max:
|
||||
count_max = counter
|
||||
counts.append(counter)
|
||||
counter = 0
|
||||
else:
|
||||
counter += int(line)
|
||||
counts.append(counter)
|
||||
|
||||
counts.sort(reverse=True)
|
||||
return sum(counts[:3])
|
||||
def solve(input: Input, second=False):
|
||||
ps = input.text.split("\n\n")
|
||||
xss = map(lambda p: map(int, p.splitlines()), ps)
|
||||
xs = list(map(sum, xss))
|
||||
if not second:
|
||||
return max(xs)
|
||||
xs.sort()
|
||||
return sum(xs[-3:])
|
||||
|
||||
def main():
|
||||
example = str(EXAMPLE)
|
||||
print("Example 1:", solve(example))
|
||||
|
||||
data = open("i1.txt").read()
|
||||
print("Solution 1:", solve(data))
|
||||
|
||||
example = str(EXAMPLE)
|
||||
print("Example 2:", solve2(example))
|
||||
|
||||
data = open("i1.txt").read()
|
||||
print("Solution 2:", solve2(data))
|
||||
return
|
||||
DAY_INPUT = "i1.txt"
|
||||
print("Solution 1:", solve(Input(DAY_INPUT)))
|
||||
assert solve(Input(DAY_INPUT)) == 69883
|
||||
print("Solution 2:", solve(Input(DAY_INPUT), True))
|
||||
assert solve(Input(DAY_INPUT), True) == 207576
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
4
d15.py
4
d15.py
@@ -73,7 +73,7 @@ def solve(lines: list[str], yt):
|
||||
sensors = []
|
||||
bacons = set()
|
||||
for (i, line) in enumerate(lines):
|
||||
digits = lib.str_to_int_list(line)
|
||||
digits = lib.str_to_ints(line)
|
||||
sx, sy, bx, by = digits
|
||||
sm = mdist(bx - sx, by - sy)
|
||||
sensors.append([sx, sy, sm])
|
||||
@@ -102,7 +102,7 @@ def solve2(lines: list[str], xymax):
|
||||
sensors = []
|
||||
bacons = set()
|
||||
for (i, line) in enumerate(lines):
|
||||
digits = lib.str_to_int_list(line)
|
||||
digits = lib.str_to_ints(line)
|
||||
sx, sy, bx, by = digits
|
||||
sm = mdist(bx - sx, by - sy)
|
||||
sensors.append([sx, sy, sm])
|
||||
|
||||
6
d16.py
6
d16.py
@@ -18,9 +18,10 @@ def solve(lines: list[str]):
|
||||
for (i, line) in enumerate(lines):
|
||||
source = line.split(" ")[1]
|
||||
to_valves = list(map(lambda v: v.replace(",", ""), line.split(" ")[9:]))
|
||||
flow_rate = lib.str_to_single_int(line)
|
||||
flow_rate = lib.str_to_int(line)
|
||||
nodes[source] = (flow_rate, to_valves)
|
||||
|
||||
|
||||
options = [(0, 0, "AA", ())]
|
||||
visited = set()
|
||||
for _ in range(30):
|
||||
@@ -43,7 +44,7 @@ def solve2(lines: list[str]):
|
||||
for (i, line) in enumerate(lines):
|
||||
source = line.split(" ")[1]
|
||||
to_valves = list(map(lambda v: v.replace(",", ""), line.split(" ")[9:]))
|
||||
flow_rate = lib.str_to_single_int(line)
|
||||
flow_rate = lib.str_to_int(line)
|
||||
nodes[source] = (flow_rate, to_valves)
|
||||
|
||||
options = [(0, 0, ("AA", "AA"), ())]
|
||||
@@ -93,6 +94,7 @@ def main():
|
||||
|
||||
lines = lib.str_to_lines_no_empty(open("i16.txt").read())
|
||||
print("Solution 2:", solve2(lines))
|
||||
assert solve2(lines) == 2591
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
2
d2.py
2
d2.py
@@ -85,12 +85,14 @@ def main():
|
||||
|
||||
data = clean(open("i2.txt").read())
|
||||
print("Solution 1:", solve(data))
|
||||
assert solve(data) == 14069
|
||||
|
||||
example = clean(EXAMPLE)
|
||||
print("Example 2:", solve2(example))
|
||||
|
||||
data = clean(open("i2.txt").read())
|
||||
print("Solution 2:", solve2(data))
|
||||
assert solve2(data) == 12411
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
37
dx.py
37
dx.py
@@ -1,37 +0,0 @@
|
||||
import lib
|
||||
|
||||
EXAMPLE = """
|
||||
"""
|
||||
|
||||
def solve(lines: list[str]):
|
||||
res = 0
|
||||
for (i, line) in enumerate(lines):
|
||||
print(i, line)
|
||||
# digits = lib.str_to_int_list(line)
|
||||
# digit = lib.str_to_single_int(line)
|
||||
return res
|
||||
|
||||
def solve2(lines: list[str]):
|
||||
res = 0
|
||||
for (i, line) in enumerate(lines):
|
||||
print(i, line)
|
||||
return res
|
||||
|
||||
def main():
|
||||
lines = lib.str_to_lines_no_empty(EXAMPLE)
|
||||
print("Example 1:", solve(lines))
|
||||
return
|
||||
|
||||
lines = lib.str_to_lines_no_empty(open("ix.txt").read())
|
||||
print("Solution 1:", solve(lines))
|
||||
return
|
||||
|
||||
lines = lib.str_to_lines_no_empty(EXAMPLE)
|
||||
print("Example 2:", solve2(lines))
|
||||
return
|
||||
|
||||
lines = lib.str_to_lines_no_empty(open("ix.txt").read())
|
||||
print("Solution 2:", solve2(lines))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
17
get.sh
17
get.sh
@@ -1,17 +0,0 @@
|
||||
#!/bin/bash
|
||||
|
||||
if [ "$#" -ne 1 ]; then
|
||||
echo "Usage: $0 <day>"
|
||||
exit 1
|
||||
fi
|
||||
DAY=$1
|
||||
|
||||
SESSION_COOKIE=$(keyring get aoc-session-cookie felixm)
|
||||
echo $SESSION_COOKIE
|
||||
|
||||
curl "https://adventofcode.com/2022/day/$DAY/input" --cookie "session=$SESSION_COOKIE" > "i$DAY.txt"
|
||||
|
||||
# Get instructions as markdown file.
|
||||
# curl "https://adventofcode.com/2022/day/$DAY" --cookie "session=$SESSION_COOKIE" > "day$DAY.html"
|
||||
# pandoc -f html -t markdown "day$DAY.html" -o "d$DAY.md"
|
||||
# rm "day$DAY.html"
|
||||
18
lib.py
18
lib.py
@@ -1,18 +0,0 @@
|
||||
import re
|
||||
|
||||
def str_to_single_int(line: str) -> int:
|
||||
line = line.replace(" ", "")
|
||||
r = re.compile(r"-?\d+")
|
||||
for m in r.findall(line):
|
||||
return int(m)
|
||||
raise Exception("No single digit sequence in '{line}'")
|
||||
|
||||
def str_to_int_list(line: str) -> list[int]:
|
||||
r = re.compile(r"-?\d+")
|
||||
return list(map(int, r.findall(line)))
|
||||
|
||||
def str_to_lines_no_empty(text: str) -> list[str]:
|
||||
return list(filter(lambda l: l.strip() != "", text.splitlines()))
|
||||
|
||||
def str_to_lines(text: str) -> list[str]:
|
||||
return list(text.splitlines())
|
||||
1
monitor.py
Symbolic link
1
monitor.py
Symbolic link
@@ -0,0 +1 @@
|
||||
../aoc2023/monitor.py
|
||||
Reference in New Issue
Block a user