Day 6: Cleanup
This commit is contained in:
parent
f62207d5de
commit
38eb10ed45
43
06/Main.idr
43
06/Main.idr
|
@ -20,56 +20,17 @@ findStart n cs =
|
||||||
Nothing => Nothing
|
Nothing => Nothing
|
||||||
(Just x) => Just $ finToNat x + n
|
(Just x) => Just $ finToNat x + n
|
||||||
|
|
||||||
findStartOfPacket : List Char -> Maybe Nat
|
|
||||||
findStartOfPacket = findStart 4
|
|
||||||
|
|
||||||
findStartOfMessage : List Char -> Maybe Nat
|
|
||||||
findStartOfMessage = findStart 14
|
|
||||||
|
|
||||||
testCases : List (String, Nat, Nat)
|
|
||||||
testCases = [("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 7, 19),
|
|
||||||
("bvwbjplbgvbhsrlpgdmjqwftvncz", 5, 23),
|
|
||||||
("nppdvjthqldpwncqszvftbrmjlhg", 6, 23),
|
|
||||||
("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 10, 29),
|
|
||||||
("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 11, 26)]
|
|
||||||
|
|
||||||
testCase : (String, Nat, Nat) -> IO Bool
|
|
||||||
testCase (string, start, message) =
|
|
||||||
do putStrLn $ "Test Case: " ++ string
|
|
||||||
putStrLn $ "Expected Start of packet: " ++ show start
|
|
||||||
putStrLn $ "Expected Start of message: " ++ show message
|
|
||||||
let result = findStartOfPacket (unpack string)
|
|
||||||
let messageResult = findStartOfMessage (unpack string)
|
|
||||||
putStrLn $ "Evaluated start of packet: " ++ show result
|
|
||||||
putStrLn $ "Evaluated start of message: " ++ show messageResult
|
|
||||||
let result = not (result == Just start)
|
|
||||||
result <- if result
|
|
||||||
then do putStrLn "Test Failed!!!"
|
|
||||||
pure True
|
|
||||||
else if messageResult == Just message
|
|
||||||
then do putStrLn "Test Passed"
|
|
||||||
pure False
|
|
||||||
else do putStrLn "Test Failed!!!"
|
|
||||||
pure True
|
|
||||||
putStrLn ""
|
|
||||||
pure result
|
|
||||||
|
|
||||||
main : IO ()
|
main : IO ()
|
||||||
main =
|
main =
|
||||||
do testsPart1 <- traverse testCase testCases
|
|
||||||
if or . map (\x => Delay x) $ testsPart1
|
|
||||||
then putStrLn "Failed tests"
|
|
||||||
else
|
|
||||||
do file <- readFile "input"
|
do file <- readFile "input"
|
||||||
case file of
|
case file of
|
||||||
Right contents =>
|
Right contents =>
|
||||||
do putStr "Part 1: "
|
do putStr "Part 1: "
|
||||||
putStrLn $ case findStartOfPacket (unpack contents) of
|
putStrLn $ case findStart 4 (unpack contents) of
|
||||||
Just start => show start
|
Just start => show start
|
||||||
Nothing => "Failed to find start"
|
Nothing => "Failed to find start"
|
||||||
putStr "Part 2: "
|
putStr "Part 2: "
|
||||||
putStrLn $ case findStartOfMessage (unpack contents) of
|
putStrLn $ case findStart 14 (unpack contents) of
|
||||||
Just start => show start
|
Just start => show start
|
||||||
Nothing => "Failed to find start"
|
Nothing => "Failed to find start"
|
||||||
|
|
||||||
Left err => printLn err
|
Left err => printLn err
|
||||||
|
|
Loading…
Reference in New Issue