Day 07 solution

This commit is contained in:
Natty 2023-12-08 04:08:48 +01:00
parent 75639dcf20
commit 5d49c6428f
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
18 changed files with 2264 additions and 2 deletions

2
day07a/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

28
day07a/day07a.cabal Normal file
View File

@ -0,0 +1,28 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack
name: day07a
version: 0.1.0.0
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
author: Natty
maintainer: natty.sh.git@gmail.com
copyright: 2023 Natty
build-type: Simple
executable day07a
main-is: Main.hs
other-modules:
Paths_day07a
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, extra
, megaparsec
, text
default-language: Haskell2010

1000
day07a/input.txt generated Normal file

File diff suppressed because it is too large Load Diff

34
day07a/package.yaml Normal file
View File

@ -0,0 +1,34 @@
name: day07a
version: 0.1.0.0
author: "Natty"
maintainer: "natty.sh.git@gmail.com"
copyright: "2023 Natty"
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
dependencies:
- base >= 4.7 && < 5
- megaparsec
- containers
- text
- extra
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
executables:
day07a:
main: Main.hs
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N

39
day07a/src/Main.hs Normal file
View File

@ -0,0 +1,39 @@
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main (main) where
import qualified Data.Text as T
import Data.List ( elemIndex, sort, sortBy, group )
import Data.Maybe (fromJust)
import GHC.Num (integerFromInt)
import Data.Function (on)
import Data.Ord (comparing, Down (Down))
cardStrength :: Char -> Integer
cardStrength = integerFromInt . fromJust . flip elemIndex (reverse "AKQJT98765432")
countTypes :: [Char] -> [Int]
countTypes = sortBy (comparing Down) . (length <$>) . group . sort
handStrength :: [Char] -> Integer
handStrength cc = case countTypes cc of
[5] -> 6 -- 5 of a kind
[4, 1] -> 5 -- 4 of a kind
[3, 2] -> 4 -- full house
[3, 1, 1] -> 3 -- 3 of a kind
[2, 2, 1] -> 2 -- two pairs
[2, 1, 1, 1] -> 1 -- a pair
[1, 1, 1, 1, 1] -> 0 -- nothing
_ -> error "Unreachable"
parseHand :: [T.Text] -> ([Char], Integer)
parseHand [T.unpack -> hand, read . T.unpack -> bid :: Integer] = (hand, bid)
parseHand _ = error "Parse error"
cmpHands :: [Char] -> [Char] -> Ordering
cmpHands = comparing handStrength <> comparing (fmap cardStrength)
main :: IO ()
main = do
hands <- (parseHand . T.words . T.pack <$>) . lines <$> getContents
print $ sum . zipWith (*) [1..] $ snd <$> sortBy (cmpHands `on` fst) hands

6
day07a/stack.yaml Normal file
View File

@ -0,0 +1,6 @@
resolver: lts-21.21
packages:
- .
extra-deps: []

12
day07a/stack.yaml.lock Normal file
View File

@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages: []
snapshots:
- completed:
sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186
size: 640060
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml
original: lts-21.21

5
day07a/test.txt generated Normal file
View File

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

2
day07b/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.stack-work/
*~

27
day07b/day07b.cabal Normal file
View File

@ -0,0 +1,27 @@
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack
name: day07b
version: 0.1.0.0
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
author: Natty
maintainer: natty.sh.git@gmail.com
copyright: 2023 Natty
build-type: Simple
executable day07b
main-is: Main.hs
other-modules:
Paths_day07b
hs-source-dirs:
src
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, containers
, megaparsec
, text
default-language: Haskell2010

1000
day07b/input.txt generated Normal file

File diff suppressed because it is too large Load Diff

33
day07b/package.yaml Normal file
View File

