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