Day 06 solution
This commit is contained in:
parent
5ffe6aea7a
commit
4ffa71e1cc
|
@ -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: day06a
|
||||||
|
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 day06a
|
||||||
|
main-is: Main.hs
|
||||||
|
other-modules:
|
||||||
|
Paths_day06a
|
||||||
|
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
|
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 58 99 64 69
|
||||||
|
Distance: 478 2232 1019 1071
|
|
@ -0,0 +1,33 @@
|
||||||
|
name: day06a
|
||||||
|
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:
|
||||||
|
day06a:
|
||||||
|
main: Main.hs
|
||||||
|
source-dirs: src
|
||||||
|
ghc-options:
|
||||||
|
- -threaded
|
||||||
|
- -rtsopts
|
||||||
|
- -with-rtsopts=-N
|
|
@ -0,0 +1,26 @@
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
|
{-# LANGUAGE TypeApplications #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
|
||||||
|
module Main (main) where
|
||||||
|
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import Data.List ( transpose )
|
||||||
|
import Data.Bifunctor (Bifunctor(bimap))
|
||||||
|
|
||||||
|
parseRace :: [T.Text] -> (Integer, Integer)
|
||||||
|
parseRace [read @Integer . T.unpack -> time, read @Integer . T.unpack -> dist] = (time, dist)
|
||||||
|
parseRace _ = error "Parse"
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
lns <- lines <$> getContents
|
||||||
|
print
|
||||||
|
$ product
|
||||||
|
$ (\(time, dist) -> (\x -> floor (time / 2 + x) - ceiling (time / 2 - x) + 1) . subtract 0.000001 . sqrt . subtract dist . (**2) . (/2) $ time)
|
||||||
|
<$> ((bimap (fromIntegral @_ @Double) (fromIntegral @_ @Double)
|
||||||
|
. parseRace <$>)
|
||||||
|
. drop 1
|
||||||
|
. transpose
|
||||||
|
$ T.words . T.pack <$> lns)
|
|
@ -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,2 @@
|
||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
|
@ -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: day06b
|
||||||
|
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 day06b
|
||||||
|
main-is: Main.hs
|
||||||
|
other-modules:
|
||||||
|
Paths_day06b
|
||||||
|
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
|
||||||
|
, bifunctors
|
||||||
|
, containers
|
||||||
|
, megaparsec
|
||||||
|
, text
|
||||||
|
default-language: Haskell2010
|
|
@ -0,0 +1,2 @@
|
||||||
|
Time: 58 99 64 69
|
||||||
|
Distance: 478 2232 1019 1071
|
|
@ -0,0 +1,34 @@
|
||||||
|
name: day06b
|
||||||
|
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
|
||||||
|
- bifunctors
|
||||||
|
|
||||||
|
ghc-options:
|
||||||
|
- -Wall
|
||||||
|
- -Wcompat
|
||||||
|
- -Widentities
|
||||||
|
- -Wincomplete-record-updates
|
||||||
|
- -Wincomplete-uni-patterns
|
||||||
|
- -Wmissing-export-lists
|
||||||
|
- -Wmissing-home-modules
|
||||||
|
- -Wpartial-fields
|
||||||
|
- -Wredundant-constraints
|
||||||
|
|
||||||
|
executables:
|
||||||
|
day06b:
|
||||||
|
main: Main.hs
|
||||||
|
source-dirs: src
|
||||||
|
ghc-options:
|
||||||
|
- -threaded
|
||||||
|
- -rtsopts
|
||||||
|
- -with-rtsopts=-N
|
|
@ -0,0 +1,28 @@
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE ViewPatterns #-}
|
||||||
|
{-# LANGUAGE TypeApplications #-}
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
|
||||||
|
module Main (main) where
|
||||||
|
|
||||||
|
import qualified Data.Text as T
|
||||||
|
import Data.List ( transpose )
|
||||||
|
import Data.Bifunctor (Bifunctor(bimap))
|
||||||
|
|
||||||
|
parseRace :: [T.Text] -> (Integer, Integer)
|
||||||
|
parseRace [read @Integer . T.unpack -> time, read @Integer . T.unpack -> dist] = (time, dist)
|
||||||
|
parseRace _ = error "Parse"
|
||||||
|
|
||||||
|
main :: IO ()
|
||||||
|
main = do
|
||||||
|
lns <- lines <$> getContents
|
||||||
|
print
|
||||||
|
$ (\(time, dist) -> (\x -> floor (time / 2 + x) - ceiling (time / 2 - x) + 1)
|
||||||
|
. subtract 0.000001
|
||||||
|
. sqrt
|
||||||
|
. subtract dist
|
||||||
|
. (**2)
|
||||||
|
. (/2) $ time)
|
||||||
|
. bimap (fromIntegral @_ @Double) (fromIntegral @_ @Double)
|
||||||
|
$ parseRace
|
||||||
|
$ T.concat <$> (transpose . drop 1 . transpose $ T.words . T.pack <$> lns)
|
|
@ -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,2 @@
|
||||||
|
Time: 7 15 30
|
||||||
|
Distance: 9 40 200
|
3
run.sh
3
run.sh
|
@ -24,7 +24,8 @@ case "$1" in
|
||||||
"day01a" | "day01b" | \
|
"day01a" | "day01b" | \
|
||||||
"day02a" | "day02b" | \
|
"day02a" | "day02b" | \
|
||||||
"day03a" | "day03b" | \
|
"day03a" | "day03b" | \
|
||||||
"day04a" | "day04b") stack run <input.txt
|
"day04a" | "day04b" | \
|
||||||
|
"day06a" | "day06b" ) stack run <input.txt
|
||||||
;;
|
;;
|
||||||
# Default solutions -- Java
|
# Default solutions -- Java
|
||||||
*) ./gradlew run <input.txt
|
*) ./gradlew run <input.txt
|
||||||
|
|
3
test.sh
3
test.sh
|
@ -24,7 +24,8 @@ case "$1" in
|
||||||
"day01a" | "day01b" | \
|
"day01a" | "day01b" | \
|
||||||
"day02a" | "day02b" | \
|
"day02a" | "day02b" | \
|
||||||
"day03a" | "day03b" | \
|
"day03a" | "day03b" | \
|
||||||
"day04a" | "day04b") stack run <test.txt
|
"day04a" | "day04b" | \
|
||||||
|
"day06a" | "day06b") stack run <test.txt
|
||||||
;;
|
;;
|
||||||
# Default solutions -- Java
|
# Default solutions -- Java
|
||||||
*) ./gradlew run <test.txt
|
*) ./gradlew run <test.txt
|
||||||
|
|
Loading…
Reference in New Issue