trunk
HeNine 2 years ago
parent 8e98a80ff0
commit c3d62c3c5e

@ -0,0 +1,106 @@
name: aoc2022
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://git.raptorpond.com/henine/AoC2022
license: MIT
license-file: LICENSE
author: henine
maintainer: example@example.com
copyright: 2022 HeNine
category: Misc
build-type: Simple
cabal-version: >=1.10
-- Day 1
test-suite day01aoctest
type: exitcode-stdio-1.0
hs-source-dirs: src/day01/test, src/day01
main-is: AoCTest.hs
other-modules: Day1Lib
build-depends: base
, HUnit
, split
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
executable day011
hs-source-dirs: src/day01
main-is: Main1.hs
other-modules:
Day1Lib
default-language: Haskell2010
build-depends: base >= 4.7 && < 5, split
ghc-options: -Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints
executable day012
hs-source-dirs: src/day01
main-is: Main2.hs
other-modules:
Day1Lib
default-language: Haskell2010
build-depends: base >= 4.7 && < 5, split
ghc-options: -Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints
-- Day 2
test-suite day02aoctest
type: exitcode-stdio-1.0
hs-source-dirs: src/day02/test, src/day02
main-is: AoCTest.hs
other-modules: Day2Lib
build-depends: base
, HUnit
ghc-options: -threaded -rtsopts -with-rtsopts=-N
default-language: Haskell2010
executable day021
hs-source-dirs: src/day02
main-is: Main1.hs
other-modules:
Day2Lib
default-language: Haskell2010
build-depends: base >= 4.7 && < 5, split
ghc-options: -Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints
executable day022
hs-source-dirs: src/day02
main-is: Main2.hs
other-modules:
Day2Lib
default-language: Haskell2010
build-depends: base >= 4.7 && < 5, split
ghc-options: -Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints

@ -0,0 +1,3 @@
A Y
B X
C Z

File diff suppressed because it is too large Load Diff

@ -1,48 +0,0 @@
name: day01
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/githubuser/day01#readme
license: MIT
license-file: ../LICENSE
author: Author name here
maintainer: example@example.com
copyright: 2022 Author name here
category: Web
build-type: Simple
cabal-version: >=1.10
executable day011
hs-source-dirs: src
main-is: Main1.hs
other-modules:
Lib
default-language: Haskell2010
build-depends: base >= 4.7 && < 5, split
ghc-options: -Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints
executable day012
hs-source-dirs: src
main-is: Main2.hs
other-modules:
Lib
default-language: Haskell2010
build-depends: base >= 4.7 && < 5, split
ghc-options: -Wall
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wmissing-export-lists
-Wmissing-home-modules
-Wpartial-fields
-Wredundant-constraints

@ -1,6 +0,0 @@
module Lib (parseBackpacks) where
import Data.List.Split
parseBackpacks :: String -> [[Int]]
parseBackpacks input = [map read b | b <- splitOn [""] $ lines input]

@ -1,14 +0,0 @@
module Main (main) where
-- import Debug.Trace
import Lib
main :: IO ()
main = do
input <- getContents
let backpacks = parseBackpacks input
max_backpack = maximum [sum b | b <- backpacks]
print $ max_backpack
return ()

@ -1,14 +0,0 @@
module Main (main) where
-- import Debug.Trace
import Data.List (sort)
import Lib
main :: IO ()
main = do
input <- getContents
let backpacks = parseBackpacks input
top_3 = sum $ take 3 $ reverse $ sort [sum b | b <- backpacks]
print $ top_3
return ()

@ -0,0 +1,15 @@
module Day1Lib (parseBackpacks, processInput1, processInput2) where
import Data.List (sort)
import Data.List.Split
parseBackpacks :: String -> [[Int]]
parseBackpacks input = [map read b | b <- splitOn [""] $ lines input]
-- ################
processInput1 :: String -> Int
processInput1 = maximum . (map sum) . parseBackpacks
processInput2 :: String -> Int
processInput2 = sum . (take 3) . reverse . sort . (map sum) . parseBackpacks

