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
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
This topic is about Day 3 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.strea...
New
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
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 9 of the Advent of Code 2021 .
We have a private leaderboard (shared with users of Erlang Forums):
https://adve...
New
Phew, this one took a while to get right. My naive attempts was way to slow so I reached for Dijkstras shortest path algorithm.. and that...
New
Here is my solution for day 1 of Advent of Code:
defmodule Day01 do
def part1(input) do
all = parse(input)
{first, second} = E...
New
I mapped both the cards and every possible hand to numeric values and sorted them. In part 2 I could only think of replacing the jokers w...
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
Other popular topics
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hey all,
I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
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
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








