adventofcode-2020/day13/main.hs

40 lines
1.1 KiB
Haskell
Raw Normal View History

2020-12-13 17:50:00 +01:00
#! /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 -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 #-}
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Data.Attoparsec.Text (Parser)
import Data.Text (Text)
import Debug.Trace (trace)
import Text.Pretty.Simple
import qualified Data.Attoparsec.Text as A
import qualified Data.Text as T
exampleData :: String
exampleData = "939\n7,13,x,x,59,x,31,19"
numOrXParser :: Parser Text
numOrXParser = A.takeWhile (/= ',')
inputParser :: Parser (Int,[Int])
inputParser = do
n <- A.decimal
A.skipSpace
xs <- numOrXParser `A.sepBy` ","
pure (n,read <$> T.filter (/= ',') xs)
parseInput :: String -> Either String (Int,[Int])
parseInput = (A.parseOnly inputParser) . T.pack
main :: IO ()
main = do
putStrLn ":: Test"
A.parseTest inputParser $ T.pack exampleData
putStrLn ":: Day 13 - Part 1"