1
0
Fork 0

Compare commits

..

1 Commits

Author SHA1 Message Date
Bill Ewanick 3b05729081 Advent of Code, 2024 - Day 2 Part 2 2024-12-29 14:05:49 -05:00
2 changed files with 14 additions and 7 deletions

View File

@ -13,7 +13,6 @@
"HLINT",
"mempty",
"Prec",
"succ",
"unrecognised"
]
}

View File

@ -33,8 +33,7 @@ solveP1 = length . filter (== True) . map safeReportP1
solveP2 :: [[Int]] -> Int
solveP2 = length . filter (== True) . map safeReportP2
safeReportP1 :: [Int] -> Bool
safeReportP1 report = isSorted report && adjacentLevelCheck report
isSorted :: [Int] -> Bool
isSorted report = report `elem` [sort report, sortOn Down report]
@ -44,8 +43,17 @@ adjacentLevelCheck report =
all (((== True) . (\n-> n>=1 && n<=3)) . abs)
(zipWith subtract report (tail report))
safeReportP2 :: [Int] -> Bool
safeReportP2 report = any safeReportP1 (removeOneBadReport report)
removeOneBadReport :: [Int] -> [[Int]]
removeOneBadReport lst = map (\i -> take i lst <> drop (succ i) lst) [0..length lst-1]
removeOneBadReport lst = map (\i -> take i lst <> drop (succ i) lst) [0..(length lst)-1]
safeReportP1 :: [Int] -> Bool
safeReportP1 report = isSorted report && adjacentLevelCheck report
safeReportP2 :: [Int] -> Bool
safeReportP2 report =
or $ zipWith (&&) (map adjacentLevelCheck reports) (map isSorted reports)
where
reports = removeOneBadReport report