Skip to content

Commit

Permalink
refactor(2023.06-haskell): use the quadratic formula
Browse files Browse the repository at this point in the history
  • Loading branch information
yurrriq committed Dec 22, 2023
1 parent 4a27606 commit 65e7ae9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023.6.2.2
2023.6.2.3
10 changes: 7 additions & 3 deletions src/AdventOfCode/Year2023/Day06.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ module AdventOfCode.Year2023.Day06 where

import AdventOfCode.Input (parseInput, parseString)
import AdventOfCode.TH (defaultMain, inputFilePath)
import AdventOfCode.Util (count)
import Data.List.Extra (productOn')
import Text.Trifecta (Parser, natural, some, symbol)

Expand All @@ -19,8 +18,13 @@ partTwo races = waysToWin (time, distance)
distance = read $ concatMap (show . snd) races

waysToWin :: (Int, Int) -> Int
waysToWin (time, distance) =
count (\speed -> (time - speed) * speed > distance) [1 .. time - 1]
waysToWin (time, distance) = maxTime - minTime + 1
where
maxTime = floor ((time' + delta) / 2)
minTime = ceiling ((time' - delta) / 2)
delta, time' :: Double
delta = sqrt $ time' ** 2 - 4 * fromIntegral distance
time' = fromIntegral time

getInput :: IO [(Int, Int)]
getInput = parseInput document $(inputFilePath)
Expand Down

0 comments on commit 65e7ae9

Please sign in to comment.