defmodule AOC.Day2 do def parse_line(line) do [min, max, char, password] = String.split(line, [" ", "-", ":"], trim: true) password = String.to_charlist(password) [char] = String.to_charlist(char) min = String.to_integer(min) max = String.to_integer(max) [password, char, min, max] end def validate_password1(password, char, min, max) do count = Enum.count(password, &(&1 == char)) count in min..max end def part1 do AOC.Util.input_lines(2, 1) |> Enum.map(&parse_line/1) |> Enum.map(&apply(__MODULE__, :validate_password1, &1)) |> Enum.count(&Function.identity/1) end def validate_password2(password, char, pos1, pos2) do m1 = Enum.at(password, pos1 - 1) == char m2 = Enum.at(password, pos2 - 1) == char (m1 or m2) and not (m1 and m2) end def part2 do AOC.Util.input_lines(2, 1) |> Enum.map(&parse_line/1) |> Enum.map(&apply(__MODULE__, :validate_password2, &1)) |> Enum.count(&Function.identity/1) end end