this post was submitted on 17 Dec 2024
9 points (100.0% liked)

Advent Of Code

995 readers
4 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 17: Chronospatial Computer

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

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 2 weeks ago

Haskell

Woah, that was suddenly a hard one: several tricky things combined. I'm not a big fan of the kind of problems like part 2 today, but eh - you can't please everyone.

Solution

import Control.Monad
import Control.Monad.RWS
import Data.Bits
import Data.Foldable
import Data.List
import Data.List.Split
import Data.Vector (Vector)
import Data.Vector qualified as Vector

type Machine = RWS (Vector Int) [Int] (Int, Int, Int)

readInput :: String -> ((Int, Int, Int), Vector Int)
readInput s =
  let (regs, _ : [prog]) = break null $ lines s
   in ( let [a, b, c] = map (read . last . words) regs in (a, b, c),
        let [_, s] = words prog in Vector.fromList $ map read $ splitOn "," s
      )

stepMachine :: Int -> Machine Int
stepMachine ip = do
  opcode <- asks (Vector.! ip)
  operand <- asks (Vector.! (ip + 1))
  (a, b, c) <- get
  let combo = [0, 1, 2, 3, a, b, c, undefined] !! operand
      ip' = ip + 2
      adv = a `div` (2 ^ combo)
      store 'A' v = modify (\(_, b, c) -> (v, b, c))
      store 'B' v = modify (\(a, _, c) -> (a, v, c))
      store 'C' v = modify (\(a, b, _) -> (a, b, v))
  case opcode of
    0 -> store 'A' adv >> return ip'
    1 -> store 'B' (b `xor` operand) >> return ip'
    2 -> store 'B' (combo .&. 7) >> return ip'
    3 -> return $ if a == 0 then ip' else operand
    4 -> store 'B' (b `xor` c) >> return ip'
    5 -> tell [combo .&. 7] >> return ip'
    6 -> store 'B' adv >> return ip'
    7 -> store 'C' adv >> return ip'

part1 (regs, prog) =
  let (a, s, w) = runRWS (go 0) prog regs
   in intercalate "," $ map show w
  where
    go ip = when (ip < Vector.length prog) $ stepMachine ip >>= go

part2 (_, prog) = minimum $ foldM go 0 $ reverse $ toList prog
  where
    go a d = do
      b <- [0 .. 7]
      let a' = (a `shiftL` 3) .|. b
          b1 = b `xor` 5
          b2 = b1 `xor` (a' `shiftR` b1)
          b3 = b2 `xor` 6
      guard $ b3 .&. 7 == d
      return a'

main = do
  input <- readInput <$> readFile "input17"
  putStrLn $ part1 input
  print $ part2 input