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!
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
Note: This topic is to talk about Day 12 of the Advent of Code.
For general discussion about the Advent of Code 2018 and links to topics...
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
Note: This topic is to talk about Day 9 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
This topic is about Day 1 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums):
https://adven...
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
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
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
I’m having so much fun working on the “Protohackers” challenges, I never got into Advent of Code much but this has been amazing. The chal...
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
I spent 3 hours struggling in part 2, until I noticed a very basic mistake :joy:
Here’s my code:
By the way, the starting position in...
New
Other popular topics
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New







