Run black
This commit is contained in:
parent
9e7415833c
commit
d57166fdd7
15 changed files with 65 additions and 49 deletions
2
day02.py
2
day02.py
|
@ -1,6 +1,7 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
part1 = 0
|
part1 = 0
|
||||||
part2 = 0
|
part2 = 0
|
||||||
|
@ -32,4 +33,5 @@ def main():
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
2
day04.py
2
day04.py
|
@ -1,5 +1,6 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
#
|
#
|
||||||
# Part 1
|
# Part 1
|
||||||
|
@ -24,4 +25,5 @@ def main():
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
5
day05.py
5
day05.py
|
@ -17,9 +17,7 @@ def transform_ranges(map, ranges):
|
||||||
for start, length in ranges:
|
for start, length in ranges:
|
||||||
# find the start of the range
|
# find the start of the range
|
||||||
(dst_start, src_start, len) = next(
|
(dst_start, src_start, len) = next(
|
||||||
(dst, src, len)
|
(dst, src, len) for (dst, src, len) in map if src <= start < (src + len)
|
||||||
for (dst, src, len) in map
|
|
||||||
if src <= start < (src + len)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
end = start + length
|
end = start + length
|
||||||
|
@ -53,4 +51,5 @@ def main():
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
3
day06.py
3
day06.py
|
@ -1,6 +1,7 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
from math import ceil, floor, sqrt
|
from math import ceil, floor, sqrt
|
||||||
|
|
||||||
|
|
||||||
def race(time, dist):
|
def race(time, dist):
|
||||||
discr = sqrt(time * time - 4 * dist)
|
discr = sqrt(time * time - 4 * dist)
|
||||||
x1 = (time - discr) / 2
|
x1 = (time - discr) / 2
|
||||||
|
@ -8,6 +9,7 @@ def race(time, dist):
|
||||||
x1, x2 = floor(x1) + 1, ceil(x2) - 1
|
x1, x2 = floor(x1) + 1, ceil(x2) - 1
|
||||||
return x2 - x1 + 1
|
return x2 - x1 + 1
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
input = list(fileinput.input())
|
input = list(fileinput.input())
|
||||||
times = [int(x) for x in input[0][11:].split()]
|
times = [int(x) for x in input[0][11:].split()]
|
||||||
|
@ -23,4 +25,5 @@ def main():
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
2
day07.py
2
day07.py
|
@ -22,6 +22,7 @@ for line in fileinput.input():
|
||||||
counter = Counter(hand)
|
counter = Counter(hand)
|
||||||
INPUT.append((hand, int(bid), counter["J"], counter.most_common() + [("", 0)]))
|
INPUT.append((hand, int(bid), counter["J"], counter.most_common() + [("", 0)]))
|
||||||
|
|
||||||
|
|
||||||
def solve(part):
|
def solve(part):
|
||||||
STRENGTHS["J"] = 14 if part == 2 else 4
|
STRENGTHS["J"] = 14 if part == 2 else 4
|
||||||
|
|
||||||
|
@ -54,6 +55,7 @@ def solve(part):
|
||||||
hands.sort(key=lambda h: h[0], reverse=True)
|
hands.sort(key=lambda h: h[0], reverse=True)
|
||||||
return sum((i + 1) * bid for (i, (_, bid)) in enumerate(hands))
|
return sum((i + 1) * bid for (i, (_, bid)) in enumerate(hands))
|
||||||
|
|
||||||
|
|
||||||
part1 = solve(part=1)
|
part1 = solve(part=1)
|
||||||
part2 = solve(part=2)
|
part2 = solve(part=2)
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
2
day09.py
2
day09.py
|
@ -1,6 +1,7 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
from collections import deque
|
from collections import deque
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
part1 = 0
|
part1 = 0
|
||||||
part2 = 0
|
part2 = 0
|
||||||
|
@ -22,4 +23,5 @@ def main():
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
30
day10.py
30
day10.py
|
@ -68,11 +68,15 @@ print(part1)
|
||||||
|
|
||||||
side = set()
|
side = set()
|
||||||
for y in range(len(map)):
|
for y in range(len(map)):
|
||||||
if map[y][0] == ".": side.add((0, y))
|
if map[y][0] == ".":
|
||||||
if map[y][-1] == ".": side.add((len(map[0]) - 1, y))
|
side.add((0, y))
|
||||||
|
if map[y][-1] == ".":
|
||||||
|
side.add((len(map[0]) - 1, y))
|
||||||
for x in range(len(map[0])):
|
for x in range(len(map[0])):
|
||||||
if map[0][x] == ".": side.add((x, 0))
|
if map[0][x] == ".":
|
||||||
if map[-1][x] == ".": side.add((x, len(map) - 1))
|
side.add((x, 0))
|
||||||
|
if map[-1][x] == ".":
|
||||||
|
side.add((x, len(map) - 1))
|
||||||
|
|
||||||
mapp = []
|
mapp = []
|
||||||
for (x, row) in enumerate(map):
|
for (x, row) in enumerate(map):
|
||||||
|
@ -80,21 +84,21 @@ for (x, row) in enumerate(map):
|
||||||
for (y, c) in enumerate(row):
|
for (y, c) in enumerate(row):
|
||||||
match c:
|
match c:
|
||||||
case ".":
|
case ".":
|
||||||
r.append(["..."]*3)
|
r.append(["..."] * 3)
|
||||||
case "S":
|
case "S":
|
||||||
r.append([".X.","XXX",".X."])
|
r.append([".X.", "XXX", ".X."])
|
||||||
case "-":
|
case "-":
|
||||||
r.append(["...","XXX","..."])
|
r.append(["...", "XXX", "..."])
|
||||||
case "|":
|
case "|":
|
||||||
r.append([".X.",".X.",".X."])
|
r.append([".X.", ".X.", ".X."])
|
||||||
case "7":
|
case "7":
|
||||||
r.append(["...","XX.",".X."])
|
r.append(["...", "XX.", ".X."])
|
||||||
case "F":
|
case "F":
|
||||||
r.append(["...",".XX",".X."])
|
r.append(["...", ".XX", ".X."])
|
||||||
case "J":
|
case "J":
|
||||||
r.append([".X.","XX.","..."])
|
r.append([".X.", "XX.", "..."])
|
||||||
case "L":
|
case "L":
|
||||||
r.append([".X.",".XX","..."])
|
r.append([".X.", ".XX", "..."])
|
||||||
mapp.append(r)
|
mapp.append(r)
|
||||||
|
|
||||||
bigmap = []
|
bigmap = []
|
||||||
|
@ -146,6 +150,6 @@ for y in range(1, len(bigmap), 3):
|
||||||
for x in range(1, len(bigmap[y]), 3):
|
for x in range(1, len(bigmap[y]), 3):
|
||||||
if bigmap[y][x] == "." or (distances[y // 3][x // 3] is None):
|
if bigmap[y][x] == "." or (distances[y // 3][x // 3] is None):
|
||||||
part2 += 1
|
part2 += 1
|
||||||
map[y//3][x//3] = 'I'
|
map[y // 3][x // 3] = "I"
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
2
day11.py
2
day11.py
|
@ -3,6 +3,7 @@ import math
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from itertools import pairwise
|
from itertools import pairwise
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
(part1, part2) = (0, 0)
|
(part1, part2) = (0, 0)
|
||||||
|
|
||||||
|
@ -72,5 +73,6 @@ def main():
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
6
day12.py
6
day12.py
|
@ -6,10 +6,12 @@ import re
|
||||||
|
|
||||||
RE = re.compile(r"[#\?]")
|
RE = re.compile(r"[#\?]")
|
||||||
|
|
||||||
|
|
||||||
def powerset(iterable):
|
def powerset(iterable):
|
||||||
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
|
||||||
s = list(iterable)
|
s = list(iterable)
|
||||||
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
|
return chain.from_iterable(combinations(s, r) for r in range(len(s) + 1))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
(part1, part2) = (0, 0)
|
(part1, part2) = (0, 0)
|
||||||
|
@ -27,8 +29,8 @@ def main():
|
||||||
if match == groups:
|
if match == groups:
|
||||||
part1 += 1
|
part1 += 1
|
||||||
|
|
||||||
|
|
||||||
print(part1, part2)
|
print(part1, part2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
4
day16.py
4
day16.py
|
@ -1,5 +1,6 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def simulate(map, entry):
|
def simulate(map, entry):
|
||||||
seen = set()
|
seen = set()
|
||||||
energised = set()
|
energised = set()
|
||||||
|
@ -45,9 +46,10 @@ def simulate(map, entry):
|
||||||
stack.append(((x + 1, y), (1, 0)))
|
stack.append(((x + 1, y), (1, 0)))
|
||||||
case "\\" if (dx, dy) == (0, -1):
|
case "\\" if (dx, dy) == (0, -1):
|
||||||
stack.append(((x - 1, y), (-1, 0)))
|
stack.append(((x - 1, y), (-1, 0)))
|
||||||
|
|
||||||
return len(energised)
|
return len(energised)
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
with open(sys.argv[1]) as f:
|
with open(sys.argv[1]) as f:
|
||||||
map = [list(l) for l in f.read().strip().splitlines()]
|
map = [list(l) for l in f.read().strip().splitlines()]
|
||||||
|
|
16
day17.py
16
day17.py
|
@ -13,7 +13,9 @@ def main() -> None:
|
||||||
|
|
||||||
pprint(map)
|
pprint(map)
|
||||||
|
|
||||||
heap: list[tuple[int, tuple[int, int], int, int, tuple[Any, ...]]] = [(0, (0, 0), 0, 0, ())]
|
heap: list[tuple[int, tuple[int, int], int, int, tuple[Any, ...]]] = [
|
||||||
|
(0, (0, 0), 0, 0, ())
|
||||||
|
]
|
||||||
seen = set()
|
seen = set()
|
||||||
while heap:
|
while heap:
|
||||||
(cost, (x, y), dir, straight, trace) = heapq.heappop(heap)
|
(cost, (x, y), dir, straight, trace) = heapq.heappop(heap)
|
||||||
|
@ -30,9 +32,11 @@ def main() -> None:
|
||||||
if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
||||||
newtrace = (*trace, (x + dx, y + dy, dir))
|
newtrace = (*trace, (x + dx, y + dy, dir))
|
||||||
newcost = cost + map[y + dy][x + dx]
|
newcost = cost + map[y + dy][x + dx]
|
||||||
heapq.heappush(heap, (newcost, (x + dx, y + dy), dir, straight + 1, newtrace))
|
heapq.heappush(
|
||||||
|
heap, (newcost, (x + dx, y + dy), dir, straight + 1, newtrace)
|
||||||
|
)
|
||||||
|
|
||||||
newdir = (dir + 1) % 4
|
newdir = (dir + 1) % 4
|
||||||
(dx, dy) = DIRS[newdir]
|
(dx, dy) = DIRS[newdir]
|
||||||
if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
||||||
newtrace = (*trace, (x + dx, y + dy, newdir))
|
newtrace = (*trace, (x + dx, y + dy, newdir))
|
||||||
|
@ -65,10 +69,6 @@ def main() -> None:
|
||||||
cost += map[y][x]
|
cost += map[y][x]
|
||||||
print(cost)
|
print(cost)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
15
day17b.py
15
day17b.py
|
@ -32,13 +32,19 @@ def main() -> None:
|
||||||
break
|
break
|
||||||
|
|
||||||
(dx, dy) = DIRS[dir]
|
(dx, dy) = DIRS[dir]
|
||||||
if 10 >= (straight + 1) and (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
if (
|
||||||
|
10 >= (straight + 1)
|
||||||
|
and (0 <= (y + dy) < len(map))
|
||||||
|
and (0 <= (x + dx) < len(map[y]))
|
||||||
|
):
|
||||||
newtrace = trace + ((x + dx, y + dy, dir),)
|
newtrace = trace + ((x + dx, y + dy, dir),)
|
||||||
newcost = cost + map[y + dy][x + dx]
|
newcost = cost + map[y + dy][x + dx]
|
||||||
heapq.heappush(heap, (newcost, (x + dx, y + dy), dir, straight + 1, newtrace))
|
heapq.heappush(
|
||||||
|
heap, (newcost, (x + dx, y + dy), dir, straight + 1, newtrace)
|
||||||
|
)
|
||||||
|
|
||||||
if 11 >= (straight + 1) > 4:
|
if 11 >= (straight + 1) > 4:
|
||||||
newdir = (dir + 1) % 4
|
newdir = (dir + 1) % 4
|
||||||
(dx, dy) = DIRS[newdir]
|
(dx, dy) = DIRS[newdir]
|
||||||
if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])):
|
||||||
newtrace = trace + ((x + dx, y + dy, newdir),)
|
newtrace = trace + ((x + dx, y + dy, newdir),)
|
||||||
|
@ -64,8 +70,9 @@ def main() -> None:
|
||||||
print("\n".join("".join(line) for line in path))
|
print("\n".join("".join(line) for line in path))
|
||||||
print(cost)
|
print(cost)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
# 1040 too low
|
# 1040 too low
|
||||||
# 1066 too high
|
# 1066 too high
|
||||||
|
|
9
day19.py
9
day19.py
|
@ -1,12 +1,13 @@
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def part1(workflows: dict[str, list[list[str]]], ratings: str) -> int:
|
def part1(workflows: dict[str, list[list[str]]], ratings: str) -> int:
|
||||||
result = 0
|
result = 0
|
||||||
|
|
||||||
for rating in ratings.splitlines():
|
for rating in ratings.splitlines():
|
||||||
assert [r.split("=")[0] for r in rating[1:-1].split(",")] == list("xmas")
|
assert [r.split("=")[0] for r in rating[1:-1].split(",")] == list("xmas")
|
||||||
(x, m, a, s) = (int(r.split("=")[1]) for r in rating[1:-1].split(","))
|
(x, m, a, s) = (int(r.split("=")[1]) for r in rating[1:-1].split(","))
|
||||||
|
|
||||||
workflow = "in"
|
workflow = "in"
|
||||||
while workflow not in "RA":
|
while workflow not in "RA":
|
||||||
for rule in workflows[workflow]:
|
for rule in workflows[workflow]:
|
||||||
|
@ -76,10 +77,11 @@ def part2(workflows: dict[str, list[list[str]]]) -> int:
|
||||||
s = (value, s[1])
|
s = (value, s[1])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
with open(sys.argv[1]) as f:
|
with open(sys.argv[1]) as f:
|
||||||
(workflows_, ratings) = f.read().split("\n\n")
|
(workflows_, ratings) = f.read().split("\n\n")
|
||||||
|
|
||||||
workflows = {}
|
workflows = {}
|
||||||
for workflow in workflows_.splitlines():
|
for workflow in workflows_.splitlines():
|
||||||
(name, rules_) = workflow.split("{", 1)
|
(name, rules_) = workflow.split("{", 1)
|
||||||
|
@ -90,5 +92,6 @@ def main() -> None:
|
||||||
p2 = part2(workflows)
|
p2 = part2(workflows)
|
||||||
print(p1, p2)
|
print(p1, p2)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
1
empty.py
1
empty.py
|
@ -1,2 +1,3 @@
|
||||||
import fileinput
|
import fileinput
|
||||||
|
|
||||||
list(fileinput.input())
|
list(fileinput.input())
|
||||||
|
|
15
template.py
15
template.py
|
@ -1,15 +0,0 @@
|
||||||
import fileinput
|
|
||||||
import math
|
|
||||||
from pprint import pprint
|
|
||||||
|
|
||||||
def main():
|
|
||||||
(part1, part2) = (0, 0)
|
|
||||||
|
|
||||||
inp = fileinput.input()
|
|
||||||
for line in inp:
|
|
||||||
pass
|
|
||||||
|
|
||||||
print(part1, part2)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
main()
|
|
Loading…
Reference in a new issue