@ -0,0 +1,33 @@
name: day07b
version: 0.1.0.0
author: "Natty"
maintainer: "natty.sh.git@gmail.com"
copyright: "2023 Natty"
description: Please see the README at <https://git.astolfo.cool/natty/aoc23#readme>
dependencies:
- base >= 4.7 && < 5
- megaparsec
- containers
- text
ghc-options:
- -Wall
- -Wcompat
- -Widentities
- -Wincomplete-record-updates
- -Wincomplete-uni-patterns
- -Wmissing-export-lists
- -Wmissing-home-modules
- -Wpartial-fields
- -Wredundant-constraints
executables:
day07b:
main: Main.hs
source-dirs: src
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N

49
day07b/src/Main.hs Normal file
View File

@ -0,0 +1,49 @@
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main (main) where
import qualified Data.Text as T
import Data.List ( elemIndex, sort, sortBy, group, maximumBy )
import Data.Maybe (fromJust)
import GHC.Num (integerFromInt)
import Data.Function (on)
import Data.Ord (comparing, Down (Down))
cardStrength :: Char -> Integer
cardStrength = integerFromInt . fromJust . flip elemIndex (reverse "AKQT98765432J")
countTypes :: [Char] -> [Int]
countTypes = sortBy (comparing Down) . (length <$>) . group . sort
handStrength :: [Char] -> Integer
handStrength cc = case countTypes cc of
[5] -> 6 -- 5 of a kind
[4, 1] -> 5 -- 4 of a kind
[3, 2] -> 4 -- full house
[3, 1, 1] -> 3 -- 3 of a kind
[2, 2, 1] -> 2 -- two pairs
[2, 1, 1, 1] -> 1 -- a pair
[1, 1, 1, 1, 1] -> 0 -- nothing
_ -> error "Unreachable"
replaceJokers :: Char -> [Char] -> [Char]
replaceJokers c = fmap (\cc -> if cc == 'J' then c else cc)
maxJokerHand :: [Char] -> Char
maxJokerHand hand = maximumBy (compare `on` handStrength . flip replaceJokers hand) "AKQJT98765432"
maximizeHand :: [Char] -> [Char]
maximizeHand = replaceJokers =<< maxJokerHand
parseHand :: [T.Text] -> ([Char], Integer)
parseHand [T.unpack -> hand, read . T.unpack -> bid :: Integer] = (hand, bid)
parseHand _ = error "Parse error"
cmpHands :: [Char] -> [Char] -> Ordering
cmpHands = comparing (handStrength . maximizeHand)
<> comparing (fmap cardStrength)
main :: IO ()
main = do
hands <- (parseHand . T.words . T.pack <$>) . lines <$> getContents
print $ sum . zipWith (*) [1..] $ snd <$> sortBy (cmpHands `on` fst) hands

6
day07b/stack.yaml Normal file
View File

@ -0,0 +1,6 @@
resolver: lts-21.21
packages:
- .
extra-deps: []

12
day07b/stack.yaml.lock Normal file
View File

@ -0,0 +1,12 @@
# This file was autogenerated by Stack.
# You should not edit this file by hand.
# For more information, please see the documentation at:
# https://docs.haskellstack.org/en/stable/lock_files
packages: []
snapshots:
- completed:
sha256: 7d4b649cf368f9076d8aa049aa44efe58950971d105892734e9957b2a26a2186
size: 640060
url: https://raw.githubusercontent.com/commercialhaskell/stackage-snapshots/master/lts/21/21.yaml
original: lts-21.21

5
day07b/test.txt generated Normal file
View File

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

3
run.sh
View File

@ -25,7 +25,8 @@ case "$1" in
"day02a" | "day02b" | \
"day03a" | "day03b" | \
"day04a" | "day04b" | \
"day06a" | "day06b" ) stack run <input.txt
"day06a" | "day06b" | \
"day07a" | "day07b" ) stack run <input.txt
;;
# Default solutions -- Java
*) ./gradlew run <input.txt

View File

@ -25,7 +25,8 @@ case "$1" in
"day02a" | "day02b" | \
"day03a" | "day03b" | \
"day04a" | "day04b" | \
"day06a" | "day06b") stack run <test.txt
"day06a" | "day06b" | \
"day07a" | "day07b" ) stack run <test.txt
;;
# Default solutions -- Java
*) ./gradlew run <test.txt