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

ehayun
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
sneako
Note by the Moderators: This topic is to talk about the first day of the Advent of Code. For general discussion about the Advent of Code...
New
kwando
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
groovyda
Today’s challenge for me was about using reduce: defmodule Prob5 do def move([[h1 | rest] = _list1, list2]) do [rest, [h1 | list2]...
New
bjorng
Note: This topic is to talk about Day 3 of the Advent of Code 2019 . There is a private leaderboard for elixirforum members. You can jo...
New
cblavier
Hey there :wave: No magic or algorithmic finesse today, I just finished the challenge and I my code is quite slow (1sec for part1, 3se...
New
New
Aetherus
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
kwando
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
rvnash
Anyone have a solution to Part 2 today? Part 1 was straight forward, but I can’t figure out a programatic way to do part 2. I understand ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
lessless
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
JorisKok
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
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement