-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay07.hs
64 lines (58 loc) · 1.7 KB
/
Day07.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
-- |
-- Module : AOC2016.Day07
-- License : BSD3
--
-- Stability : experimental
-- Portability : non-portable
--
-- Day 7. See "AOC.Solver" for the types used in this module!
module AOC2016.Day07 (
day07a,
day07b,
) where
import AOC.Solver ((:~>) (..))
import Data.Bifunctor (first, second)
import qualified Data.Set as S
import Data.Tuple (swap)
day07a :: [([String], [String])] :~> Int
day07a =
MkSol
{ sParse = Just . map chunkUp . lines
, sShow = show
, sSolve = Just . length . filter (uncurry validIP)
}
where
validIP outer inner =
not (null (concatMap findAbba outer))
&& null (concatMap findAbba inner)
day07b :: [([String], [String])] :~> Int
day07b =
MkSol
{ sParse = Just . map chunkUp . lines
, sShow = show
, sSolve = Just . length . filter (uncurry validIP)
}
where
validIP outer inner = not (S.null (S.intersection abaO abaI))
where
abaO = S.fromList (concatMap findAba outer)
abaI = S.fromList (swap <$> concatMap findAba inner)
findAbba :: String -> [(Char, Char)]
findAbba (a : b : c : d : xs)
| a /= b && a == d && b == c = (a, b) : findAbba (b : c : d : xs)
| otherwise = findAbba (b : c : d : xs)
findAbba _ = []
findAba :: String -> [(Char, Char)]
findAba (a : b : c : xs)
| a == c && a /= b = (a, b) : findAba (b : c : xs)
| otherwise = findAba (b : c : xs)
findAba _ = []
chunkUp :: String -> ([String], [String])
chunkUp = outOfBracket
where
outOfBracket str = case span (/= '[') str of
(xs, []) -> ([xs], [])
(xs, _ : ys) -> first (xs :) (inBracket ys)
inBracket str = case span (/= ']') str of
(xs, []) -> ([], [xs])
(xs, _ : ys) -> second (xs :) (outOfBracket ys)