1
0
Fork 0

Compare commits

..

2 Commits

Author SHA1 Message Date
Bill Ewanick 724b4abcf0 Advent of Code, 2024 - Day 2 Part 2 2024-12-29 14:13:25 -05:00
Bill Ewanick caee6ea61b Spelling 2024-12-29 14:12:58 -05:00
2 changed files with 6 additions and 13 deletions

View File

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

View File

@ -33,7 +33,8 @@ 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]
@ -43,17 +44,8 @@ adjacentLevelCheck report =
all (((== True) . (\n-> n>=1 && n<=3)) . abs)
(zipWith subtract report (tail report))
removeOneBadReport :: [Int] -> [[Int]]
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
safeReportP2 report = any safeReportP1 (removeOneBadReport report)
removeOneBadReport :: [Int] -> [[Int]]
removeOneBadReport lst = map (\i -> take i lst <> drop (succ i) lst) [0..length lst-1]