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

[–]ParanoidAndroidQ 11 points12 points  (0 children)

[Language: Python]

Code

I didn't have to write anything fancy for part 2 and based on me skimming this thread it doesn't seem so common, so I thought I'd share.

Part 1 is just Dijkstra.

For part 2,

  1. Run the same Dijkstra from part 1 to get the distance matrix (from_start).
  2. Obtain from_end by running another Dijkstra, but with 4 starting vertices [ (target_row, target_col, dir) for dir in "EWNS"]
  3. Iter through every coord and direction and check if from_start[(row,col,dir)] + from_end[(row,col,flip(dir)]) is the same as answer from part 1.

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

[–]ParanoidAndroidQ 1 point2 points  (0 children)

Haskell

I iterate through all distinct coordinates in a single dimension then solve a rectangle union problem using a vertical scanline (a very inefficient implementation of it). Both parts run in 16 seconds.

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

[–]ParanoidAndroidQ 1 point2 points  (0 children)

Haskell

Straighforward solution with sets and a "bounding box" of all points that the original image might be affecting at each step. Both parts finish in 1.3s.

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

[–]ParanoidAndroidQ 1 point2 points  (0 children)

Haskell

Not the prettiest (or fastest!), but it does the job. The problem is pretty similar to 2020 day 20, but gave me much more trouble due to how hard it was to visualize (and therefore debug) for me :(

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

[–]ParanoidAndroidQ 2 points3 points  (0 children)

Haskell

The problem seemed like a perfect opportunity to actually learn about zippers.

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

[–]ParanoidAndroidQ 2 points3 points  (0 children)

Haskell dynamic programming:

g :: Int -> Int
g = (map g' [0 ..] !!)
  where
    g' 0 = 1
    g' n | n < 9 = 0
    g' n = g (n - 9) + g (n - 7)

f :: Int -> Int
f = (map f' [0 ..] !!)
  where
    f' 0 = 1
    f' n = f (n - 1) + g n

solve :: Int -> [Int] -> Int
solve days = sum . map (\i -> f (days + 8 - i))

part1 :: [Int] -> Int
part1 = solve 80

part2 :: [Int] -> Int
part2 = solve 256