2022/06/Main.idr

37 lines
1.1 KiB
Idris
Raw Normal View History

2022-12-06 01:27:55 -05:00
import Data.Vect
import Data.List
import Data.Fin
import System.File.ReadWrite
-- Get windows of a specific size over the data
windows : {n : Nat} -> List a -> List (Vect n a)
windows [] = []
windows {n} xs =
let (head, _) = splitAt n xs
tail = drop 1 xs
in case toVect n head of
Nothing => []
(Just x) => x :: windows tail
2022-12-06 01:43:12 -05:00
findStart : Nat -> List Char -> Maybe Nat
findStart n cs =
let segments = windows {n} cs
in case findIndex (\x => let ( p ** _ ) = nub x in p == n) segments of
2022-12-06 01:27:55 -05:00
Nothing => Nothing
2022-12-06 01:43:12 -05:00
(Just x) => Just $ finToNat x + n
2022-12-06 01:27:55 -05:00
main : IO ()
main =
2022-12-06 01:46:55 -05:00
do file <- readFile "input"
case file of
Right contents =>
do putStr "Part 1: "
putStrLn $ case findStart 4 (unpack contents) of
Just start => show start
Nothing => "Failed to find start"
putStr "Part 2: "
putStrLn $ case findStart 14 (unpack contents) of
Just start => show start
Nothing => "Failed to find start"
Left err => printLn err