diff --git a/day13/main.hs b/day13/main.hs index 143b932..066bb89 100755 --- a/day13/main.hs +++ b/day13/main.hs @@ -1,11 +1,12 @@ #! /usr/bin/env -S"ANSWER=42" nix-shell #! nix-shell -p ghcid -#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple attoparsec])" +#! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple attoparsec arithmoi])" #! nix-shell -i "ghcid -c 'ghci' -T main" {-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-} {-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports #-} {-# OPTIONS_GHC -Wno-unused-matches -Wno-type-defaults #-} +{-# OPTIONS_GHC -Wno-unused-local-binds #-} {-# LANGUAGE OverloadedStrings #-} import Control.Applicative @@ -17,6 +18,7 @@ import Data.Maybe (fromMaybe,catMaybes) import Data.List (find,sortOn) import qualified Data.Attoparsec.Text as A import qualified Data.Text as T +import Math.NumberTheory.Primes exampleData :: String exampleData = "939\n7,13,x,x,59,x,31,19" @@ -42,6 +44,22 @@ solvePart1 str = do $ map (\x -> (x,fromMaybe (-1) $ find (> n) [0,x..])) xs pure $ bus * (time - n) +inputParser2 :: Parser (Int,[Maybe Int]) +inputParser2 = do + n <- A.decimal + A.skipSpace + xs <- numOrXParser `A.sepBy` "," + pure (n,xs) + +parseInput2 :: String -> Either String (Int,[Maybe Int]) +parseInput2 = (A.parseOnly inputParser2) . T.pack + +solvePart2 :: String -> Either String [(Int,Int)] +solvePart2 str = do + (_,xs) <- parseInput2 str + let startAndIds = catMaybes $ sequence <$> zip [0..] xs + pure startAndIds + main :: IO () main = do putStrLn ":: Test" @@ -51,3 +69,7 @@ main = do input <- readFile "day13/input" pPrint $ solvePart1 exampleData pPrint $ solvePart1 input + putStrLn ":: Test" + print $ unPrime . fst <$> factorise (1068781::Integer) + putStrLn ":: Day 13 - Part 2" + print $ solvePart2 exampleData