diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d0933d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +lib/__pycache__ diff --git a/day10/day10.py b/day10/day10.py index 984d129..a64b014 100644 --- a/day10/day10.py +++ b/day10/day10.py @@ -50,7 +50,7 @@ class Tile(object): self.links = [S, E] def __str__(self): - return f'{self.val} x: {self.x} y: {self.y}' + return f'{self.val}' # x: {self.x} y: {self.y}' def __eq__(self, obj) -> bool: return self.x == obj.x and self.y == obj.y @@ -126,10 +126,12 @@ class Walker(Field): while True: self.loop.append(curr) nxt = self.get_adjacent(curr, dir) - if not nxt or not nxt.has_link_from(dir.opposite()): + if not nxt: break if nxt.val == START: return + if not nxt.has_link_from(dir.opposite()): + break curr = nxt dir = nxt.opposite_pipe_end(dir.opposite()) @@ -147,17 +149,27 @@ class Walker(Field): inside_loop = set() for t in self.loop: for d in [N, E, S, W]: - newt = self.get_adjacent(t, d) + newt = self.get_adjacent(t, d) if not newt or newt in self.visited: continue - if self._is_inside_loop(newt): + if self._is_inside_loop(newt, [N, E, S, W]): inside_loop.add(newt) self.visited.add(newt) return len(inside_loop) - def _is_inside_loop(self, t: Tile) -> bool: - # TODO + def _is_inside_loop(self, t: Tile, dirs: list[Direction]) -> bool: + if not t: + return False + + if t in self.loop: + return True + + for d in dirs: + newt = self.get_adjacent(t, d) + if not self._is_inside_loop(newt, [d]): + return False + return True @@ -167,8 +179,7 @@ if __name__ == '__main__': lines = r.read() w = Walker(lines) - steps = w.find_max_steps_in_loop() - print(steps) + print(w) n = w.find_captured_by_loop() print(n) @@ -178,5 +189,6 @@ if __name__ == '__main__': w = Walker(lines) steps = w.find_max_steps_in_loop() print(steps) - # print(w.loop) + n = w.find_captured_by_loop() + print(n) diff --git a/lib/__pycache__/tools.cpython-310.pyc b/lib/__pycache__/tools.cpython-310.pyc deleted file mode 100644 index 31e8706..0000000 Binary files a/lib/__pycache__/tools.cpython-310.pyc and /dev/null differ