woojiahao
Advent of Code 2023 - Day 15
Got top 1000 for part 1, part 2 was super long so I took a while to understand it. Pretty fun day overall!
https://github.com/woojiahao/aoc/blob/main/lib/aoc/y2023/day_15.ex
Most Liked
trnasistor
My beginner’s solution, Day 15 part 1. Lens Library
defmodule Day15 do
def part1(input) do
parse(input)
|> Enum.map(&hash/1)
|> Enum.sum
end
def hash(string) do
for <<char::8 <- string>>, reduce: 0 do
acc -> rem(17 * (acc + char), 256)
end
end
def parse(raw_sequence) do
raw_sequence
|> remove("\n")
|> String.split(",")
end
defp remove(s, p), do: String.replace(s, p, "")
end
1
stevensonmt
The problem was really straightforward on both parts for this day. Makes me sad that I got so far behind on days 12-14. I didn’t do anything clever in my solution. This thread showed me some things I had not heard of. Namely, @code-shoily introducing Aja and @exists highlighting List.keydelete and List.keymember?. I wanted the theme this year to be binary pattern matching and manipulation as much as possible, but I didn’t want to overcomplicate this one and just used the String functions mostly.
defmodule Part1 do
def solve(input) do
input
|> parse()
|> Enum.sum()
end
defp parse(input) do
hasher(input, 0, [])
end
def hasher(<<>>, current_val, acc), do: [current_val | acc]
def hasher(<<",", rest::binary>>, current_val, acc), do: hasher(rest, 0, [current_val | acc])
def hasher(<<"\n", rest::binary>>, current_val, acc), do: hasher(rest, current_val, acc)
def hasher(<<next::8, rest::binary>>, current_val, acc),
do: hasher(rest, hash_fun(next, current_val), acc)
def hash_fun(next, current_val), do: rem((next + current_val) * 17, 256)
end
defmodule Part2 do
def solve(input) do
input
|> parse()
|> Enum.map(&calc_focus_pwr/1)
|> total_focus_pwr()
end
defp parse(input) do
input
|> String.replace("\n", "")
|> String.split(",")
|> Enum.map(&parse_label/1)
|> Enum.reduce(%{}, fn ops_label, map ->
hash_mapper(ops_label, map)
end)
end
defp parse_label(label) do
String.split(label, ~r{-|=}, include_captures: true)
end
defp hash_mapper([label, op, len], map) do
[box] = Part1.hasher(label, 0, [])
case op do
"-" ->
Map.update(map, box, [], fn curr ->
Enum.reject(curr, fn lens -> match?([^label, _], lens) end)
end)
"=" ->
Map.update(map, box, [[label, len]], fn curr ->
with nil <- Enum.find_index(curr, fn [a, _b] -> a == label end) do
[[label, len] | curr]
else
ndx ->
List.replace_at(curr, ndx, [label, len])
end
end)
end
end
defp calc_focus_pwr({box, lenses}) do
lenses
|> Enum.reverse()
|> Enum.with_index(1)
|> Enum.map(fn {[_label, lens], ndx} -> String.to_integer(lens) * ndx * (box + 1) end)
|> Enum.sum()
end
defp total_focus_pwr(enum), do: Enum.sum(enum)
end
1
Popular in Challenges
It is that time of the year again: Advent of Code 2022 :christmas_tree:
Day 1
Leaderboard:
New
I have 2 arrays: a1 can be any combination of value or nil like that
a1 = [1,nil,3]
and array 2 the same
a2 = [4,2, nil]
How do I com...
New
Since I started using Elixir, I have benefited greatly from being able to study various open-source projects. The codebase of LiveBook, i...
New
A frustrating one for me. I spent a long time trying to understand why some combinations resulted in fewer presses and struggled to keep ...
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
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
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
Just did part 1. Part 2 seems to be demanding too much of my reading time so will get to that after I am done with some chores.
Oh here ...
New
This topic is about Day 7 of the Advent of Code 2020 .
Thanks to @egze, we have a private leaderboard:
https://adventofcode.com/2020/le...
New
Thought I’d kick today’s thread off!
Parsing
Enum rocks, so most of my code was actually in parsing input.
▶ Preprocessing input
Part 1...
New
Other popular topics
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
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
Hi all,
I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I’m trying to use Postgres...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar.
I p...
New
I would like to know what is the best IDE for elixir development?
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
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









