Skip to content

Commit 9f89df2

Browse files
committed
y2024/d2/p2 -- I forgot how drop works and it took me a while to find this issue
1 parent 0dec156 commit 9f89df2

File tree

3 files changed

+37
-29
lines changed

3 files changed

+37
-29
lines changed

Y2024/day-2.hs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ import Data.Maybe
55

66
solve :: [[Int]] -> Int -> Int
77
solve inp = \case
8-
1 -> res issafe
9-
2 -> res issafe'
10-
where
11-
res safe = pTraceShowCompact (map safe inp)
12-
(filter safe inp |> length)
8+
1 -> countIf issafe inp
9+
2 -> countIf issafe' inp
1310

14-
diffok' r = zipWith diffok r (tail r)
15-
diffok x y = abs (x - y) <= 3
11+
diffokAll r = zipWith diffok r (tail r)
12+
where
13+
diffok x y = abs (x - y) <= 3
1614

17-
issafe r = and (diffok' r)
15+
issafe r = and (diffokAll r)
1816
&& ismono r
1917

2018
ismono r =
@@ -25,26 +23,24 @@ ismono r =
2523

2624
-- Part 2
2725

28-
mono2 inc r = zipWith (monopred inc) r (tail r)
29-
|> flip zip [1..]
26+
-- Nothing if increasing, otherwise the index of the first element that breaks it minus one
27+
mono2 r = zipWith (<) r (tail r)
28+
|> flip zip [0..]
3029
|> lookup False
31-
where
32-
monopred inc x y = inc * (x - y) > 0
33-
34-
issafe' r = trace ("Is r=" ++ show r ++ " almost safe?")
35-
almostsafe 1 r || almostsafe -1 r
36-
37-
almostsafe inc r =
38-
case mono2 inc r of
39-
Nothing -> trace " - It's already mono. Is diffok?"
40-
(pTraceShowIdCompact $ and $ diffok' r)
41-
Just n -> trace (" - Not mono "
42-
++ (if inc == 1 then "one" else "another")
43-
++ " way, try to drop "
44-
++ show n ++ " and check safety: " )
45-
pTraceShowIdCompact (issafe (drop n r))
46-
47-
--t = pTraceShowCompact
30+
31+
issafe' r =
32+
almostsafe r || almostsafe (reverse r)
33+
34+
almostsafe r =
35+
case mono2 r of
36+
Nothing ->
37+
let dok = diffokAll r in
38+
and (tail dok) || and (init dok)
39+
Just n ->
40+
issafe (remove n r) || issafe (remove (n+1) r)
41+
42+
-- brute force -- works like a charm!
43+
issafe'' r = issafe r || or [ issafe (remove n r) | n <- [0..(length r - 1)]]
4844

4945
main :: IO ()
5046
main = defaultMain solve

Y2024/day-N.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module Main where
1+
module Main (main) where
22

33
import AoC
44

aoc-lib/src/AoC.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ module AoC (
2626
foldl',
2727
iterateWhile,
2828
counter, genericCounter,
29-
groupOn, catMaybes,
29+
groupOn, catMaybes, remove,
30+
countIf, count,
3031

3132
module Maps
3233
)
@@ -74,3 +75,14 @@ counter = flip zip [1,1..] .> IntMap.fromListWith (+)
7475

7576
genericCounter :: Ord a => [a] -> Map.Map a Int
7677
genericCounter = flip zip [1,1..] .> Map.fromListWith (+)
78+
79+
remove :: Int -> [a] -> [a]
80+
remove _ [] = []
81+
remove 0 (x:xs) = xs
82+
remove n (x:xs) = x : remove (n-1) xs
83+
84+
countIf :: (a -> Bool) -> [a] -> Int
85+
countIf p = filter p .> length
86+
87+
count:: Eq a => a -> [a] -> Int
88+
count a = filter (== a) .> length

0 commit comments

Comments
 (0)