diff --git a/14/n.py b/14/n.py new file mode 100644 index 0000000..52a797e --- /dev/null +++ b/14/n.py @@ -0,0 +1,74 @@ +import os + +input = open(os.path.dirname(__file__) + + "/input.txt", "r").readlines() + +cave = set() + +for l in input: + points = l.strip().split(' -> ') + prev = (0, 0) + for p in points: + x, y = p.split(',') + x = int(x) + y = int(y) + if prev == (0, 0): + prev = (y, x) + cave.add(prev) + else: + dy = y-prev[0] + dx = x-prev[1] + tmp = set() + dir = 1 + if dy < 0 or dx < 0: + dir = -1 + if dy != 0: + for i in range(prev[0], prev[0]+dy+dir, dir): + tmp.add((i, prev[1])) + if dx != 0: + for i in range(prev[1], prev[1]+dx+dir, dir): + tmp.add((prev[0], i)) + cave.update(tmp) + prev = (y, x) + + +def nextdown(pos): + possibles = [] + for y, x in cave: + if x == pos[1] and y > pos[0]: + possibles.append(y) + return (sorted(possibles)[0]-1, X) + + +cont = True + +emptycave = len(cave) + +floor = max(r[0] for r in cave)+2 +while cont: + X = 500 + Y = 0 + current = nextdown((Y, X)) + moving = True + while (moving): + + down = (current[0]+1, current[1]) + left = (current[0]+1, current[1]-1) + right = (current[0]+1, current[1]+1) + + if not down in cave: + current = down + if current[0] >= floor: + cont = False + moving = False + + elif not left in cave: + current = left + elif not right in cave: + current = right + else: + cave.add(current) + moving = False + +# print(sorted(cave)) +print(len(cave)-emptycave)