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
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.
8
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
8
aaronnamba
Well… same general idea. 
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)
2
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
2
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
2
Popular in Challenges
This topic is about Day 9 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Took me a minute to remember my binary math :smile: :grimacing:…
import Bitwise
__DIR__
|> Path.join("puzzle.txt")
|> File.stream...
New
Everything went smoothly today.
Nothing to change to solve part 2 because I already used memoization for part 1 (it looked like an AoC e...
New
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
New
Anyone else think the prompt for this challenge is contradictory?
The rules for comparing packets include
If both values are lists, c...
New
Monkeys fitted squarely as GenServers in my head. My initial problem was using cast instead of call; I imagine impolite monkeys slinging...
New
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
Note: This topic is to talk about Day 4 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
Don’t know why the regex ~r/[\W && [^\.]]/x does not work in Elixir. It works pretty well in Ruby.
Anyway, here is my solution:
New
Other popular topics
can someone please explain to me how Enum.reduce works with maps
New
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this:
...
New
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
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
What is most correct way to open, read and parse JSON file with poison?
For example if we have example.json file in root of some projec...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Hi everyone,
One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
by Lance Halvorsen
Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
New







