update aoc2024 for record changes
This commit is contained in:
@@ -75,10 +75,10 @@ seqCost cur (pt :: pts) (kp, cost) =
|
|||||||
|
|
||||||
-- cost of best path from -> to in kp
|
-- cost of best path from -> to in kp
|
||||||
pathCost from to kp =
|
pathCost from to kp =
|
||||||
case lookupMap (from, to) (costs kp) of
|
case lookupMap (from, to) kp.costs of
|
||||||
Just (_, cost) => (kp, cost)
|
Just (_, cost) => (kp, cost)
|
||||||
Nothing =>
|
Nothing =>
|
||||||
let (path :: paths) = getPaths (interdit kp) from to | _ => ? in
|
let (path :: paths) = getPaths kp.interdit from to | _ => ? in
|
||||||
case kp of
|
case kp of
|
||||||
(KP n s i c Nothing) => (kp, 1)
|
(KP n s i c Nothing) => (kp, 1)
|
||||||
(KP n s i c (Just kp')) =>
|
(KP n s i c (Just kp')) =>
|
||||||
@@ -92,7 +92,7 @@ pathCost from to kp =
|
|||||||
|
|
||||||
mincost : List Dir → List (List Dir) → Keypad → Keypad × Int
|
mincost : List Dir → List (List Dir) → Keypad → Keypad × Int
|
||||||
mincost path paths kp =
|
mincost path paths kp =
|
||||||
let (kp', cost) = seqCost (0,2) (xlate path $ start kp) (kp, 0) in
|
let (kp', cost) = seqCost (0,2) (xlate path kp.start) (kp, 0) in
|
||||||
case paths of
|
case paths of
|
||||||
Nil => (kp', cost)
|
Nil => (kp', cost)
|
||||||
(path :: paths) => let (kp', cost') = mincost path paths kp' in (kp', min cost cost')
|
(path :: paths) => let (kp', cost') = mincost path paths kp' in (kp', min cost cost')
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ getPaths interdit pt@(a,b) to@(c,d) =
|
|||||||
updateCost : Point × Point → Int → State Keypad Unit
|
updateCost : Point × Point → Int → State Keypad Unit
|
||||||
updateCost path cost = do
|
updateCost path cost = do
|
||||||
kp <- get
|
kp <- get
|
||||||
put (KP (name kp) (start kp) (interdit kp) (updateMap path cost (costs kp)) (next kp))
|
put (KP (.name kp) (.start kp) (.interdit kp) (updateMap path cost (.costs kp)) (.next kp))
|
||||||
|
|
||||||
keyPos : Dir → Point
|
keyPos : Dir → Point
|
||||||
keyPos North = (0,1)
|
keyPos North = (0,1)
|
||||||
@@ -103,10 +103,10 @@ seqCost cur (pt :: pts) cost = do
|
|||||||
-- cost of best path from -> to in kp
|
-- cost of best path from -> to in kp
|
||||||
pathCost from to = do
|
pathCost from to = do
|
||||||
kp <- get {Keypad}
|
kp <- get {Keypad}
|
||||||
case lookupMap (from, to) (costs kp) of
|
case lookupMap (from, to) (.costs kp) of
|
||||||
Just (_, cost) => pure {State Keypad} cost
|
Just (_, cost) => pure {State Keypad} cost
|
||||||
Nothing =>
|
Nothing =>
|
||||||
let (path :: paths) = getPaths (interdit kp) from to | _ => ? in
|
let (path :: paths) = getPaths (.interdit kp) from to | _ => ? in
|
||||||
case kp of
|
case kp of
|
||||||
(KP n s i c Nothing) => pure 1
|
(KP n s i c Nothing) => pure 1
|
||||||
(KP n s i c (Just kp')) => do
|
(KP n s i c (Just kp')) => do
|
||||||
@@ -125,7 +125,7 @@ pathCost from to = do
|
|||||||
mincost : List Dir → List (List Dir) → State Keypad Int
|
mincost : List Dir → List (List Dir) → State Keypad Int
|
||||||
mincost path paths = do
|
mincost path paths = do
|
||||||
kp <- get
|
kp <- get
|
||||||
cost <- seqCost (0,2) (xlate path $ start kp) 0
|
cost <- seqCost (0,2) (xlate path $ kp.start) 0
|
||||||
case paths of
|
case paths of
|
||||||
Nil => pure cost
|
Nil => pure cost
|
||||||
(path :: paths) => do
|
(path :: paths) => do
|
||||||
@@ -163,13 +163,13 @@ run fn = do
|
|||||||
let rob1 = KP "r1" (0,2) (0,0) EmptyMap Nothing
|
let rob1 = KP "r1" (0,2) (0,0) EmptyMap Nothing
|
||||||
let robn = makeKeypad 2 rob1
|
let robn = makeKeypad 2 rob1
|
||||||
let kp = KP "kp" (3,2) (3,0) EmptyMap (Just robn)
|
let kp = KP "kp" (3,2) (3,0) EmptyMap (Just robn)
|
||||||
let p1 = foldl _+_ 0 $ map (uncurry _*_) $ snd $ runState (traverse (runOne numpad) codes) kp
|
let p1 = foldl _+_ 0 $ map (uncurry _*_) $ snd $ .runState (traverse (runOne numpad) codes) kp
|
||||||
putStrLn $ "part1 " ++ show p1
|
putStrLn $ "part1 " ++ show p1
|
||||||
|
|
||||||
let rob1 = KP "r1" (0,2) (0,0) EmptyMap Nothing
|
let rob1 = KP "r1" (0,2) (0,0) EmptyMap Nothing
|
||||||
let robn = makeKeypad 25 rob1
|
let robn = makeKeypad 25 rob1
|
||||||
let kp = KP "kp" (3,2) (3,0) EmptyMap (Just robn)
|
let kp = KP "kp" (3,2) (3,0) EmptyMap (Just robn)
|
||||||
let p2 = foldl _+_ 0 $ map (uncurry _*_) $ snd $ runState (traverse (runOne numpad) codes) kp
|
let p2 = foldl _+_ 0 $ map (uncurry _*_) $ snd $ .runState (traverse (runOne numpad) codes) kp
|
||||||
putStrLn $ "part2 " ++ show p2
|
putStrLn $ "part2 " ++ show p2
|
||||||
|
|
||||||
main : IO Unit
|
main : IO Unit
|
||||||
|
|||||||
@@ -24,6 +24,18 @@ record Gate where
|
|||||||
op : Op
|
op : Op
|
||||||
out : String
|
out : String
|
||||||
|
|
||||||
|
in1 : Gate → String
|
||||||
|
in1 g = g.in1
|
||||||
|
|
||||||
|
in2 : Gate → String
|
||||||
|
in2 g = g.in2
|
||||||
|
|
||||||
|
out : Gate → String
|
||||||
|
out g = g.out
|
||||||
|
|
||||||
|
op : Gate → Op
|
||||||
|
op g = g.op
|
||||||
|
|
||||||
Wire : U
|
Wire : U
|
||||||
Wire = String × Int
|
Wire = String × Int
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user