In progress
This commit is contained in:
parent
0e498f3caa
commit
2240fd1325
1 changed files with 23 additions and 1 deletions
|
@ -1,11 +1,12 @@
|
||||||
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
#! /usr/bin/env -S"ANSWER=42" nix-shell
|
||||||
#! nix-shell -p ghcid
|
#! 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"
|
#! nix-shell -i "ghcid -c 'ghci' -T main"
|
||||||
|
|
||||||
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
|
{-# OPTIONS_GHC -Wall -Wincomplete-uni-patterns #-}
|
||||||
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports #-}
|
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-unused-imports #-}
|
||||||
{-# OPTIONS_GHC -Wno-unused-matches -Wno-type-defaults #-}
|
{-# OPTIONS_GHC -Wno-unused-matches -Wno-type-defaults #-}
|
||||||
|
{-# OPTIONS_GHC -Wno-unused-local-binds #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
|
||||||
import Control.Applicative
|
import Control.Applicative
|
||||||
|
@ -17,6 +18,7 @@ import Data.Maybe (fromMaybe,catMaybes)
|
||||||
import Data.List (find,sortOn)
|
import Data.List (find,sortOn)
|
||||||
import qualified Data.Attoparsec.Text as A
|
import qualified Data.Attoparsec.Text as A
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import Math.NumberTheory.Primes
|
||||||
|
|
||||||
exampleData :: String
|
exampleData :: String
|
||||||
exampleData = "939\n7,13,x,x,59,x,31,19"
|
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
|
$ map (\x -> (x,fromMaybe (-1) $ find (> n) [0,x..])) xs
|
||||||
pure $ bus * (time - n)
|
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 :: IO ()
|
||||||
main = do
|
main = do
|
||||||
putStrLn ":: Test"
|
putStrLn ":: Test"
|
||||||
|
@ -51,3 +69,7 @@ main = do
|
||||||
input <- readFile "day13/input"
|
input <- readFile "day13/input"
|
||||||
pPrint $ solvePart1 exampleData
|
pPrint $ solvePart1 exampleData
|
||||||
pPrint $ solvePart1 input
|
pPrint $ solvePart1 input
|
||||||
|
putStrLn ":: Test"
|
||||||
|
print $ unPrime . fst <$> factorise (1068781::Integer)
|
||||||
|
putStrLn ":: Day 13 - Part 2"
|
||||||
|
print $ solvePart2 exampleData
|
||||||
|
|
Loading…
Reference in a new issue