Day 6: Guard Gallivant
Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
python
Takes around 10s on my machine. I came up 10 short at first for part two and then realized sometimes you have to turn multiple times to avoid an obstacle.
solution
from dataclasses import dataclass import aoc @dataclass class Guard(): r: int = 0 c: int = 0 d: str = 'n' def move(self, lines): for _ in range(4): if self.d == 'n': if lines[self.r - 1][self.c] == '#': self.d = 'e' else: self.r -= 1 break elif self.d == 'e': if lines[self.r][self.c + 1] == '#': self.d = 's' else: self.c += 1 break elif self.d == 's': if lines[self.r + 1][self.c] == '#': self.d = 'w' else: self.r += 1 break elif self.d == 'w': if lines[self.r][self.c - 1] == '#': self.d = 'n' else: self.c -= 1 break def setup(): lines = aoc.get_lines(6, padded=(True, 'X', 1)) g = Guard() for row, l in enumerate(lines): for col, c in enumerate(l): if c == '^': g.r, g.c = (row, col) s = (g.r, g.c) p = set() while lines[g.r][g.c] != 'X': p.add((g.r, g.c)) g.move(lines) return (lines, g, s, p) def one(): print(len(setup()[3])) def two(): lines, g, s, p = setup() acc = 0 for r, c in p: t = list(lines) ts = list(t[r]) ts[c] = '#' t[r] = ''.join(ts) g.r, g.c, g.d = (s[0], s[1], 'n') v = set() while t[g.r][g.c] != 'X': st = (g.r, g.c, g.d) if st in v: acc += 1 break v.add(st) g.move(t) print(acc) one() two()