Day 07 solution
This commit is contained in:
parent
75639dcf20
commit
5d49c6428f
|
@ -0,0 +1,2 @@
|
|||
.stack-work/
|
||||
*~
|
|
@ -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
|
File diff suppressed because it is too large
Load Diff
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1,6 @@
|
|||
resolver: lts-21.21
|
||||
|
||||
packages:
|
||||
- .
|
||||
|
||||
extra-deps: []
|
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
|
@ -0,0 +1,2 @@
|
|||
.stack-work/
|
||||
*~
|
|
@ -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
|
File diff suppressed because it is too large
Load Diff
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1,6 @@
|
|||
resolver: lts-21.21
|
||||
|
||||
packages:
|
||||
- .
|
||||
|
||||
extra-deps: []
|
|
@ -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
|
|
@ -0,0 +1,5 @@
|
|||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
3
run.sh
3
run.sh
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue