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

Aetherus
Hello, guys. I’m back again, but only for the weekends, maybe. This topic is about Day 13 of the Advent of Code 2020 . Thanks to @egze,...
New
bjorng
Here is my solution for day 2 of Advent of Code: https://github.com/bjorng/advent-of-code/blob/main/2024/day02/lib/day02.ex
New
bjorng
This topic is about Day 18 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
bjorng
This topic is about Day 9 of the Advent of Code 2021 . We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
bjorng
Here is my solution: https://github.com/bjorng/advent-of-code-2021/blob/77bdef7a51b428b58ffb4ab76f540901e636f841/day12/lib/day12.ex
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
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
New
rvnash
Anyone have a solution to Part 2 today? Part 1 was straight forward, but I can’t figure out a programatic way to do part 2. I understand ...
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

Other popular topics Top

danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29603 241
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement