<jupyter_start><jupyter_text>Day 2: Password Philosophyhttps://adventofcode.com/2020/day/2<jupyter_code>inputLines = lines <$> readFile "input/day02.txt"<jupyter_output><empty_output><jupyter_text>We will use a regular expression to parse the input file.<jupyter_code>import Text.Regex.PCRE  -- install with 'stack install regex-pcre'
testInput = [ "1-3 a: abcde"
            , "1-3 b: cdefg"
            , "2-9 c: ccccccccc" ]<jupyter_output><empty_output><jupyter_text>`parseLine` reads a line in the specified format and extracts two `Int`s (separated by '`-`'), a `Char` (followed by a '`:`') and the password.<jupyter_code>parseLine :: String -> (Int, Int, Char, String)
parseLine line =
    let
        match = (line =~ "(\\d+)-(\\d+) (\\w): (\\w+)" :: [[String]])
        [_, n1, n2, letterString, password] = head match
    in
        (read n1, read n2, head letterString, password)<jupyter_output><empty_output><jupyter_text>Part 1First, we check if the number of ocurrences of the given letter in the password is between the two numbers.<jupyter_code>validate1 :: (Int, Int, Char, String) -> Bool
validate1 (minCount, maxCount, letter, password) =
    minCount <= count && count <= maxCount
    where
        count = length . filter (== letter) $ password
solution lineValidator = length . filter (lineValidator . parseLine)
solution1 = solution validate1
solution1 testInput<jupyter_output><empty_output><jupyter_text>Solution, part 1<jupyter_code>solution1 <$> inputLines<jupyter_output><empty_output><jupyter_text>Part 2Then we verify if exactly one of the letters in the password which are given by the two one-based indices is equal to the given letter.<jupyter_code>validate2 :: (Int, Int, Char, String) -> Bool
validate2 (pos1, pos2, letter, password) =
    matchLetter pos1 /= matchLetter pos2
    where
        matchLetter pos = letter == password !! pred pos
solution2 = solution validate2
solution2 testInput<jupyter_output><empty_output><jupyter_text>Solution, part 2<jupyter_code>solution2 <$> inputLines<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11959163830821584, 'fraction_numerical': 0.018473505104521146, 'mean_word_length': 4.716666666666667, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 35, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '116581', 'license': 'MIT', 'n_tokens_mistral': 695, 'n_tokens_neox': 665, 'n_words': 192, 'path': '2020/day02-haskell.ipynb', 'repo_name': 'freininghaus/adventofcode', 'size': '2057'}