Day 4 Part 1
This commit is contained in:
parent
9e042d5812
commit
755142de86
2 changed files with 1182 additions and 0 deletions
46
2020/day4/day4.ex
Normal file
46
2020/day4/day4.ex
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
defmodule Day4 do
|
||||||
|
defp read_input do
|
||||||
|
Path.expand('input', Path.dirname(__ENV__.file))
|
||||||
|
|> File.read!()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_passports(text) do
|
||||||
|
String.split(text, "\n\n")
|
||||||
|
|> Enum.map(fn block ->
|
||||||
|
fields = String.split(block)
|
||||||
|
|
||||||
|
Enum.reduce(fields, %{}, fn field, passport ->
|
||||||
|
[key, val] = String.split(field, ":")
|
||||||
|
Map.put(passport, key, val)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
@required_fields [
|
||||||
|
"byr",
|
||||||
|
"iyr",
|
||||||
|
"eyr",
|
||||||
|
"hgt",
|
||||||
|
"hcl",
|
||||||
|
"ecl",
|
||||||
|
"pid"
|
||||||
|
# "cid"
|
||||||
|
]
|
||||||
|
|
||||||
|
defp valid?(passport) do
|
||||||
|
Enum.all?(@required_fields, fn field ->
|
||||||
|
case Map.fetch(passport, field) do
|
||||||
|
{:ok, _val} -> true
|
||||||
|
:error -> false
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
def part1 do
|
||||||
|
read_input()
|
||||||
|
|> parse_passports()
|
||||||
|
|> Enum.count(&valid?/1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
Day4.part1() |> IO.inspect()
|
||||||
1136
2020/day4/input
Normal file
1136
2020/day4/input
Normal file
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue