Aetherus

Aetherus

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

Showing Posts 1 to 10

aaronnamba

aaronnamba

ARGH, I spent way too much time looking for “the catch,” the reason it couldn’t as easy as it looked. :joy:

Aetherus

Aetherus OP

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.

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)
Aetherus

Aetherus OP

Kinda miss Ruby’s String#tr.

code-shoily

code-shoily

Here’s how I attempted it. Followed the boarding pass and walked.

https://github.com/code-shoily/advent_of_code/blob/master/lib/2020/day_5.ex

code-shoily

code-shoily

Wow, why didn’t this occur to me! Awesome solve! Thanks for sharing.

Aetherus

Aetherus OP

For me, the hint is the 8 in

multiply the row by 8, then add the column.

Multiplying by 8 is equivalent to shift 3 bits to the left, and guess how many bits is needed to represent the all the column numbers?

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
Aetherus

Aetherus OP

I feel the obfuscatedness (is that even a word?) of Perl code is way beyond the level of your code :grin:

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? Top

Trending in Challenges Top

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews