summaryrefslogtreecommitdiff
path: root/2015/day5.exs
blob: fa9cdf8cf53539f33c86b6a22d7e9e58349bb061 (plain)
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
santasList = File.stream!("inputs/day5.txt")

niceVowelRegex = ~r/[aeiou].*[aeiou].*[aeiou]/
niceDuplicateRegex = ~r/(.)\1/
naughtySubstrRegex = ~r/(ab)|(cd)|(pq)|(xy)/

isNice1 = fn name ->
  niceVowels? = Regex.match?(niceVowelRegex, name)
  niceDuplicate? = Regex.match?(niceDuplicateRegex, name)
  naughtySustr? = Regex.match?(naughtySubstrRegex, name)
  niceVowels? && niceDuplicate? && !naughtySustr?
end

niceCount1 =
  Enum.filter(santasList, isNice1)
  |> Enum.count()

IO.puts("Nice count 1: #{niceCount1}")

niceDuplicatePairRegex = ~r/(..).*\1/
niceLetterRepeatRegex = ~r/(.).\1/

isNice2 = fn name ->
  niceDuplicatePair? = Regex.match?(niceDuplicatePairRegex, name)
  niceLetterRepeat? = Regex.match?(niceLetterRepeatRegex, name)
  niceDuplicatePair? && niceLetterRepeat?
end

niceCount2 =
  Enum.filter(santasList, isNice2)
  |> Enum.count()

IO.puts("Nice count 2: #{niceCount2}")