Aetherus

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

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
Aetherus

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()
Damirados

Damirados

As this is typical day to day work I just imported Ecto in my solution

Where Next?

Popular in Challenges Top

sasajuric
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
bjorng
This topic is about Day 16 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
groovyda
Today’s challenge for me was about using reduce: defmodule Prob5 do def move([[h1 | rest] = _list1, list2]) do [rest, [h1 | list2]...
New
Qqwy
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code. For general discussion about the Advent of Code 2018 an...
New
Aetherus
This topic is about Day 4 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/le...
New
bjorng
Note: This topic is to talk about Day 5 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
seeplusplus
This one was much easier for me than yesterday’s. Part 1 runs in 22ms and part 2 runs in ~3s. defmodule BridgeRepair do def parse_inpu...
New
Aetherus
I spent 3 hours struggling in part 2, until I noticed a very basic mistake :joy: Here’s my code: https://github.com/Aetherus/advent-of-...
New
bjorng
Note: This topic is to talk about Day 6 of the Advent of Code 2019. There is a private leaderboard for elixirforum members. You can join...
New
christhekeele
Thought I’d kick today’s thread off! Parsing Enum rocks, so most of my code was actually in parsing input. ▶ Preprocessing input Part 1...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement