Part 1
This commit is contained in:
parent
2ee9fa16c4
commit
f0c7f30103
2 changed files with 26 additions and 7 deletions
27
day8/main.hs
27
day8/main.hs
|
@ -1,8 +1,10 @@
|
||||||
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
||||||
#! nix-shell -p ghcid
|
#! nix-shell -p ghcid
|
||||||
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple vector containers])"
|
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple vector containers])"
|
||||||
#! nix-shell -i "ghcid -c 'ghci -Wall' -T main"
|
#! nix-shell -i "ghcid -c 'ghci' -T main"
|
||||||
|
|
||||||
|
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
|
||||||
|
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
import Text.Pretty.Simple
|
import Text.Pretty.Simple
|
||||||
|
@ -11,7 +13,9 @@ import Text.Pretty.Simple
|
||||||
import Data.Vector (Vector)
|
import Data.Vector (Vector)
|
||||||
import Debug.Trace (trace)
|
import Debug.Trace (trace)
|
||||||
import Data.List (unfoldr)
|
import Data.List (unfoldr)
|
||||||
|
import Data.IntMap.Strict (IntMap)
|
||||||
import qualified Data.Vector as V
|
import qualified Data.Vector as V
|
||||||
|
import qualified Data.IntMap.Strict as M
|
||||||
|
|
||||||
data Op = OpNop
|
data Op = OpNop
|
||||||
| OpJmp !Int
|
| OpJmp !Int
|
||||||
|
@ -66,16 +70,29 @@ vmFromInput input = initialVM (ins input)
|
||||||
|
|
||||||
-- Find smallest looping prefix in list of Int, returns associated pgmAccumulator
|
-- Find smallest looping prefix in list of Int, returns associated pgmAccumulator
|
||||||
solvePart1 :: VirtualMachine -> Int
|
solvePart1 :: VirtualMachine -> Int
|
||||||
solvePart1 vm = undefined vm
|
solvePart1 vm0 =
|
||||||
|
snd . last $ shortestCycleOn $ (\vm -> (pgmCounter vm, pgmAccumulator vm)) <$> unfoldr stepVM vm0
|
||||||
|
|
||||||
-- Shortest cycle in [Int]
|
-- Shortest cycle in [Int]
|
||||||
shortestCycle :: [Int]
|
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
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
putStrLn "Day 8 - Part 1"
|
putStrLn "Day 8 - Part 1"
|
||||||
putStrLn ":: Tests"
|
putStrLn ":: Tests"
|
||||||
pPrint exampleData
|
pPrint exampleData
|
||||||
print $ take 10 $ (\vm -> (pgmCounter vm, pgmAccumulator vm)) <$> unfoldr stepVM (vmFromInput exampleData)
|
|
||||||
putStrLn ":: Solving part 1"
|
putStrLn ":: Solving part 1"
|
||||||
print $ solvePart1 (vmFromInput exampleData)
|
pPrint $ solvePart1 (vmFromInput exampleData)
|
||||||
|
input <- lines <$> readFile "day8/input"
|
||||||
|
pPrint $ solvePart1 (vmFromInput input)
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
||||||
#! nix-shell -p ghcid
|
#! nix-shell -p ghcid
|
||||||
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [shower])"
|
#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [])"
|
||||||
#! nix-shell -i "ghcid -c 'ghci -Wall' -T main"
|
#! nix-shell -i "ghcid -c 'ghci' -T main"
|
||||||
|
|
||||||
|
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
|
||||||
|
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
|
|
Loading…
Reference in a new issue