rugyoga
Most Liked
hauleth
Thanks @josevalim for custom operators. Again - LiveBook:
Day 8
input =
File.stream!("day8.txt")
|> Stream.map(fn line ->
line
|> String.split(" | ")
|> Enum.map(fn part ->
part
|> String.trim()
|> String.split(" ")
|> Enum.map(fn disp ->
disp
|> String.to_charlist()
|> Enum.sort()
|> List.to_string()
end)
end)
|> List.to_tuple()
end)
#Stream<[
enum: %File.Stream{
line_or_bytes: :line,
modes: [:raw, :read_ahead, :binary],
path: "day8.txt",
raw: true
},
funs: [#Function<47.58486609/1 in Stream.map/2>]
]>
input
|> Enum.map(fn {_, output} ->
Enum.count(output, &(byte_size(&1) in [2, 3, 4, 7]))
end)
|> Enum.sum()
390
defmodule Day8.Task2 do
def a --- b do
MapSet.difference(a, b)
end
def a +++ b do
MapSet.union(a, b)
end
def a <~> b do
MapSet.intersection(a, b)
end
def a <|> b do
(a +++ b) --- (a <~> b)
end
# 1. 7. 4. 2|3|5. 2|3|5. 2|3|5. 6|9|0. 6|9|0. 6|9|0. 8.
def deduce({cf, acf, bcdf, acdeg, acdfg, abdfg, abdefg, abcdfg, abcefg, abcdefg}) do
a = acf --- cf
eg = abcdefg --- (acf +++ bcdf)
bd = bcdf --- cf
abfg = abdefg <|> abcdfg <|> abcefg
b = abfg <~> bd
f = abfg <~> cf
g = abfg --- (a +++ b +++ f)
d = bd --- b
c = cf --- f
e = eg --- g
{a, b, c, d, e, f, g} =
[a, b, c, d, e, f, g]
|> Enum.map(&extract/1)
|> List.to_tuple()
[
# 0
[a, b, c, e, f, g],
# 1
[c, f],
# 2
[a, c, d, e, g],
# 3
[a, c, d, f, g],
# 4
[b, c, d, f],
# 5
[a, b, d, f, g],
# 6
[a, b, d, e, f, g],
# 7
[a, c, f],
# 8
[a, b, c, d, e, f, g],
# 9
[a, b, c, d, f, g]
]
|> Enum.map(&List.to_string(Enum.sort(&1)))
|> Enum.with_index()
|> Map.new()
end
defp extract(a) do
[v] = MapSet.to_list(a)
v
end
def decode(matches, output) do
output
|> Enum.map(&matches[&1])
|> Integer.undigits()
end
end
input
|> Enum.map(fn {input, output} ->
input
|> Enum.sort_by(&byte_size/1)
|> Enum.map(&MapSet.new(String.to_charlist(&1)))
|> List.to_tuple()
|> Day8.Task2.deduce()
|> Day8.Task2.decode(output)
end)
|> Enum.sum()
warning: variable "abdfg" is unused (if the variable is not meant to be used, prefix it with an underscore)
solutions.livemd#cell:19: Day8.Task2.deduce/1
warning: variable "acdeg" is unused (if the variable is not meant to be used, prefix it with an underscore)
solutions.livemd#cell:19: Day8.Task2.deduce/1
warning: variable "acdfg" is unused (if the variable is not meant to be used, prefix it with an underscore)
solutions.livemd#cell:19: Day8.Task2.deduce/1
1011785
4
mruoss
Second part approach:
- Get the correct mapping from segments to digit
- 1, 4, 7 and 8 are unique by the number of segments
- 6, 9, 0 all have 6 segments
- derive 9 which is the only one that fully contains 4
- derive 0 which is the only one that fully contains 7
- 6 is the remainder of the 6-segment digits
- 2, 3, 5 all have 5 segments
- derive 3 which is the only one that fully contains 7
- derive 5 which is the only one fully contained in 6
- 2 is the remainder of the 5-segment digits
def solve(stream, :second) do
stream
|> parse() # returns a tuple {patterns, output}, both being sorted charlists
|> Stream.map(&derive_numbers/1)
|> Enum.sum()
end
defp derive_numbers({patterns, output}) do
%{2 => [one], 3 => [seven], 4 => [four], 5 => len_five, 6 => len_six, 7 => [eight]} =
(output ++ patterns)
|> Enum.uniq()
|> Enum.group_by(&Kernel.length/1)
{[nine], len_six} = Enum.split_with(len_six, fn nr -> four -- nr == [] end)
{[zero], [six]} = Enum.split_with(len_six, fn nr -> seven -- nr == [] end)
{[three], len_five} = Enum.split_with(len_five, fn nr -> seven -- nr == [] end)
{[five], [two]} = Enum.split_with(len_five, fn nr -> nr -- six == [] end)
output
|> Enum.map(fn
^one -> 1
^two -> 2
^three -> 3
^four -> 4
^five -> 5
^six -> 6
^seven -> 7
^eight -> 8
^nine -> 9
^zero -> 0
end)
|> Integer.undigits()
end
3
josevalim
Creator of Elixir
Day 8 was my favorite day by far, here is my solution: https://github.com/josevalim/aoc/blob/main/2021/day-08.livemd
Ended up using a lot of pattern matching and charlists. Streaming here: Twitch (there is a discussion about bytes, charlists, and Unicode at the end).
2
Popular in Challenges
This topic is about Day 17 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Not the prettiest but it works
https://github.com/rugyoga/aoc2023/blob/main/lib/2024/9.ex
New
At first I was scared but I found is a simple way to compute the sides.
defmodule AdventOfCode.Solutions.Y24.Day12 do
alias AdventOfCo...
New
Took a while, but another use case for “move vectors” today and pattern matching. :slight_smile:
The trick was to first generate a list...
New
Note by the Moderators: This topic is to talk about Day 6 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
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
Note: This topic is to talk about Day 25 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
Anyone else think the prompt for this challenge is contradictory?
The rules for comparing packets include
If both values are lists, c...
New
Note: This topic is to talk about Day 13 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can joi...
New
This topic is about Day 14 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/l...
New
Other popular topics
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hello, how can I check the Phoenix version ?
Thanks !
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
In templates/appointment/index.html.eex:
<%= for appointment <- @appointments do %>
<tr>
<td><%= appoi...
New
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’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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









