-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOC2.hs
66 lines (53 loc) · 1.62 KB
/
AOC2.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
module AOC2 where
import Data.List
import Data.Maybe
import Text.Parsec
import Text.Parsec.ByteString (Parser, parseFromFile)
inputParser :: Parser [String]
inputParser = many (many1 letter <* skipMany space)
input = do
x <- parseFromFile inputParser "AOC2.input"
pure $
case x of
Left _ -> []
Right x -> x
test = ["abcdef", "bababc", "abbcde", "abcccd", "aabcdd", "abcdee", "ababab"]
nSet :: Int -> String -> Bool
nSet _ [] = False
nSet n (x:xs) =
case length $ filter (== x) xs of
c
| c == (n - 1) -> True
_ -> nSet n $ filter (/= x) xs
isDouble :: String -> Bool
isDouble = nSet 2
isTriple :: String -> Bool
isTriple = nSet 3
checksum :: [String] -> Int
checksum input =
let doubles = length $ filter isDouble input
triples = length $ filter isTriple input
in doubles * triples
-- 6225
solution1 = checksum <$> input
difference :: String -> String -> Int
difference = difference' 0
where
difference' d [] other = d + length other
difference' d other [] = d + length other
difference' d (x:xs) (y:ys) =
if x == y
then difference' d xs ys
else difference' (d + 1) xs ys
leastDiff :: [String] -> Maybe (String, String)
leastDiff [] = Nothing
leastDiff (x:xs) =
case filter (\(x', d) -> d == 1) $ fmap (\x' -> (x', difference x x')) xs of
[] -> leastDiff xs
(x', _):_ -> Just (x, x')
removeDiff :: (String, String) -> String
removeDiff (x, y) = map fst $ filter (uncurry (==)) $ zip x y
-- revtaubfniyhsgxdoajwkqilp
solution2 = do
x <- leastDiff <$> input
pure $ removeDiff $ fromMaybe undefined x