Day 6 Part 2
This commit is contained in:
parent
ca66dc8740
commit
f62207d5de
55
06/Main.idr
55
06/Main.idr
|
@ -13,30 +13,44 @@ windows {n} xs =
|
||||||
Nothing => []
|
Nothing => []
|
||||||
(Just x) => x :: windows tail
|
(Just x) => x :: windows tail
|
||||||
|
|
||||||
findStartOfPacket : List Char -> Maybe Nat
|
findStart : Nat -> List Char -> Maybe Nat
|
||||||
findStartOfPacket cs =
|
findStart n cs =
|
||||||
let segments = windows {n = 4} cs
|
let segments = windows {n} cs
|
||||||
in case findIndex (\x => let ( p ** _ ) = nub x in p == 4) segments of
|
in case findIndex (\x => let ( p ** _ ) = nub x in p == n) segments of
|
||||||
Nothing => Nothing
|
Nothing => Nothing
|
||||||
(Just x) => Just $ finToNat x + 4
|
(Just x) => Just $ finToNat x + n
|
||||||
|
|
||||||
testCases : List (String, Nat)
|
findStartOfPacket : List Char -> Maybe Nat
|
||||||
testCases = [("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 7),
|
findStartOfPacket = findStart 4
|
||||||
("bvwbjplbgvbhsrlpgdmjqwftvncz",5),
|
|
||||||
("nppdvjthqldpwncqszvftbrmjlhg",6),
|
|
||||||
("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg",10),
|
|
||||||
("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw",11)]
|
|
||||||
|
|
||||||
testCase : (String, Nat) -> IO Bool
|
findStartOfMessage : List Char -> Maybe Nat
|
||||||
testCase (string, start) =
|
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
|
do putStrLn $ "Test Case: " ++ string
|
||||||
putStrLn $ "Expected Start: " ++ show start
|
putStrLn $ "Expected Start of packet: " ++ show start
|
||||||
|
putStrLn $ "Expected Start of message: " ++ show message
|
||||||
let result = findStartOfPacket (unpack string)
|
let result = findStartOfPacket (unpack string)
|
||||||
putStrLn $ "Evaluated start: " ++ show result
|
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)
|
let result = not (result == Just start)
|
||||||
if result
|
result <- if result
|
||||||
then putStrLn "Test Failed!!!"
|
then do putStrLn "Test Failed!!!"
|
||||||
else putStrLn "Test Passed"
|
pure True
|
||||||
|
else if messageResult == Just message
|
||||||
|
then do putStrLn "Test Passed"
|
||||||
|
pure False
|
||||||
|
else do putStrLn "Test Failed!!!"
|
||||||
|
pure True
|
||||||
putStrLn ""
|
putStrLn ""
|
||||||
pure result
|
pure result
|
||||||
|
|
||||||
|
@ -53,4 +67,9 @@ main =
|
||||||
putStrLn $ case findStartOfPacket (unpack contents) of
|
putStrLn $ case findStartOfPacket (unpack contents) of
|
||||||
Just start => show start
|
Just start => show start
|
||||||
Nothing => "Failed to find start"
|
Nothing => "Failed to find start"
|
||||||
|
putStr "Part 2: "
|
||||||
|
putStrLn $ case findStartOfMessage (unpack contents) of
|
||||||
|
Just start => show start
|
||||||
|
Nothing => "Failed to find start"
|
||||||
|
|
||||||
Left err => printLn err
|
Left err => printLn err
|
||||||
|
|
Loading…
Reference in New Issue