@ -0,0 +1,12 @@
module Main (main) where
-- import Debug.Trace
import Day1Lib
main :: IO ()
main = do
input <- getContents
print $ processInput1 input
return ()

@ -0,0 +1,11 @@
module Main (main) where
-- import Debug.Trace
import Day1Lib (processInput2)
main :: IO ()
main = do
input <- getContents
print $ processInput2 input
return ()

@ -0,0 +1,42 @@
import System.IO
import Test.HUnit
import Day1Lib (processInput1, processInput2)
testCases1 =
[ ("data/input010.txt", 24000),
("data/input011.txt", 69912)
]
testCase1 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual "input test" result $ processInput1 contents
)
testCases2 =
[ ("data/input010.txt", 45000),
("data/input011.txt", 208180)
]
testCase2 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual "input test" result $ processInput2 contents
)
tests =
TestList $
[TestCase (testCase1 c) | c <- testCases1]
++ [TestCase (testCase2 c) | c <- testCases2]
main :: IO ()
main = do
runTestTT tests
return ()

@ -0,0 +1,61 @@
module Day2Lib
( Opponent (..),
Me (..),
Trick (..),
Hint (..),
assembleNaiveTrick,
evaluateTrick,
evaluateWin,
evaluateMe,
parseHint,
assembleTrick,
--
processInput1,
processInput2,
)
where
data Opponent = A | B | C deriving (Show, Read, Eq, Enum)
data Me = X | Y | Z deriving (Show, Read, Eq, Enum)
data Hand = R | P | S deriving (Show, Eq, Enum)
newtype Hint = Hint (Opponent, Me) deriving (Show, Eq)
newtype Trick = Trick (Hand, Hand) deriving (Show, Eq)
parseHint :: String -> (Opponent, Me)
parseHint (o : _ : m : []) = (read [o], read [m])
opponentHand :: Opponent -> Hand
opponentHand = toEnum . fromEnum
naiveResponse :: Hand -> Me -> Hand
naiveResponse _ m = toEnum $ fromEnum m
findResponse :: Hand -> Me -> Hand
findResponse o m = toEnum ((fromEnum o + fromEnum m - 1) `mod` 3)
assembleTrick :: Hint -> Trick
assembleTrick (Hint (o, m)) = Trick (opponentHand o, findResponse (opponentHand o) m)
assembleNaiveTrick :: Hint -> Trick
assembleNaiveTrick (Hint (o, m)) = Trick (opponentHand o, naiveResponse (opponentHand o) m)
evaluateTrick :: Trick -> Int
evaluateTrick t = evaluateWin t + evaluateMe t
evaluateMe :: Trick -> Int
evaluateMe (Trick (_, m)) = fromEnum m + 1
evaluateWin :: Trick -> Int
evaluateWin (Trick (o, m)) = 6 - (fromEnum o - fromEnum m + 1) `mod` 3 * 3
-- ######################################
processInput1 :: String -> Int
processInput1 = sum . (map $ evaluateTrick . assembleNaiveTrick . Hint . parseHint) . lines
processInput2 :: String -> Int
processInput2 = sum . (map $ evaluateTrick . assembleTrick . Hint . parseHint) . lines

@ -0,0 +1,10 @@
module Main (main) where
import Day2Lib
main :: IO ()
main = do
input <- getContents
print $ processInput1 input
return ()

@ -0,0 +1,10 @@
module Main (main) where
import Day2Lib (processInput2)
main :: IO ()
main = do
input <- getContents
print $ processInput2 input
return ()

@ -0,0 +1,42 @@
import System.IO
import Test.HUnit
import Day2Lib (processInput1, processInput2)
testCases1 =
[ ("data/input020.txt", 15),
("data/input021.txt", 14264)
]
testCase1 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual "input test" result $ processInput1 contents
)
testCases2 =
[ ("data/input020.txt", 12),
("data/input021.txt", 12382)
]
testCase2 (file, result) = do
withFile
file
ReadMode
( \handle -> do
contents <- hGetContents handle
assertEqual "input test" 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