+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanDays 7-14 (in python for now)a7718a0

master
1.1 KiB43 linesraw
1from operator import add, sub
2
3lines = open("input").readlines()
4lines = [[tuple(map(int, x.split(","))) for x in s.split("->")] for s in lines]
5world = {}
6floor = max((y for line in lines for _, y in line))
7for line in lines:
8    for i in range(1, len(line)):
9        a, b = line[i - 1], line[i]
10        diff = tuple(map(lambda a, b: ((x := b - a), x and x // abs(x) or 0)[-1], a, b))
11        while a != b:
12            world[a] = "#"
13            a = tuple(map(add, a, diff))
14        world[a] = "#"
15
16# fmt: off
17def drop(has_floor):
18    x, y = 500, 0
19    while True:
20        if y > floor:
21            if has_floor: world[(x, y)] = "o"; return (x, y)
22            else: return False
23        if not world.get((x, y + 1)):
24            y += 1
25        elif not world.get((x - 1, y + 1)):
26            x -= 1; y += 1
27        elif not world.get((x + 1, y + 1)):
28            x += 1; y += 1
29        else:
30            world[(x, y)] = "o"; return (x, y)
31# fmt: on
32
33
34orig = world.copy()
35while drop(False):
36    pass
37count = lambda: len([a for a, b in world.items() if b == "o"])
38print(count())
39
40world = orig
41while last := drop(True) != (500, 0):
42    pass
43print(count())