-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.hs
136 lines (99 loc) · 3.86 KB
/
Solution2.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
-- http://book.realworldhaskell.org/read/using-parsec.html
-- http://adventofcode.com/2016/day/7/input
-- http://stackoverflow.com/questions/10168756/parsing-a-string-in-haskell
-- https://www.haskell.org/hoogle/?hoogle=%5BBool%5D+-%3E+Bool
-- :m + Data.List
module Main where
import Debug.Trace
--import qualified Prelude as P
--import Text.Regex.Posix
import Data.List
import Text.ParserCombinators.Parsec
testinput = "abba[mnop]qrst\n"
testinput2 = "aba[bab]xyz\n"
--result = testinput =~ "[(a-z)]" :: String
--csvFile = endBy line eol
--line = sepBy cell (char ',')
--cell = many (noneOf ",\n")
--eol = char '\n'
--parseCodes :: P.String -> P.Either ParseError [[P.String]]
--parseCodes input = parse csvFile "(unknown)" input
--eol2 :: Parser P.String
--eol2 = string "\n" <|> string "\n\r"
data IP = Address String | Hypernet String deriving (Eq, Ord)
-- or use deriving: Show
instance Show IP where
show (Address s) = "Address " ++ s
show (Hypernet s) = "Hypernet " ++ s
content :: GenParser Char st [[IP]]
content = line `endBy` newline
--content =
line = many (address <|> hypernet)
address = Address <$> many1 (noneOf "[\n")
hypernet = Hypernet <$> (string "[" *> many (noneOf "]") <* string "]")
parseS :: String -> Either ParseError [[IP]]
parseS input = parse content "" input
acceptance1 value = (reverse value) == value
acceptance2 value = length(group value) == 3
abbatest value = acceptance1 value && acceptance2 value
parselist = case (parse content "" testinput) of
Left err -> print err
Right xs -> print (xs)
windowed :: Int -> [a] -> [[a]]
windowed size [] = []
windowed size ls@(x:xs) = if length ls >= size then (take size ls) : windowed size xs else windowed size xs
-- Put simply, . is function composition, just like in math:
-- f (g x) = (f . g) x
--https://wiki.haskell.org/Pointfree
countTrues = length . filter (== True)
isValidTLS ip = do {
let validAdresspart = or [ testAddressPart a| a <- getAddresses(ip)]
;let validHypernet = [ testAddressPart a| a <- getHypernet(ip)]
;and $ validAdresspart : validHypernet
--[(part, testAddressPart(part)) | part <- ip]
}
testAddressPart :: IP -> Bool
testAddressPart (Address s) = containsAbba(s)
testAddressPart (Hypernet s) = not $ containsAbba(s)
containsAbba s = or $ map abbatest (windowed 4 s)
containsAba s = or $ map abbatest (windowed 3 s)
countValidTLS xs = do {
--map isValidTLS xs
countTrues $ map testABA xs
}
isHypernet (Hypernet _) = True
isHypernet _ = False
isAddress (Address _) = True
isAddress _ = False
getHypernet = filter isHypernet
getAddresses = filter isAddress
test2 v = v
testt = findAbas(Address("zazbz"))
testABA ip = do {
let addresses = concat [findAbas a | a <- getAddresses(ip)]
;let addressesAlternated = map alternateBab addresses
;let hypernetAbas = concat [findAbas a | a <- getHypernet(ip)]
;not $ null (intersect addressesAlternated hypernetAbas)
}
alternateBab value = (value !! 1) : (value !! 0) : (value !! 1) : []
findAbas :: IP -> [String]
findAbas (Address s) = [x|x <- (windowed 3 s), abbatest x ]
findAbas (Hypernet s) = [x|x <- (windowed 3 s), abbatest x ]
main = do
--let xxx = [Address "xx", Hypernet "dnwtsgyabbaawerfamfv", Address "acccdabdbasdsd"]
--let xxx = [Address "zazdbz", Hypernet "bzb", Address "cdb"]
--print( isValidTLS xxx )
--print(extractHypernet(Hypernet "dnwtsgyawerfamfv"))
--print(testABA xxx)
input <- readFile "input.txt"
let result = parseS input
case result of
Left err -> print err
Right xs -> print (countValidTLS(xs))
--let r <- parseS testinput
--print r
-- print([ (abbatest x, x) | x <- permutations("abba")])
-- print(map abbatest (permutations "abba"))
-- print((permutations "abba"))
-- let eol = string "\n" <|> string "\n\r" :: GenParser P.Char st P.String
-- P.print(parse eol2 "" "\n\r")