Day 6
This commit is contained in:
41
day06/Commons.hs
Normal file
41
day06/Commons.hs
Normal file
@@ -0,0 +1,41 @@
|
||||
module Commons where
|
||||
|
||||
import Data.List.Utils (split)
|
||||
|
||||
|
||||
data Race = Race { time :: Int, distance :: Int } deriving Show
|
||||
|
||||
|
||||
parseRaces :: [Int] -> [Int] -> [Race]
|
||||
parseRaces (th: tt) (dh: dt) = Race {time = th, distance = dh}: parseRaces tt dt
|
||||
parseRaces [] [] = []
|
||||
|
||||
parseTimes :: [String] -> [Int]
|
||||
parseTimes (('T': _): t) = parseTimes t
|
||||
parseTimes ([]: t) = parseTimes t
|
||||
parseTimes (h: t) = read h: parseTimes t
|
||||
parseTimes [] = []
|
||||
|
||||
parseDistances :: [String] -> [Int]
|
||||
parseDistances (('D': _): t) = parseDistances t
|
||||
parseDistances ([]: t) = parseDistances t
|
||||
parseDistances (h: t) = read h: parseDistances t
|
||||
parseDistances [] = []
|
||||
|
||||
parse :: IO [Race]
|
||||
parse = do rawTimes <- getLine
|
||||
let times = parseTimes $ split " " rawTimes
|
||||
rawDistances <- getLine
|
||||
let distances = parseDistances $ split " " rawDistances
|
||||
return $ parseRaces times distances
|
||||
|
||||
|
||||
getDistance :: Int -> Int -> Int
|
||||
getDistance holdTime totalTime = (totalTime - holdTime) * holdTime
|
||||
|
||||
getMinimalHoldTime :: Race -> Int -> Int
|
||||
getMinimalHoldTime race holdTime | getDistance holdTime (time race) > distance race = holdTime
|
||||
| otherwise = getMinimalHoldTime race (holdTime + 1)
|
||||
|
||||
getRaceRange :: Race -> Int
|
||||
getRaceRange race = time race + 1 - 2 * getMinimalHoldTime race 0
|
||||
12
day06/Main.hs
Normal file
12
day06/Main.hs
Normal file
@@ -0,0 +1,12 @@
|
||||
module Main where
|
||||
|
||||
import Commons
|
||||
import qualified Part1
|
||||
import qualified Part2
|
||||
|
||||
|
||||
main = do races <- parse
|
||||
let part1Res = product $ Part1.getRanges races
|
||||
print part1Res
|
||||
let part2Res = Part2.getRange races
|
||||
print part2Res
|
||||
7
day06/Part1.hs
Normal file
7
day06/Part1.hs
Normal file
@@ -0,0 +1,7 @@
|
||||
module Part1 where
|
||||
|
||||
import Commons
|
||||
|
||||
|
||||
getRanges :: [Race] -> [Int]
|
||||
getRanges = map getRaceRange
|
||||
13
day06/Part2.hs
Normal file
13
day06/Part2.hs
Normal file
@@ -0,0 +1,13 @@
|
||||
module Part2 where
|
||||
|
||||
import Commons
|
||||
|
||||
|
||||
getSingleRace :: [Race] -> Race
|
||||
getSingleRace [race] = race
|
||||
getSingleRace (rh: t) = let otherRace = getSingleRace t
|
||||
in Race {time = read $ show (time rh) ++ show (time otherRace),
|
||||
distance = read $ show (distance rh) ++ show (distance otherRace)}
|
||||
|
||||
getRange :: [Race] -> Int
|
||||
getRange = getRaceRange . getSingleRace
|
||||
Reference in New Issue
Block a user