Commit 7f33f392 authored by Neil Smith's avatar Neil Smith
Browse files

Done day 13

parent 0f78ee34
......@@ -58,6 +58,4 @@ executables:
- base >= 2 && < 6
- text
- attoparsec
- containers
- vector
\ No newline at end of file
......@@ -46,15 +46,15 @@ part2 actions = manhattan (positionW ship1) start
ship0 = ShipW {positionW = start, waypoint = (10, 1)}
ship1 = foldl actW ship0 actions
apAc actions = ship1
where start = (0, 0)
ship0 = Ship {position = start, direction = East }
ship1 = foldl act ship0 actions
-- apAc actions = ship1
-- where start = (0, 0)
-- ship0 = Ship {position = start, direction = East }
-- ship1 = foldl act ship0 actions
apAcW actions = ship1
where start = (0, 0)
ship0 = ShipW {positionW = start, waypoint = (10, 1) }
ship1 = foldl actW ship0 actions
-- apAcW actions = ship1
-- where start = (0, 0)
-- ship0 = ShipW {positionW = start, waypoint = (10, 1) }
-- ship1 = foldl actW ship0 actions
act Ship{..} (N d) = Ship { position = dDelta d North position, ..}
act Ship{..} (S d) = Ship { position = dDelta d South position, ..}
......@@ -63,9 +63,7 @@ act Ship{..} (E d) = Ship { position = dDelta d East position, ..}
act Ship{..} (L a) = Ship { direction = d, ..} where d = (iterate predW direction) !! (a `div` 90)
act Ship{..} (R a) = Ship { direction = d, ..} where d = (iterate succW direction) !! (a `div` 90)
act Ship{..} (F d) = Ship { position = dDelta d direction position, ..}
-- where (x, y) = position
-- (dx, dy) = (delta direction)
-- p' = (x + (d * dx), y + (d * dy))
actW ShipW{..} (N d) = ShipW { waypoint = dDelta d North waypoint, ..}
actW ShipW{..} (S d) = ShipW { waypoint = dDelta d South waypoint, ..}
......
# This YAML file describes your package. Stack will automatically generate a
# Cabal file when you run `stack build`. See the hpack website for help with
# this file: <https://github.com/sol/hpack>.
name: advent13
synopsis: Advent of Code
version: '0.0.1'
default-extensions:
- AllowAmbiguousTypes
- ApplicativeDo
- BangPatterns
- BlockArguments
- DataKinds
- DeriveFoldable
- DeriveFunctor
- DeriveGeneric
- DeriveTraversable
- EmptyCase
- FlexibleContexts
- FlexibleInstances
- FunctionalDependencies
- GADTs
- GeneralizedNewtypeDeriving
- ImplicitParams
- KindSignatures
- LambdaCase
- MonadComprehensions
- MonoLocalBinds
- MultiParamTypeClasses
- MultiWayIf
- NamedFieldPuns
- NegativeLiterals
- NumDecimals
# - OverloadedLists
- OverloadedStrings
- PartialTypeSignatures
- PatternGuards
- PatternSynonyms
- PolyKinds
- RankNTypes
- RecordWildCards
- ScopedTypeVariables
- TemplateHaskell
- TransformListComp
- TupleSections
- TypeApplications
- TypeFamilies
- TypeInType
- TypeOperators
- ViewPatterns
executables:
advent13:
main: advent13.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
- text
- attoparsec
- arithmoi
\ No newline at end of file
-- import Debug.Trace
import Data.Text (Text)
-- import qualified Data.Text as T
import qualified Data.Text.IO as TIO
import Data.Attoparsec.Text
-- import Data.Attoparsec.Combinator
import Control.Applicative
import Data.Maybe
import Data.List
import Math.NumberTheory.Moduli.Chinese
main :: IO ()
main =
do text <- TIO.readFile "data/advent13.txt"
let (timestamp, busses) = successfulParse text
print timestamp
print busses
print $ part1 timestamp busses
print $ part2 busses
part1 timestamp maybeBusses = eBus * eTime
where busses = catMaybes maybeBusses
busDepartures = map (busAndTime timestamp) busses
(eBus, eTime) = head $ sortOn snd busDepartures
part2 maybeBusses = b - a
where (a, b) = earliestGroup $ periodOffsets maybeBusses
busAndTime timestamp period = (period, earliestDeparture timestamp period)
earliestDeparture timestamp period = period - (timestamp `mod` period)
periodOffsets :: [Maybe Integer] -> [(Integer, Integer)]
periodOffsets maybeBusses = offsetBusses
where offsetMaybeBusses = zip [0..] maybeBusses
offsetJustBusses = filter (isJust . snd) offsetMaybeBusses
offsetBusses = map (\(o, b) -> (o, fromJust b)) offsetJustBusses
earliestGroup :: [(Integer, Integer)] -> (Integer, Integer)
earliestGroup offsetBusses = foldl1 chineseStep offsetBusses
chineseStep (n1, m1) (n2, m2) = (n, m1 * m2)
where n = fromJust $ chinese (n1, m1) (n2, m2)
-- Parse the input file
timeBusP = (,) <$> decimal <* endOfLine <*> busPeriodsP
busPeriodsP = busPeriodP `sepBy` (string ",")
busPeriodP = (Just <$> decimal) <|> ("x" *> pure Nothing)
successfulParse :: Text -> (Integer, [Maybe Integer])
successfulParse input =
case parseOnly timeBusP input of
Left _err -> (0, []) -- TIO.putStr $ T.pack $ parseErrorPretty err
Right timeBus -> timeBus
1000104
41,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,37,x,x,x,x,x,659,x,x,x,x,x,x,x,23,x,x,x,x,13,x,x,x,x,x,19,x,x,x,x,x,x,x,x,x,29,x,937,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,17
\ No newline at end of file
......@@ -47,6 +47,7 @@ packages:
- advent10
- advent11
- advent12
- advent13
# Dependency packages to be pulled from upstream that are not in the resolver.
# These entries can reference officially published versions as well as
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment