From d57166fdd7c3f8a2505df7dbdc28291fc7c20e91 Mon Sep 17 00:00:00 2001 From: Sijmen Date: Thu, 4 Jan 2024 04:42:27 +0100 Subject: [PATCH] Run black --- day02.py | 2 ++ day04.py | 2 ++ day05.py | 5 ++--- day06.py | 3 +++ day07.py | 2 ++ day09.py | 2 ++ day10.py | 30 +++++++++++++++++------------- day11.py | 2 ++ day12.py | 6 ++++-- day16.py | 4 +++- day17.py | 16 ++++++++-------- day17b.py | 15 +++++++++++---- day19.py | 9 ++++++--- empty.py | 1 + template.py | 15 --------------- 15 files changed, 65 insertions(+), 49 deletions(-) delete mode 100644 template.py diff --git a/day02.py b/day02.py index 5aef6fd..9d41a99 100644 --- a/day02.py +++ b/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() diff --git a/day04.py b/day04.py index 34ce9c6..1a5438a 100644 --- a/day04.py +++ b/day04.py @@ -1,5 +1,6 @@ import fileinput + def main(): # # Part 1 @@ -24,4 +25,5 @@ def main(): print(part1, part2) + main() diff --git a/day05.py b/day05.py index fbe3df1..da5a8a9 100644 --- a/day05.py +++ b/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() diff --git a/day06.py b/day06.py index 5653393..39a8870 100644 --- a/day06.py +++ b/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() diff --git a/day07.py b/day07.py index 4b7ea92..dfc33f6 100644 --- a/day07.py +++ b/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) diff --git a/day09.py b/day09.py index efd448c..f89a9ca 100644 --- a/day09.py +++ b/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() diff --git a/day10.py b/day10.py index 6dbda21..990bd94 100644 --- a/day10.py +++ b/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) diff --git a/day11.py b/day11.py index 36e1b8a..9ea377b 100644 --- a/day11.py +++ b/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() diff --git a/day12.py b/day12.py index eeda91d..83fc49f 100644 --- a/day12.py +++ b/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() diff --git a/day16.py b/day16.py index 2db0cbd..2a2fce3 100644 --- a/day16.py +++ b/day16.py @@ -1,5 +1,6 @@ import sys + def simulate(map, entry): seen = set() energised = set() @@ -45,9 +46,10 @@ def simulate(map, entry): stack.append(((x + 1, y), (1, 0))) case "\\" if (dx, dy) == (0, -1): stack.append(((x - 1, y), (-1, 0))) - + return len(energised) + def main(): with open(sys.argv[1]) as f: map = [list(l) for l in f.read().strip().splitlines()] diff --git a/day17.py b/day17.py index 5a011dc..38b696c 100644 --- a/day17.py +++ b/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,9 +32,11 @@ 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 + newdir = (dir + 1) % 4 (dx, dy) = DIRS[newdir] if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])): newtrace = (*trace, (x + dx, y + dy, newdir)) @@ -65,10 +69,6 @@ def main() -> None: cost += map[y][x] print(cost) + if __name__ == "__main__": main() - - - - - diff --git a/day17b.py b/day17b.py index e167f1f..fb5ab68 100644 --- a/day17b.py +++ b/day17b.py @@ -32,13 +32,19 @@ 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 + newdir = (dir + 1) % 4 (dx, dy) = DIRS[newdir] if (0 <= (y + dy) < len(map)) and (0 <= (x + dx) < len(map[y])): newtrace = trace + ((x + dx, y + dy, newdir),) @@ -64,8 +70,9 @@ def main() -> None: print("\n".join("".join(line) for line in path)) print(cost) + if __name__ == "__main__": main() # 1040 too low -# 1066 too high \ No newline at end of file +# 1066 too high diff --git a/day19.py b/day19.py index 4b3b7ac..38fecba 100644 --- a/day19.py +++ b/day19.py @@ -1,12 +1,13 @@ import sys + def part1(workflows: dict[str, list[list[str]]], ratings: str) -> int: result = 0 for rating in ratings.splitlines(): 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(",")) - + workflow = "in" while workflow not in "RA": for rule in workflows[workflow]: @@ -76,10 +77,11 @@ 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") - + workflows = {} for workflow in workflows_.splitlines(): (name, rules_) = workflow.split("{", 1) @@ -90,5 +92,6 @@ def main() -> None: p2 = part2(workflows) print(p1, p2) + if __name__ == "__main__": - main() \ No newline at end of file + main() diff --git a/empty.py b/empty.py index 5420848..f5cab17 100644 --- a/empty.py +++ b/empty.py @@ -1,2 +1,3 @@ import fileinput + list(fileinput.input()) diff --git a/template.py b/template.py deleted file mode 100644 index eb710ba..0000000 --- a/template.py +++ /dev/null @@ -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()