Aetherus

Aetherus

Advent of Code 2020 - Day 5

This topic is about Day 5 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

Aetherus

Aetherus

I guess you’ve found the same solution as mine.

WARNING: Huge Exploits!!

Don’t look at my solution until you find your own.

Part 1

#!/usr/bin/env elixir

"day5.txt"
|> File.stream!()
|> Enum.map(&String.trim/1)
|> Enum.map(fn s ->
    s
    |> String.replace(["F", "L"], "0", global: true)
    |> String.replace(["B", "R"], "1", global: true)
    |> String.to_integer(2)
end)
|> Enum.max()
|> IO.puts()

Part 2

#!/usr/bin/env elixir

"day5.txt"
|> File.stream!()
|> Enum.map(&String.trim/1)
|> Enum.map(fn s ->
    s
    |> String.replace(["F", "L"], "0", global: true)
    |> String.replace(["B", "R"], "1", global: true)
    |> String.to_integer(2)
end)
|> Enum.sort()
|> Enum.chunk_every(2, 1, :discard)
|> Enum.find(&match?([a, b] when a != b - 1, &1))
|> Enum.reduce(&+/2)
|> Kernel.div(2)
|> IO.puts()

I’m still working on the legitimacy of this approach.

adamu

adamu

How about this for the binary parsing @Aetherus, @aaronnamba @egze (looks like @lud was almost there too)?

def char_to_bit(c) when c in 'FL', do: <<0::1>>
def char_to_bit(c) when c in 'BR', do: <<1::1>>

def seat_id(boarding_pass \\ "FBFBBFFRLR") do
  <<id::integer-10>> = for <<char <- boarding_pass>>, into: <<>>, do: char_to_bit(char)
  id
end

aaronnamba

aaronnamba

Well… same general idea. :slight_smile:

There must be a more efficient way to do this, but the meat of it is:

  def seat_id(boarding_pass) do
    boarding_pass
    |> String.graphemes()
    |> Enum.map(&char_to_digit/1)
    |> Enum.reverse()
    |> Enum.with_index()
    |> Enum.reduce(0, fn {digit, exp}, acc -> acc + Bitwise.bsl(digit, exp) end)
  end

  def char_to_digit(str) do
    case str do
      "B" -> 1
      "F" -> 0
      "R" -> 1
      "L" -> 0
    end
  end

Then for part 2, I checked the upper and lower bounds real quick, then:

Enum.to_list(89..989) -- Enum.map(parse_input(filename), &seat_id/1)
bossek

bossek

Today imitating Perl “write-only” style:

defmodule Day05PerlLike do
  import Enum, only: [map: 2, max: 1, sort: 1]
  import String, only: [replace: 3, split: 1, to_integer: 2]

  def p1, do: max(map(input(), &seat_id/1))

  def p2, do: find(sort(map(input(), &seat_id/1)))

  defp input, do: split(File.read!("data/05"))

  defp seat_id(bp), do: to_integer(replace(bp, ~r/./, &((&1 in ~w/F L/ && "0") || "1")), 2)

  defp find([sp, sn | ss]), do: (sp + 2 == sn && sp + 1) || find([sn | ss])
end
faried

faried

Nothing fancy. I’m not happy with my part2.

defmodule Day05 do
  def readinput() do
    File.read!("5.input.txt")
    |> String.split("\n")
    |> Enum.map(&String.graphemes/1)
  end

  def part1(input \\ readinput()) do
    input
    |> Enum.map(&findseat/1)
    |> Enum.max()
  end

  def part2(input \\ readinput()) do
    ids =
      input
      |> Enum.map(&findseat/1)
      |> Enum.sort()

    findmissing(MapSet.new(ids), List.first(ids), List.last(ids))
  end

  def findmissing(_mids, curid, maxid) when curid > maxid, do: :error

  def findmissing(mids, curid, maxid) do
    if curid not in mids and (curid - 1) in mids and (curid + 1) in mids do
      curid
    else
      findmissing(mids, curid + 1, maxid)
    end
  end

  def findseat(assignment, row \\ 0..127, col \\ 0..7)

  def findseat([], row, col), do: row.first * 8 + col.first

  # just makes writing the test cases easier
  def findseat(assignment, row, col) when is_binary(assignment),
    do: findseat(String.graphemes(assignment), row, col)

  def findseat([letter | assignment], row, col) do
    case letter do
      "F" -> findseat(assignment, lower(row), col)
      "B" -> findseat(assignment, upper(row), col)
      "R" -> findseat(assignment, row, upper(col))
      "L" -> findseat(assignment, row, lower(col))
    end
  end

  def lower(range) do
    range.first..(range.first + div(range.last - range.first, 2))
  end

  def upper(range) do
    (range.first + div(1 + range.last - range.first, 2))..range.last
  end
end

Where Next?

Popular in Challenges Top

Aetherus
This topic is about Day 16 of the Advent of Code 2020 . Thanks to @egze, we have a private leaderboard: https://adventofcode.com/2020/l...
New
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
sukhmeetsd
All in all, from what I understand, it is better not to use GenServer.cast when we want some concurrent operations to happen for sure, be...
New
adamu
Nobody’s doing Advent of Code this year? :smile: I might do the first week or so. For Day 1, first I solved it using regular expression...
New
rugyoga
Fairly straightforward Dijkstra’s algorithm import AOC aoc 2023, 17 do def compute(input, candidates) do {{max_row, max_col}, ite...
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
bjorng
Note: This topic is to talk about Day 2 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can joi...
New
adamu
Probably not the most efficient implementation, because part 1 took &gt;1 ms and part 2 &gt;4ms, but the code was simple enough. def p...
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement