diff options
| author | blenovo <bk@gmail.com> | 2025-03-04 15:37:55 +0100 |
|---|---|---|
| committer | blenovo <bk@gmail.com> | 2025-03-04 15:37:55 +0100 |
| commit | 15662865f0886209d871a7225bfc62cffd2e0783 (patch) | |
| tree | 7130fb1b11a058ffdbeefe471eccd6ad461d8843 /2024/aoc2024-d10.py | |
| parent | a926f0a2aa1818879930f5843d5c2cfabd3bfebc (diff) | |
transfer from previous server
Diffstat (limited to '2024/aoc2024-d10.py')
| -rw-r--r-- | 2024/aoc2024-d10.py | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/2024/aoc2024-d10.py b/2024/aoc2024-d10.py new file mode 100644 index 0000000..4f51a0d --- /dev/null +++ b/2024/aoc2024-d10.py @@ -0,0 +1,55 @@ +#advent of code 2024 +#day 10 +#tbh I don't think I even understood the task correctly +#but the first idea for part 2 +#that came up to my head worked so i'll take it +#all I did was switch a set into a list +#rewritten so the code does both parts now + +grid = {}; +xMin, yMin, xMax, yMax = 0,0,0,0; +dirs = [(1,0),(-1,0),(0,1),(0,-1)]; +starts = []; + +f = open("10.in","r"); +for y,l in enumerate(f): + yMax = max(y,yMax); + for x,c in enumerate(l.strip()): + grid[(x,y)]=int(c); + if c == "0": starts.append((x,y)); + xMax = max(xMax,x); +f.close(); + +def getAdjacent(pos,h): + adj = []; + xp, yp = pos; + for i,d in enumerate(dirs): + dx,dy = d; + if grid.get((xp+dx,yp+dy),-1)-h == 1: + adj.append((xp+dx,yp+dy,i)); + return adj; + +def pathfinding(starts): + score1 = 0; + score2 = 0; + for start in starts: + subscore1 = set(); + subscore2 = list(); + x0,y0 = start; + queue = [(x0,y0,0)]; + while queue: + px,py,pd = queue.pop(); + CurrentHeight = grid.get((px,py),-1); + if CurrentHeight == 9: + subscore1.add((px,py)); + subscore2.append((px,py,pd)); + else: + NextValid = getAdjacent((px,py),CurrentHeight); + queue.extend(NextValid); + score1 += len(subscore1); + score2 += len(subscore2); + return score1, score2; + +part1, part2 = pathfinding(starts); +print("part 1 =", part1); +print("part 2 =", part2); |
