This commit is contained in:
shu 2019-12-02 08:17:43 +01:00
parent ee975cb56a
commit 799a12675d
2 changed files with 28 additions and 0 deletions

27
2019/day2/day2.hs Normal file
View File

@ -0,0 +1,27 @@
import Data.List.Split
import Data.Sequence as S
import Data.Foldable
main = do
content <- readFile "input"
let input = (map (map read . splitOn ",") (lines content)) !! 0
let input2 = in_replace input 12 2
print (day2a input2 (fromList input2) 0)
print (day2b input)
in_replace input a b = (head input):a:b:(Prelude.drop 3 input)
day2a :: [Int] -> Seq Int -> Int -> Int
day2a (op:a:b:out:xs) acc n
| op==99=index acc 0
| otherwise=let newacc = update out ((index acc a)+*(index acc b)) acc in
day2a (toList $ S.drop m newacc) newacc (m)
where (+*) = if op==1 then (+) else (*)
m = n+4
day2a xs acc n = index acc 0
day2b :: [Int] -> Int
day2b input = sum [ let repl = in_replace input x y in
if day2a repl (fromList repl) 0 == 19690720 then 100*x+y
else 0 | x<-[0..99], y<-[0..99]]

1
2019/day2/input Normal file
View File

@ -0,0 +1 @@
1,0,0,3,1,1,2,3,1,3,4,3,1,5,0,3,2,10,1,19,1,6,19,23,2,23,6,27,1,5,27,31,1,31,9,35,2,10,35,39,1,5,39,43,2,43,10,47,1,47,6,51,2,51,6,55,2,55,13,59,2,6,59,63,1,63,5,67,1,6,67,71,2,71,9,75,1,6,75,79,2,13,79,83,1,9,83,87,1,87,13,91,2,91,10,95,1,6,95,99,1,99,13,103,1,13,103,107,2,107,10,111,1,9,111,115,1,115,10,119,1,5,119,123,1,6,123,127,1,10,127,131,1,2,131,135,1,135,10,0,99,2,14,0,0