shritesh

shritesh

Advent of Code 2023 - Day 7

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 with all unique values in the hand. There’s probably a better way.

Most Liked

seeplusplus

seeplusplus

In part 2 I could only think of replacing the jokers with all unique values in the hand

Heh, better than I did, I just brute forced and tried every card possible

/shrug

pehbehbeh

pehbehbeh

I used DataFrames with Explorer for the first time today. May be overkill, but I learned a lot.

It was nice to be able to tinker directly in the results via LiveBook:

bjorng

bjorng

Erlang Core Team

Today’s puzzle was fun!

My solution:

Aetherus

Aetherus

I accidentally removed my code, so I wrote it again and didn’t bother to refactor it.

Prep

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2023", "7", System.fetch_env!("LB_AOC_SESSION"))

strengths =
  ?2..?9
  |> Map.new(&{&1, &1 - ?0})
  |> Map.merge(%{
    ?T => 10,
    ?J => 11,
    ?Q => 12,
    ?K => 13,
    ?A => 14
  })

Part 1

puzzle_input
|> String.split()
|> Stream.chunk_every(2)
|> Stream.map(fn [hand, bid] ->
  cards = String.to_charlist(hand)
  freqs = cards |> Enum.frequencies() |> Map.values() |> Enum.sort(:desc)
  scores = Enum.map(cards, &strengths[&1])
  {freqs, scores, String.to_integer(bid)}
end)
|> Enum.sort()
|> Stream.map(&elem(&1, 2))
|> Stream.with_index(1)
|> Stream.map(fn {bid, rank} -> bid * rank end)
|> Enum.sum()

Part 2

strengths = %{strengths | ?J => 1}

puzzle_input
|> String.split()
|> Stream.chunk_every(2)
|> Stream.map(fn [hand, bid] ->
  cards = String.to_charlist(hand)
  freqs = cards |> Enum.frequencies()
  {jokers, freqs} = Map.pop(freqs, ?J, 0)
  freqs = freqs |> Map.values() |> Enum.sort(:desc)
  freqs = (freqs == []) && [5] || [hd(freqs) + jokers | tl(freqs)]
  scores = Enum.map(cards, &strengths[&1])
  {freqs, scores, String.to_integer(bid)}
end)
|> Enum.sort()
|> Stream.map(&elem(&1, 2))
|> Stream.with_index(1)
|> Stream.map(fn {bid, rank} -> bid * rank end)
|> Enum.sum()
code-shoily

code-shoily

I loved doing this. The way I decided to calculate rank through pattern matching ended up being visual cue to compute the J assisted ranking.

advent_of_code/lib/2023/day_07.ex at master · code-shoily/advent_of_code (github.com)

This felt so natural!

One valuable lesson learned though.

My smaller? function had @card_rank_1 as default parameter. And it is recursive, so when I called it with @card_rank_2 during part 2, I forgot to provide the second parameter on the recursive calls, so subsequent matches were lies! And to make it worse, sample data were immune to this ranking algorithm and kept giving me right answer so I spent some time in frustration.

Where Next?

Popular in Challenges Top

Aetherus
Today’s challenge is quite interesting. I ended up using Zipper to solve this problem. Maybe I overengineered quite a bit. The data stru...
New
sb8244
Note: This topic is to talk about Day 10 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
bjorng
Note: This topic is to talk about Day 1 of the Advent of Code 2019.
New
christhekeele
Setting this down for the night, as after a quick naive solve for quick part 1 I realize that part 2 is by design computationally expensi...
New
Aetherus
I tried to use combinatorial to solve today’s puzzles but failed (my brain burned out :exploding_head:). In the end I just used brute for...
New
Aetherus
This topic is about the Advent of Code 2021 - Day 4. Thanks to @bjorng , we now have a new Private Leaderboard. The entry code is: 370...
New
New
bjorng
This topic is about Day 15 of the Advent of Code 2021. We have a private leaderboard (shared with users of Erlang Forums): https://adve...
New
New
code-shoily
Here’s my day 3 code This was quite easy. I was afraid Part 2 would be “un-regex-able” and was preparing for hand crafting automata bu...
New

Other popular topics Top

belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
openscript
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
malloryerik
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement