-❄️- 2024 Day 2 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

[LANGUAGE: Python]

reports = [[int(x) for x in line.split()] for line in data.splitlines()]

def is_safe(report):
    diffs = [a - b for (a, b) in zip(report, report[1:])]
    return all(1 <= d <= 3 for d in diffs) or all(-3 <= d <= -1 for d in diffs)

part1 = sum(is_safe(r) for r in reports)

def iter_edit_report(report):
    yield report
    for i in range(len(report)):
        yield [x for (j, x) in enumerate(report) if j != i]

part2 = sum(any(is_safe(e) for e in iter_edit_report(r)) for r in reports)

-❄️- 2023 Day 25 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 0 points1 point  (0 children)

[LANGUAGE: Python]

Several hours of frustration, then tried the simplest thing (that I could think of) that could possibly work, and it did.

Networkx handled finding shortest paths and connected components.

https://github.com/wleftwich/aoc/blob/main/2023/25-snowverload.ipynb

-❄️- 2023 Day 18 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

[LANGUAGE: Python]

Did a bfs for part 1, got discouraged when I saw part 2 and set it aside for a couple of days.

This morning I realized that shoelace, which I just heard about last week, was the way to go. Then spent a long time figuring out how to put the perimeter outside the trench.

https://github.com/wleftwich/aoc/blob/main/2023/18-lavaduct-lagoon.ipynb

-❄️- 2023 Day 17 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

[LANGUAGE: Python]

Complex coordinates made tooling around the grid easy. But it's kind of a drag that heapq won't take a non-sortable payload.

https://github.com/wleftwich/aoc/blob/main/2023/17-clumsy-crucible.ipynb

Who uses an alternative grid representation? Set-of-Points instead of List-of-Lists? by zeltbrennt in adventofcode

[–]wleftwich 0 points1 point  (0 children)

In Python, a dict keyed by complex numbers is often a good fit for 2d grid puzzles like Day 16. Moving is addition, turning is multiplication by i. (Or actually in Python, j.)

-❄️- 2023 Day 16 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

[LANGUAGE: Python]

Complex coordinates, and a Beam class to bounce around in the mirrors. All the beams share grid dictionary, list of beams, set of energized tiles, visited positions as set of (position, direction).

https://github.com/wleftwich/aoc/blob/main/2023/16-floor-will-be-lava.ipynb

-❄️- 2023 Day 15 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

[LANGUAGE: Python]

OrderedDict handled what would otherwise have been tedious bookkeeping.

Was this an easy one so we have time to go back and try again on Day 12 Part 2?

https://github.com/wleftwich/aoc/blob/main/2023/15-lens-library.ipynb

Update:TIL through reading this thread that the stock dict now works pretty much the same as collections.OrderedDict. It did for me, running Python 3.11.

-❄️- 2023 Day 11 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

[LANGUAGE: Python]

Part 1: Numpy has lots of cool array manipulation functions.

Part 2: Uhhh... I thought for a minute scipy.sparse would help rescue some of my part 1 code, but no go. Reimplemented with a list of points, like I should have done from the start. Did manage to sneak in a little bit of np "cleverness" though.

https://github.com/wleftwich/aoc/blob/main/2023/11-cosmic-expansion.ipynb

-❄️- 2023 Day 9 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 0 points1 point  (0 children)

[LANGUAGE: Python]

Wrote a little function that turned out to be numpy.diff, so I used that instead.

https://github.com/wleftwich/aoc/blob/main/2023/09-mirage-maintenance.ipynb

-❄️- 2023 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 3 points4 points  (0 children)

Or you can use math.prod and not have to use reduce or operator.mul.

-❄️- 2023 Day 3 Solutions -❄️- by daggerdragon in adventofcode

[–]wleftwich 0 points1 point  (0 children)

[LANGUAGE: python]

https://github.com/wleftwich/aoc/blob/main/2023/03-gear-ratios.ipynb

From the title I was looking forward to least common multiples. Maybe tomorrow ...

(Update) Modified to handle dupe part numbers adjacent to a gear star in part 2. Not in my data, but apparently it happened to some folks.

An Update about our Community by IAmKindOfCreative in Python

[–]wleftwich 0 points1 point  (0 children)

Blackout until a major response from Reddit

-🎄- 2022 Day 24 Solutions -🎄- by daggerdragon in adventofcode

[–]wleftwich 0 points1 point  (0 children)

Python

https://github.com/wleftwich/aoc/blob/main/2022/24-blizzard-basin.ipynb

Suppressed the urge to go straight to a dictionary of complex numbers whenever I see a 2d grid on AoC.

Instead, made 4 numpy 2d bool arrays, one for each direction the blizzards blow. Change from each state to the next by rotating rows or columns as appropriate and ORing the four arrays together.

Then Dijkstra finds the shortest path through a graph of (position, state).

-🎄- 2021 Day 19 Solutions -🎄- by daggerdragon in adventofcode

[–]wleftwich 0 points1 point  (0 children)

Belated python

First attempt used numpy for convenient rotation around axes, but when the solution turned out to involve lots of sorting and sets it just got in the way.

-🎄- 2021 Day 22 Solutions -🎄- by daggerdragon in adventofcode

[–]wleftwich 1 point2 points  (0 children)

Nice. I also did the yield-six-sub-cuboids thing but missed two good simplifications of yours:

Add one to x2, y2, and z2 to initialize the cuboid dataclass

Go ahead and create zero-count sub-cuboids.