Day 9: Some more cosmetics
This commit is contained in:
parent
bef16465d7
commit
90a65ce187
|
@ -1,6 +1,7 @@
|
||||||
import Data.List.Split
|
import Data.List.Split
|
||||||
import qualified Data.List as L
|
import Data.List as L
|
||||||
import Data.Vector as V
|
import Data.Vector as V hiding ((++), concatMap, map, elem, last, head, length, splitAt, reverse)
|
||||||
|
import qualified Data.Vector as V ((++), last)
|
||||||
import Data.Char
|
import Data.Char
|
||||||
import Control.DeepSeq as DeepSeq
|
import Control.DeepSeq as DeepSeq
|
||||||
|
|
||||||
|
@ -12,24 +13,27 @@ type TuringMachine = (Tape,Integer,Integer)
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
content <- readFile "input"
|
content <- readFile "input"
|
||||||
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
|
let tape = fromList $ concatMap (map read . splitOn ",") (lines content)
|
||||||
let (tm,_,out,_) = execSteps ((tapePreprocess tape,0,0),[2],[],Continue)
|
let run x = execSteps ((tapePreprocess tape,0,0),[x],[],Continue)
|
||||||
print $ L.reverse out
|
let (_,_,out1,_) = run 1
|
||||||
|
let (_,_,out2,_) = run 2
|
||||||
|
print $ L.concat ["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a<-out1,b<-out2]
|
||||||
|
|
||||||
tapePreprocess :: TapeSection -> TapeSection
|
tapePreprocess :: TapeSection -> TapeSection
|
||||||
tapePreprocess t = (V.++) t $ V.replicate 150 0
|
tapePreprocess t = (V.++) t $ V.replicate 105 0
|
||||||
|
|
||||||
|
|
||||||
opLength :: Integer -> Integer
|
opLength :: Integer -> Integer
|
||||||
opLength x
|
opLength x
|
||||||
| n`L.elem`"1278"=4
|
| n`elem`"1278"=4
|
||||||
| n`L.elem`"56"=3
|
| n`elem`"56"=3
|
||||||
| n`L.elem`"349"=2
|
| n`elem`"349"=2
|
||||||
| otherwise=1
|
| otherwise=1
|
||||||
where n = L.last $ show x
|
where n = last $ show x
|
||||||
|
|
||||||
parseModes :: String -> [Mode]
|
parseModes :: String -> [Mode]
|
||||||
parseModes m = L.replicate (3 - L.length l) Position L.++ l
|
parseModes m = L.replicate (3 - length l) Position ++ l
|
||||||
where l = L.map (toEnum . digitToInt) m
|
where l = map (toEnum . digitToInt) m
|
||||||
|
|
||||||
paramChange :: [Mode] -> Integer -> TapeSection -> Tape -> TapeSection
|
paramChange :: [Mode] -> Integer -> TapeSection -> Tape -> TapeSection
|
||||||
paramChange m rbase opvec t = imap f (V.tail opvec)
|
paramChange m rbase opvec t = imap f (V.tail opvec)
|
||||||
|
@ -40,31 +44,30 @@ paramChange m rbase opvec t = imap f (V.tail opvec)
|
||||||
|
|
||||||
getOpModes :: TapeSection -> (String, [Mode])
|
getOpModes :: TapeSection -> (String, [Mode])
|
||||||
getOpModes opvec = (op_dedup,parsed_modes)
|
getOpModes opvec = (op_dedup,parsed_modes)
|
||||||
where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0
|
where (op,modes) = splitAt 2 $ reverse $ show $ opvec ! 0
|
||||||
parsed_modes = L.reverse $ parseModes $ L.reverse modes
|
parsed_modes = reverse $ parseModes $ reverse modes
|
||||||
op_dedup = if L.last op == '0' then [L.head op] else op
|
op_dedup = if last op == '0' then [head op] else op
|
||||||
|
|
||||||
step :: TapeSection -> (TuringMachine, [Integer], [Integer]) -> (TuringMachine, OutAction, [Integer], [Integer])
|
step :: TapeSection -> (TuringMachine, [Integer], [Integer]) -> (TuringMachine, OutAction, [Integer], [Integer])
|
||||||
step opvec ((t,p,rbase),input,val) = case op of
|
step opvec ((t,p,rbase),input,output) = case op of
|
||||||
"1" -> (tm_binop (+),Continue,input,val)
|
"1" -> (tm_binop (+),Continue,input,output)
|
||||||
"2" -> (tm_binop (*),Continue,input,val)
|
"2" -> (tm_binop (*),Continue,input,output)
|
||||||
"3" -> (new_tm t $ L.head input,Continue,L.tail input,val)
|
"3" -> (new_tm t $ head input,Continue,L.tail input,output)
|
||||||
"4" -> ((t,p,rbase),Output,input,V.last params:val)
|
"4" -> ((t,p,rbase),Output,input,V.last params:output)
|
||||||
"5" -> ((t, if params ! 0/=0 then params ! 1 else p,rbase),Continue,input,val)
|
"5" -> ((t, if params ! 0/=0 then params ! 1 else p,rbase),Continue,input,output)
|
||||||
"6" -> ((t, if params ! 0==0 then params ! 1 else p,rbase),Continue,input,val)
|
"6" -> ((t, if params ! 0==0 then params ! 1 else p,rbase),Continue,input,output)
|
||||||
"7" -> (new_tm t (if (params ! 0)<(params ! 1)
|
"7" -> (tm_binop (\x y->if x<y then 1 else 0),Continue,input,output)
|
||||||
then 1 else 0),Continue,input,val)
|
"8" -> (tm_binop (\x y->if x==y then 1 else 0),Continue,input,output)
|
||||||
"8" -> (new_tm t (if (params ! 0)==(params ! 1)
|
"9" -> ((t,p,rbase + (params ! 0)),Continue,input,output)
|
||||||
then 1 else 0),Continue,input,val)
|
"99" -> ((t,p,rbase),Halt,input,output)
|
||||||
"9" -> ((t,p,rbase + (params ! 0)),Continue,input,val)
|
|
||||||
"99" -> ((t,p,rbase),Halt,input,val)
|
|
||||||
where (op,m) = getOpModes opvec
|
where (op,m) = getOpModes opvec
|
||||||
params = paramChange m rbase opvec t
|
params = paramChange m rbase opvec t
|
||||||
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
|
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
|
||||||
--without the following DeepSeq call, thunks build up eternally
|
{-without the following DeepSeq call, thunks build up eternally
|
||||||
--and the vectors won’t be garbage collected (>4GB RAM usage)
|
and the vectors won’t be garbage collected: >4GB RAM usage,
|
||||||
|
god knows how much with large vectors, now ~20mB-}
|
||||||
new_tm t x = DeepSeq.force (t // [(target, x)],p,rbase)
|
new_tm t x = DeepSeq.force (t // [(target, x)],p,rbase)
|
||||||
target = fromInteger $ case m !! (L.length params -1) of
|
target = fromInteger $ case m !! (length params -1) of
|
||||||
Relative -> V.last opvec + rbase
|
Relative -> V.last opvec + rbase
|
||||||
_ -> V.last opvec
|
_ -> V.last opvec
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user