Day 13 Part 2
This commit is contained in:
parent
7a2d76c511
commit
ce6eb795ea
92
13/Main.idr
92
13/Main.idr
|
@ -25,6 +25,26 @@ Show Data where
|
||||||
let inner = joinBy "," . toList . map (\x => show (assert_smaller input x)) $ contents in
|
let inner = joinBy "," . toList . map (\x => show (assert_smaller input x)) $ contents in
|
||||||
"[" ++ inner ++ "]"
|
"[" ++ inner ++ "]"
|
||||||
|
|
||||||
|
Eq Data where
|
||||||
|
(==) (MkAtom atom) (MkAtom k) = atom == k
|
||||||
|
(==) (MkAtom atom) (MkList n contents) = False
|
||||||
|
(==) (MkList n contents) (MkAtom atom) = False
|
||||||
|
(==) (MkList n contents) (MkList k xs) = helper (toList contents) (toList xs)
|
||||||
|
where helper : List Data -> List Data -> Bool
|
||||||
|
helper [] [] = True
|
||||||
|
helper (x :: xs) [] = False
|
||||||
|
helper [] (x :: xs) = False
|
||||||
|
helper ((MkAtom atom) :: xs) ((MkAtom j) :: ys) =
|
||||||
|
if atom == j
|
||||||
|
then helper xs ys
|
||||||
|
else False
|
||||||
|
helper ((MkAtom atom) :: xs) ((MkList j zs) :: ys) = False
|
||||||
|
helper ((MkList j zs) :: xs) ((MkAtom atom) :: ys) = False
|
||||||
|
helper input1@((MkList j zs) :: xs) ((MkList i ws) :: ys) =
|
||||||
|
if helper (assert_smaller input1 (toList zs)) (toList ws)
|
||||||
|
then helper xs ys
|
||||||
|
else False
|
||||||
|
|
||||||
atomToList : Nat -> Data
|
atomToList : Nat -> Data
|
||||||
atomToList i = MkList 1 [MkAtom i]
|
atomToList i = MkList 1 [MkAtom i]
|
||||||
|
|
||||||
|
@ -122,46 +142,13 @@ parsePairs str =
|
||||||
y <- parseData y
|
y <- parseData y
|
||||||
pure (x, y)
|
pure (x, y)
|
||||||
|
|
||||||
simple = """
|
parseVect : String -> Maybe (m : Nat ** Vect m Data)
|
||||||
[1,1,3,1,1]
|
parseVect str =
|
||||||
[1,1,5,1,1]
|
do let lines = filter (not . (=="")). map trim . lines $ str
|
||||||
|
datas <- traverse parseData lines
|
||||||
[[1],[2,3,4]]
|
let m = length datas
|
||||||
[[1],4]
|
datas <- toVect m datas
|
||||||
|
pure (m ** datas)
|
||||||
[9]
|
|
||||||
[[8,7,6]]
|
|
||||||
|
|
||||||
[[4,4],4,4]
|
|
||||||
[[4,4],4,4,4]
|
|
||||||
|
|
||||||
[7,7,7,7]
|
|
||||||
[7,7,7]
|
|
||||||
|
|
||||||
[]
|
|
||||||
[3]
|
|
||||||
|
|
||||||
[[[]]]
|
|
||||||
[[]]
|
|
||||||
|
|
||||||
[1,[2,[3,[4,[5,6,7]]]],8,9]
|
|
||||||
[1,[2,[3,[4,[5,6,0]]]],8,9]
|
|
||||||
"""
|
|
||||||
|
|
||||||
testPart1 : IO ()
|
|
||||||
testPart1 =
|
|
||||||
do Just (n ** pairs) <- pure (parsePairs simple)
|
|
||||||
| Nothing => putStrLn "Failed to parse pairs"
|
|
||||||
let indicies = allFins' n
|
|
||||||
let correct_order =
|
|
||||||
map S .
|
|
||||||
map finToNat .
|
|
||||||
filter (\x =>
|
|
||||||
let (x,y) = index x pairs
|
|
||||||
in checkOrder x y) $
|
|
||||||
indicies
|
|
||||||
printLn pairs
|
|
||||||
printLn correct_order
|
|
||||||
|
|
||||||
part1 : (pairs : (n : Nat ** Vect n (Data, Data))) -> Nat
|
part1 : (pairs : (n : Nat ** Vect n (Data, Data))) -> Nat
|
||||||
part1 (n ** pairs) =
|
part1 (n ** pairs) =
|
||||||
|
@ -175,6 +162,26 @@ part1 (n ** pairs) =
|
||||||
indicies
|
indicies
|
||||||
in sum correct_order
|
in sum correct_order
|
||||||
|
|
||||||
|
sort : {n : Nat} -> Vect n Data -> Vect n Data
|
||||||
|
sort [] = []
|
||||||
|
sort (x :: xs) = insert x (sort xs)
|
||||||
|
where insert : {m : Nat} -> Data -> Vect m Data -> Vect (S m) Data
|
||||||
|
insert x [] = [x]
|
||||||
|
insert x rest@(y :: xs) =
|
||||||
|
if checkOrder x y
|
||||||
|
then x :: y :: xs
|
||||||
|
else y :: insert x xs
|
||||||
|
|
||||||
|
part2 : (datas : (m : Nat ** Vect m Data)) -> Maybe Nat
|
||||||
|
part2 (m ** datas) =
|
||||||
|
do let divider_packets =
|
||||||
|
(MkList 1 [MkList 1 [MkAtom 2]]) :: (MkList 1 [MkList 1 [MkAtom 6]]) :: datas
|
||||||
|
let sorted = sort divider_packets
|
||||||
|
divider_1 <- findIndex (== MkList 1 [MkList 1 [MkAtom 2]]) sorted
|
||||||
|
divider_2 <- findIndex (== MkList 1 [MkList 1 [MkAtom 6]]) sorted
|
||||||
|
let (divider_1, divider_2) = (1 + finToNat divider_1, 1 + finToNat divider_2)
|
||||||
|
pure (divider_1 * divider_2)
|
||||||
|
|
||||||
main : IO ()
|
main : IO ()
|
||||||
main =
|
main =
|
||||||
do Right file <- readFile "input"
|
do Right file <- readFile "input"
|
||||||
|
@ -182,3 +189,8 @@ main =
|
||||||
Just pairs <- pure (parsePairs file)
|
Just pairs <- pure (parsePairs file)
|
||||||
| Nothing => putStrLn "Failed parsing input"
|
| Nothing => putStrLn "Failed parsing input"
|
||||||
putStrLn $ "Part 1: " ++ show (part1 pairs)
|
putStrLn $ "Part 1: " ++ show (part1 pairs)
|
||||||
|
Just datas <- pure (parseVect file)
|
||||||
|
| Nothing => putStrLn "Failed parsing input"
|
||||||
|
Just part2Result <- pure (part2 datas)
|
||||||
|
| Nothing => putStrLn "Error in part2"
|
||||||
|
putStrLn $ "Part 2: " ++ show part2Result
|
||||||
|
|
Loading…
Reference in New Issue