Skip to content

Commit

Permalink
2024 day 18
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Dec 25, 2024
1 parent 5f355d5 commit 4636fea
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
40 changes: 40 additions & 0 deletions 2024/18/first.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from utils import Point, lines
from collections import deque, namedtuple

def parse(lines):
return [Point(*map(int, line.split(','))) for line in lines]

bounds = Point(71, 71)

Node = namedtuple('Node', ['pos', 'cost'])

def solve(points):
seen = set([(0,0)])
been = set()
fringe = deque([Node(Point(0,0), 0)])
while fringe:
cur = fringe.popleft()
been.add(cur.pos)
if cur.pos == bounds - (1, 1):
return cur.cost
for n in cur.pos.neighbors():
if n not in bounds or n in points:
continue
if n not in seen:
seen.add(n)
fringe.append(Node(n, cur.cost + 1))
return None
# def draw(x, y):
# if (x, y) in points:
# return '#'
# if (x, y) in been:
# return 'O'
# if (x, y) in seen:
# return '.'
# return ' '
# for y in range(bounds.y):
# print(''.join(draw(x, y) for x in range(bounds.x)))

if __name__ == '__main__':
points = parse(lines())
print(solve(set(points[:1024])))
12 changes: 12 additions & 0 deletions 2024/18/second.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from utils import Point, lines
from collections import deque, namedtuple
from first import parse, solve

if __name__ == '__main__':
points = parse(lines())
fallen = set(points[:1024])
for p in points[1024:]:
fallen.add(p)
if not solve(fallen):
print(f'{p.x},{p.y}')
break
26 changes: 26 additions & 0 deletions 2024/18/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from enum import IntEnum, auto

def lines():
while True:
try:
yield input()
except:
break

class Point:
__match_args__ = ['x', 'y']

def __init__(self, x, y): self.x, self.y = x, y
def neighbors(self):
for d in ((1, 0), (0, 1), (-1, 0), (0, -1)):
yield self + d
def __add__(self, other): return Point(self.x + other[0], self.y + other[1])
def __mul__(self, sca): return Point(self.x * sca, self.y * sca)
def __sub__(self, other): return Point(self.x - other[0], self.y - other[1])
def __neg__(self): return Point(-self.x, -self.y)
def __eq__(self, other): return self.x == other[0] and self.y == other[1]
def __getitem__(self, i): return self.x if i == 0 else self.y
def __str__(self): return "({}, {})".format(self.x, self.y)
def __repr__(self): return "({} {})".format(self.x, self.y)
def __hash__(self): return (self.x, self.y).__hash__()
def __contains__(self, other): return other[0] in range(self.x) and other[1] in range(self.y)
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ My solutions for the [Advent of Code](https://adventofcode.com), a challenge sta
| 15 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 16 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 17 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 18 |||||:star::star:|:star::star:|:star::star:|:star::star:|
| 18 |||||:star::star:|:star::star:|:star::star:|:star::star:|:star::star:
| 19 |||||:star::star:||:star::star:|:star::star:|
| 20 |||||:star:|:star::star:|:star::star:|:star:|
| 21 |||||:star::star:|:star::star:|:star::star:|:star:|
| 22 |||||:star::star:|:star:|:star::star:|:star::star:|
| 23 |||||||:star::star:|:star:|
| 24 |||||:star::star:|:star::star:|:star::star:|:star:|
| 25 |||||:star:|:star:|:star::star:||
| Total | 10 | 14 | 4 | 12 | 46 | 44 | 50 | 44 | 28
| Total | 10 | 14 | 4 | 12 | 46 | 44 | 50 | 44 | 30

Total stars: 252
Total stars: 254

0 comments on commit 4636fea

Please sign in to comment.