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)

Last Post!

code-shoily

code-shoily

I learned very recently about Raku, along with its relationship with Perl. From a brief look, I realized seasoned Raku users would be quite swift at solving AoC puzzles

Where Next?

Popular in Challenges Top

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
QuinnWilton
Note: This topic is to talk about Day 7 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can joi...
New
Aetherus
This topic is about Day 15 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
Aetherus
Today’s challenge is quite interesting. I ended up using Zipper to solve this problem. Maybe I overengineered quite a bit. The data stru...
New
adamu
Nobody’s doing Advent of Code this year? :grinning_face_with_smiling_eyes: I might do the first week or so. For Day 1, first I solved i...
New
lud
Gosh this one took me sooo much time. At first I was trying to iterate each digit independently on the input A number to make digits cha...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
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

We're in Beta

About us Mission Statement