From 2240fd13255d4a392e48ddbc60e56111cf1c4494 Mon Sep 17 00:00:00 2001
From: "EEva (JPotier)" <jpo.contributes.to.nixos@marvid.fr>
Date: Tue, 15 Dec 2020 08:27:32 +0200
Subject: [PATCH] In progress

---
 day13/main.hs | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

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