adventofcode-2020/day8/main.hs

141 lines
4.4 KiB
Haskell
Raw Permalink Normal View History

2020-12-08 21:26:09 +01:00
#! /usr/bin/env -S"ANSWER=42" nix-shell
#! nix-shell -p ghcid
2020-12-09 13:04:43 +01:00
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple vector containers])"
2020-12-09 18:59:35 +01:00
#! nix-shell -i "ghcid -c 'ghci' -T main"
2020-12-08 21:26:09 +01:00
2020-12-09 18:59:35 +01:00
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
2020-12-09 22:30:18 +01:00
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-local-binds #-}
2020-12-08 21:26:09 +01:00
{-# LANGUAGE OverloadedStrings #-}
2020-12-09 13:04:43 +01:00
import Text.Pretty.Simple
-- import Data.List.NonEmpty (NonEmpty(..))
-- import qualified Data.List.NonEmpty as NE
import Data.Vector (Vector)
import Debug.Trace (trace)
2020-12-09 22:30:18 +01:00
import Data.List (unfoldr, zip4, unzip4)
2020-12-09 18:59:35 +01:00
import Data.IntMap.Strict (IntMap)
2020-12-09 13:04:43 +01:00
import qualified Data.Vector as V
2020-12-09 18:59:35 +01:00
import qualified Data.IntMap.Strict as M
2020-12-09 13:04:43 +01:00
2020-12-09 22:30:18 +01:00
data Op = OpNop !Int
2020-12-09 13:04:43 +01:00
| OpJmp !Int
| OpAcc !Int
deriving (Show, Ord, Eq)
2020-12-09 22:30:18 +01:00
data VMState = VMRun | VMHalt
2020-12-09 13:04:43 +01:00
deriving (Show, Ord, Eq)
data VirtualMachine = VM
{ pgmCounter :: !Int
, pgmAccumulator :: !Int
, pgmInstructions :: Vector Op
, vmState :: VMState
}
deriving (Show, Ord, Eq)
initialVM :: Vector Op -> VirtualMachine
initialVM ins = VM 1 0 ins VMRun
stepVM :: VirtualMachine -> Maybe (VirtualMachine, VirtualMachine)
2020-12-10 11:49:39 +01:00
stepVM current@(VM c a ins VMRun) = Just (current, checkHalt next)
2020-12-09 13:04:43 +01:00
where
2020-12-10 11:49:39 +01:00
next = go (ins V.! (c-1))
2020-12-09 22:30:18 +01:00
go (OpNop _) = VM (c+1) a ins VMRun
go (OpJmp n) = VM (c+n) a ins VMRun
go (OpAcc n) = VM (c+1) (a+n) ins VMRun
2020-12-10 11:49:39 +01:00
checkHalt vm0@(VM c0 a0 _ _) | c0 == lastCount + 1 =
trace ("halted, acc="<>show a0) $ vm0 { vmState = VMHalt }
checkHalt vm0@(VM _ _ _ _) | otherwise =
vm0
2020-12-09 22:30:18 +01:00
lastCount = V.length ins
stepVM (VM _ _ _ VMHalt) = Nothing
2020-12-09 13:04:43 +01:00
exampleData :: [String]
exampleData =
[ "nop +0"
, "acc +1"
, "jmp +4"
, "acc +3"
, "jmp -3"
, "acc -99"
, "acc +1"
, "jmp -4"
, "acc +6"
]
vmFromInput :: [String] -> VirtualMachine
vmFromInput input = initialVM (ins input)
where
2020-12-09 22:30:18 +01:00
ins = V.fromList . map parseOp
parseOp :: String -> Op
parseOp = go . (\(x,y)-> (x,tail y)) . break (== ' ')
where
go ("nop",'+':s) = OpNop (read s :: Int)
go ("acc",'+':s) = OpAcc (read s :: Int)
go ("jmp",'+':s) = OpJmp (read s :: Int)
go ("nop",s) = OpNop (read s :: Int)
go ("acc",s) = OpAcc (read s :: Int)
go ("jmp",s) = OpJmp (read s :: Int)
go s@_ = trace (show s) $ undefined
2020-12-09 13:04:43 +01:00
-- Find smallest looping prefix in list of Int, returns associated pgmAccumulator
solvePart1 :: VirtualMachine -> Int
2020-12-09 18:59:35 +01:00
solvePart1 vm0 =
snd . last $ shortestCycleOn $ (\vm -> (pgmCounter vm, pgmAccumulator vm)) <$> unfoldr stepVM vm0
2020-12-09 13:04:43 +01:00
2020-12-09 13:05:30 +01:00
-- Shortest cycle in [Int]
2020-12-09 18:59:35 +01:00
shortestCycle :: [Int] -> [Int]
shortestCycle ix = reverse $ go M.empty ix []
where
go :: (IntMap ()) -> [Int] -> [Int] -> [Int]
go mem (x:_ ) ys | (mem M.!? x) /= Nothing = ys
go mem (x:xs) ys | otherwise = go (M.insert x () mem) xs (x:ys)
go _ [] ys = ys
shortestCycleOn :: [(Int,Int)] -> [(Int,Int)]
shortestCycleOn tx = zip (shortestCycle cx) ax
where
(cx, ax) = unzip tx
2020-12-09 13:05:30 +01:00
2020-12-09 22:30:18 +01:00
shortestCycleVm :: [VirtualMachine] -> [VirtualMachine]
shortestCycleVm vx = (\(a,b,c,d) -> VM a b c d) <$> zip4 (shortestCycle cx) ax ins state
where
(cx, ax, ins, state) = unzip4 $ (\(VM a b c d) -> (a,b,c,d)) <$> vx
-- Try lots of permutations of OpNop -> OpJmp, or OpJmp -> OpNop
-- until the VirtualMachine halts, and read the value of the accumulator
2020-12-10 11:49:39 +01:00
solvePart2 :: Vector Op -> Vector VMState
solvePart2 = fmap (vmState . last . shortestCycleVm . unfoldr stepVM . initialVM) . swapInstructions
2020-12-09 22:30:18 +01:00
swapInstructions :: Vector Op -> Vector (Vector Op)
2020-12-10 11:49:39 +01:00
swapInstructions ins = V.imap swap ins
2020-12-09 22:30:18 +01:00
where
2020-12-10 11:49:39 +01:00
swap i (OpNop v) = ins V.// [(i,OpJmp v)]
swap i (OpJmp v) = ins V.// [(i,OpNop v)]
swap _ (OpAcc _) = ins
2020-12-09 22:30:18 +01:00
2020-12-08 21:26:09 +01:00
main :: IO ()
main = do
putStrLn "Day 8 - Part 1"
2020-12-09 13:04:43 +01:00
putStrLn ":: Tests"
2020-12-09 22:30:18 +01:00
print exampleData
print $ parseOp <$> exampleData
2020-12-09 13:04:43 +01:00
putStrLn ":: Solving part 1"
2020-12-09 18:59:35 +01:00
pPrint $ solvePart1 (vmFromInput exampleData)
input <- lines <$> readFile "day8/input"
pPrint $ solvePart1 (vmFromInput input)
2020-12-09 22:30:18 +01:00
putStrLn ":: Tests"
let miniIns = V.fromList [OpJmp 2, OpNop 0, OpAcc (-8)]
pPrint $ length $ swapInstructions $ miniIns
2020-12-10 11:49:39 +01:00
let miniVM = initialVM $ V.fromList [ OpNop 0 ]
pPrint $ miniVM { vmState = VMHalt }
pPrint $ (unfoldr stepVM) $ miniVM
2020-12-09 22:30:18 +01:00
putStrLn ":: Solving part 2"
2020-12-10 11:49:39 +01:00
pPrint $ solvePart2 $ V.fromList $ parseOp <$> exampleData
pPrint $ solvePart2 $ V.fromList $ parseOp <$> input
2020-12-09 22:30:18 +01:00