Aetherus
Advent of Code 2020 - Day 4
This topic is about Day 4 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/leaderboard/private/view/39276
The join code is:
39276-eeb74f9a
Most Liked
aaronnamba
Observations:
- Woof, data validation… kinda tedious. A lot like work, and I do AoC to take a break from work…
- I anticipated having to look up things, so I parsed into a map, but that ended up being entirely unnecessary.
- Thank goodness for pattern matching.
Excerpt:
def all_fields_valid?(record) do
Enum.all?(record, fn {field, value} -> valid?(field, value) end)
end
def valid?(field, year) when field in ["byr", "eyr", "iyr"] and is_binary(year),
do: valid?(field, String.to_integer(year))
def valid?("byr", year) when year in 1920..2002, do: true
def valid?("iyr", year) when year in 2010..2020, do: true
def valid?("eyr", year) when year in 2020..2030, do: true
def valid?("hgt", {cm, "cm"}) when cm in 150..193, do: true
def valid?("hgt", {inch, "in"}) when inch in 59..76, do: true
def valid?("hgt", {_, _}), do: false
def valid?("hgt", height), do: valid?("hgt", Integer.parse(height))
def valid?("hcl", color), do: Regex.match?(~r/^#[0-9a-f]{6}$/, color)
def valid?("ecl", color) when color in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"],
do: true
def valid?("pid", pid), do: Regex.match?(~r/^[0-9]{9}$/, pid)
def valid?("cid", _), do: true
def valid?(_, _), do: false
5
Aetherus
A straightforward solution for Part 2:
#!/usr/bin/env elixir
defmodule Validator do
def valid?({"byr", value}) do
value
|> String.to_integer()
|> Kernel.in(1920..2002)
end
def valid?({"iyr", value}) do
value
|> String.to_integer()
|> Kernel.in(2010..2020)
end
def valid?({"eyr", value}) do
value
|> String.to_integer()
|> Kernel.in(2020..2030)
end
def valid?({"hgt", value}) do
case String.split(value, ~r/(?<=\d)(?=[a-z])/) do
[num, unit] ->
valid_height?(String.to_integer(num), unit)
_ -> false
end
end
def valid?({"hcl", value}) do
value =~ ~r/^\#[0-9a-f]{6}$/
end
def valid?({"ecl", value}) do
value in ~w[amb blu brn gry grn hzl oth]
end
def valid?({"pid", value}) do
value =~ ~r/^\d{9}$/
end
def valid?({_key, _value}) do
true
end
defp valid_height?(num, "cm"), do: num in (150..193)
defp valid_height?(num, "in"), do: num in (59..76)
defp valid_height?(_, _), do: false
end
required_fields = ~w[
byr
iyr
eyr
hgt
hcl
ecl
pid
]
to_passport = fn(fields)->
fields
|> Stream.map(&String.split(&1, ":"))
|> Stream.map(&List.to_tuple/1)
|> Map.new()
end
valid? = fn(passport)->
has_all_required_keys? = Enum.all?(required_fields, &Map.has_key?(passport, &1))
all_values_are_valid? = Enum.all?(passport, &Validator.valid?/1)
has_all_required_keys? and all_values_are_valid?
end
"day4.txt"
|> File.read!()
|> String.split("\n\n")
|> Stream.map(&String.split/1)
|> Enum.map(to_passport)
|> Enum.count(valid?)
|> IO.puts()
4
Damirados
Last Post!
NobbZ
Very slowly catching up!
https://gitlab.com/NobbZ/aoc_ex/-/blob/c668312f441ceccf5c6efa6fa14d7a96ab20fe51/lib/y2020/d04.ex
2
Trending in Challenges
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #elixirconf-us
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









