trunk
HeNine 2 years ago
parent 96cf504c7a
commit b7367da63b

@ -260,7 +260,6 @@ test-suite day09basictest
Day9Lib
build-depends: base
, HUnit
, parsec
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
@ -270,8 +269,22 @@ test-suite day09aoctest
main-is: AoCTest.hs
other-modules:
Day9Lib
build-depends: base
, HUnit
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
-- Day 10
test-suite day10aoctest
type: exitcode-stdio-1.0
hs-source-dirs: src/day10/test, src/day10
main-is: AoCTest.hs
other-modules:
Day10Lib
build-depends: base
, HUnit
, parsec
, split
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010

@ -0,0 +1,146 @@
addx 15
addx -11
addx 6
addx -3
addx 5
addx -1
addx -8
addx 13
addx 4
noop
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx 5
addx -1
addx -35
addx 1
addx 24
addx -19
addx 1
addx 16
addx -11
noop
noop
addx 21
addx -15
noop
noop
addx -3
addx 9
addx 1
addx -3
addx 8
addx 1
addx 5
noop
noop
noop
noop
noop
addx -36
noop
addx 1
addx 7
noop
noop
noop
addx 2
addx 6
noop
noop
noop
noop
noop
addx 1
noop
noop
addx 7
addx 1
noop
addx -13
addx 13
addx 7
noop
addx 1
addx -33
noop
noop
noop
addx 2
noop
noop
noop
addx 8
noop
addx -1
addx 2
addx 1
noop
addx 17
addx -9
addx 1
addx 1
addx -3
addx 11
noop
noop
addx 1
noop
addx 1
noop
noop
addx -13
addx -19
addx 1
addx 3
addx 26
addx -30
addx 12
addx -1
addx 3
addx 1
noop
noop
noop
addx -9
addx 18
addx 1
addx 2
noop
noop
addx 9
noop
noop
noop
addx -1
addx 2
addx -37
addx 1
addx 3
noop
addx 15
addx -21
addx 22
addx -6
addx 1
noop
addx 2
addx 1
noop
addx -10
noop
noop
addx 20
addx 1
addx 2
addx 2
addx -6
addx -11
noop
noop
noop

@ -0,0 +1,145 @@
noop
noop
addx 5
addx 21
addx -16
noop
addx 1
noop
noop
addx 4
addx 1
addx 4
addx 1
noop
addx 4
addx -9
noop
addx 19
addx -5
noop
noop
addx 5
addx 1
addx -38
addx 5
addx -2
addx 2
noop
noop
addx 7
addx 9
addx 20
addx -3
addx -18
addx 2
addx 5
noop
noop
addx -2
noop
noop
addx 7
addx 3
addx -2
addx 2
addx -28
addx -7
addx 5
noop
addx 2
addx 32
addx -27
noop
noop
noop
noop
noop
addx 7
noop
addx 22
addx -19
noop
addx 5
noop
addx -7
addx 17
addx -7
noop
addx -20
addx 27
noop
addx -16
addx -20
addx 1
noop
addx 3
addx 15
addx -8
addx -2
addx -6
addx 14
addx 4
noop
noop
addx -17
addx 22
noop
addx 5
noop
noop
noop
addx 2
noop
addx 3
addx -32
addx -5
noop
addx 4
addx 3
addx -2
addx 34
addx -27
addx 5
addx 16
addx -18
addx 7
noop
addx -2
addx -1
addx 8
addx 14
addx -9
noop
addx -15
addx 16
addx 2
addx -35
noop
noop
noop
noop
addx 3
addx 4
noop
addx 1
addx 4
addx 1
noop
addx 4
addx 2
addx 3
addx -5
addx 19
addx -9
addx 2
addx 4
noop
noop
noop
noop
addx 3
addx 2
noop
noop
noop

@ -1,9 +1,6 @@
{-# LANGUAGE InstanceSigs #-}
module Day9Lib
( Rope (..),
newRope,
-- move,
follow,
--
processInput1,
@ -17,22 +14,22 @@ import Debug.Trace
newtype Vector = Vector (Int, Int) deriving (Show, Eq)
instance Num Vector where
(+) :: Vector -> Vector -> Vector
Vector v1 + Vector v2 = Vector (fst v1 + fst v2, snd v1 + snd v2)
(-) :: Vector -> Vector -> Vector
Vector v1 - Vector v2 = Vector (fst v1 - fst v2, snd v1 - snd v2)
(*) :: Vector -> Vector -> Vector
Vector v1 * Vector v2 = Vector (fst v1 * fst v2, snd v1 * snd v2)
fromInteger :: Integer -> Vector
fromInteger i = Vector (fromInteger i, fromInteger i)
abs :: Vector -> Vector
abs (Vector v) = Vector (abs . fst $ v, abs . snd $ v)
signum :: Vector -> Vector
signum (Vector (x, y)) = Vector (signum x, signum y)
newtype Rope = Rope [Vector] deriving (Show, Eq)
infix 7 .*
(.*) :: Int -> Vector -> Vector
m .* v = (fromInteger . toInteger) m * v

@ -0,0 +1,58 @@
module Day10Lib
( --
processInput1,
processInput2,
)
where
import Data.List
import Data.List.Split
import Debug.Trace
newtype CPU = CPU (Int, Int) deriving (Show, Eq)
data Instruction = AddX Int | Noop deriving (Show, Eq)
processInstruction :: Instruction -> CPU -> CPU
processInstruction (AddX n) (CPU (a, c)) = CPU (a + n, c + 2)
processInstruction Noop (CPU (a, c)) = CPU (a, c + 1)
parseInstruction :: String -> Instruction
parseInstruction ins = case words ins of
["addx", ns] -> AddX (read ns)
["noop"] -> Noop
_ -> error $ "illegal instruction: " ++ ins
every :: Int -> [a] -> [a]
every n xs = case drop (n - 1) xs of
y : ys -> y : every n ys
[] -> []
-- ####################
processLine :: (CPU, [Int]) -> String -> (CPU, [Int])
processLine (cpu@(CPU (a, _)), hist) inss =
let ins = parseInstruction inss
newcpu = processInstruction ins cpu
in (newcpu, hist ++ replicate (if ins == Noop then 1 else 2) a)
-- ####################
processInput1 :: String -> Int
processInput1 =
sum
. zipWith (*) [20, 60 ..]
. every 40
. (++) (replicate 20 0)
. snd
. foldl processLine (CPU (1, 0), [])
. lines
processInput2 :: String -> String
processInput2 =
intercalate "\n"
. map (zipWith (\x y -> if abs ((y + 1) - x) <= 1 then '#' else '.') [1 ..])
. chunksOf 40
. snd
. foldl processLine (CPU (1, 0), [])
. lines

@ -0,0 +1,55 @@
import Day10Lib (processInput1, processInput2)
import System.IO
import Test.HUnit
testCases1 =
[ ("data/input100.txt", 13140),
("data/input101.txt", 17020)
]
testCase1 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual "input test" result $ processInput1 contents
)
testCases2 =
[ ( "data/input100.txt",
"##..##..##..##..##..##..##..##..##..##..\n\
\###...###...###...###...###...###...###.\n\
\####....####....####....####....####....\n\
\#####.....#####.....#####.....#####.....\n\
\######......######......######......####\n\
\#######.......#######.......#######....."
),
( "data/input101.txt",
"###..#....####.####.####.#.....##..####.\n\
\#..#.#....#.......#.#....#....#..#.#....\n\
\#..#.#....###....#..###..#....#....###..\n\
\###..#....#.....#...#....#....#.##.#....\n\
\#.#..#....#....#....#....#....#..#.#....\n\
\#..#.####.####.####.#....####..###.####." -- RLEZFLGE
)
]
testCase2 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual ("input test: " ++ file) result $ processInput2 contents
)
tests =
TestList $
[TestCase (testCase1 c) | c <- testCases1]
++ [TestCase (testCase2 c) | c <- testCases2]
main :: IO ()
main = do
runTestTT tests
return ()
Loading…
Cancel
Save