Day 7: Shuffle around a bit
This commit is contained in:
parent
a627f553bb
commit
fa612e9eb6
|
@ -3,16 +3,19 @@ import qualified Data.List as L
|
||||||
import Data.Vector as V
|
import Data.Vector as V
|
||||||
import Data.Char
|
import Data.Char
|
||||||
|
|
||||||
|
data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show)
|
||||||
|
type Tape = Vector Int
|
||||||
|
type TapeSection = Vector Int
|
||||||
|
type TuringMachine = (Tape,Int)
|
||||||
|
|
||||||
main = do
|
main = do
|
||||||
content <- readFile "input"
|
content <- readFile "input"
|
||||||
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
|
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
|
||||||
print $ find_max tape [0..4]
|
print $ find_max tape [0..4]
|
||||||
print $ find_max tape [5..9]
|
print $ find_max tape [5..9]
|
||||||
|
|
||||||
data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show)
|
find_max :: Tape -> [Int] -> Int
|
||||||
type Tape = Vector Int
|
find_max tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range]
|
||||||
type TapeSection = Vector Int
|
|
||||||
type TuringMachine = (Tape,Int)
|
|
||||||
|
|
||||||
run_amps_feedback :: Tape -> [Int] -> Int
|
run_amps_feedback :: Tape -> [Int] -> Int
|
||||||
run_amps_feedback tape intseq = L.head $ run_amps_feedback' tms intseq [0]
|
run_amps_feedback tape intseq = L.head $ run_amps_feedback' tms intseq [0]
|
||||||
|
@ -26,9 +29,6 @@ run_amps_feedback' ((t,p):tms) intseq prev =
|
||||||
tailseq = if L.null intseq then [] else L.tail intseq
|
tailseq = if L.null intseq then [] else L.tail intseq
|
||||||
xprev = if L.null intseq then prev else (L.head intseq):prev
|
xprev = if L.null intseq then prev else (L.head intseq):prev
|
||||||
|
|
||||||
find_max :: Tape -> [Int] -> Int
|
|
||||||
find_max tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range]
|
|
||||||
|
|
||||||
opl :: Int -> Int
|
opl :: Int -> Int
|
||||||
opl x
|
opl x
|
||||||
| n`L.elem`"1278"=4
|
| n`L.elem`"1278"=4
|
||||||
|
@ -51,7 +51,6 @@ getopmodes opvec = (op_dedup,parsed_modes)
|
||||||
parsed_modes = parsemodes $ L.reverse modes
|
parsed_modes = parsemodes $ L.reverse modes
|
||||||
op_dedup = if L.last op == '0' then [L.head op] else op
|
op_dedup = if L.last op == '0' then [L.head op] else op
|
||||||
|
|
||||||
|
|
||||||
step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int])
|
step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int])
|
||||||
step opvec ((t,p),input,val)
|
step opvec ((t,p),input,val)
|
||||||
| op=="1" = (tm_binop (+),Continue,input,val)
|
| op=="1" = (tm_binop (+),Continue,input,val)
|
||||||
|
@ -70,7 +69,6 @@ step opvec ((t,p),input,val)
|
||||||
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
|
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
|
||||||
new_tm t x = ((t // [((V.last opvec), x)]),p)
|
new_tm t x = ((t // [((V.last opvec), x)]),p)
|
||||||
|
|
||||||
|
|
||||||
exec_steps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction)
|
exec_steps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction)
|
||||||
exec_steps ((t,p),input,output,halt) =
|
exec_steps ((t,p),input,output,halt) =
|
||||||
let command_length = opl $ t ! p
|
let command_length = opl $ t ! p
|
||||||
|
@ -79,5 +77,3 @@ exec_steps ((t,p),input,output,halt) =
|
||||||
step opvec ((t,p+command_length),input,output) in
|
step opvec ((t,p+command_length),input,output) in
|
||||||
if cond `L.elem` [Output, Halt] then ((t_new,p_new),input_new,output_new,cond)
|
if cond `L.elem` [Output, Halt] then ((t_new,p_new),input_new,output_new,cond)
|
||||||
else exec_steps ((t_new,p_new),input_new,output_new,cond)
|
else exec_steps ((t_new,p_new),input_new,output_new,cond)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user