From 87b30e106fe9cfb305d49aed87b59a3ca778f97e Mon Sep 17 00:00:00 2001 From: Adam Millerchip Date: Wed, 2 Dec 2020 14:18:45 +0900 Subject: [PATCH] Day 2 Part 1 --- day2/day2part1.exs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 day2/day2part1.exs diff --git a/day2/day2part1.exs b/day2/day2part1.exs new file mode 100644 index 0000000..b2531b4 --- /dev/null +++ b/day2/day2part1.exs @@ -0,0 +1,21 @@ +defmodule Day2Part1 do + def run do + regex = ~r/\A(\d+)-(\d+) (\w): (\w+)\z/ + + File.read!("input") + |> String.trim() + |> String.split("\n") + |> Enum.map(&Regex.run(regex, &1, capture: :all_but_first)) + |> Enum.map(fn [least, most, char, password] -> + {String.to_integer(least), String.to_integer(most), char, password} + end) + |> Enum.filter(fn {least, most, char, password} -> + count = Enum.count(String.graphemes(password), &(&1 == char)) + least <= count && count <= most + end) + |> Enum.count() + |> IO.puts() + end +end + +Day2Part1.run()