Advent of Code 2023 - Day 7

  Рет қаралды 424

Daniel Persson

Daniel Persson

Күн бұрын

Пікірлер
@WayneBagguley
@WayneBagguley Жыл бұрын
If you learn to use Streams then this will make your code more concise, easier to read, easer to reason about and not prone to array index out of bounds bugs. Kotlin streaming operations are much nicer IMO and both parts can be solved in less than 50 lines of code each. Also, if you need to declare a related number of statics then consider enums, it gives type safety and improves readability.
@Needforspeedfreak2nd
@Needforspeedfreak2nd Жыл бұрын
For this is i did mostly the same as you. I did it a lot more bloated with a Card class and a Hand class, but i really like my rankmeasurer and my check for 5ofakind etc. Combination here is just a enum that represents a number. so just for better readability i first got the amount of each card i have, and depending on how many diffrent cards i got i only ad to handle two edgecases, namely fullhouse and double pair. also made the joker handling fairly trivial. var groups = new Dictionary(); foreach (var c in this.Cards) { if (!groups.ContainsKey(c.Symbol)) { groups.Add(c.Symbol, 0); } groups[c.Symbol]++; } int jokerAmount = 0; if (groups.ContainsKey("J")) { jokerAmount = groups["J"]; } switch (groups.Count - (jokerAmount == 0 ? 0:1)) { case 0: case 1: return Combination.Quint; case 2: if (groups.Any(c=>c.Value+jokerAmount == 4)) { return Combination.Quads; } else { return Combination.FullHouse; } case 3: if (groups.Any(c=>c.Value + jokerAmount == 3)) { return Combination.Triple; } else { return Combination.DoublePair; } case 4: return Combination.Pair; case 5: return Combination.HighCard; } As for determening the strength of each hand i just dumped it all into a huge number and compared those. base 16 because on one hand i only got 14 values anyway and it is a power of two so that is always nice public double Strength { get { double str = 0; for (int i = 0; i < this.Cards.Length; i++) { str += Math.Pow(16, (this.Cards.Length - 1 - i)) * this.Cards[i].Value; } return str; } } the rest of the code is a MESS tho github.com/TDietrich99/AoC2023/blob/master/7/7/Program.cs
@DanielPersson
@DanielPersson Жыл бұрын
Hi NFSF Yes, this one was a messy one but I think if I got a second try with it I would probably structure my code differently. Thank you for sharing so more people can benefit from your code. It doesn't matter how messy the solution is there is always learnings crammed in there somewhere :) Thank you for watching my videos. Best regards Daniel
@WayneBagguley
@WayneBagguley Жыл бұрын
Why do you fill a new int array with zeros? It's initialised with zeros by default. @10:26
@DanielPersson
@DanielPersson Жыл бұрын
Probably over doing it. :)
Advent of Code 2023 - Day 8
27:51
Daniel Persson
Рет қаралды 699
Coding in a Random Programming Language Everyday (Huge Mistake)
17:17
Advent of Code 2023 | General Tips
15:31
HyperNeutrino
Рет қаралды 10 М.
Advent of Code 2024 Go - Day 1: Historian Hysteria
11:29
Josh Ackland
Рет қаралды 2,6 М.
What Makes Rust Different?
12:38
No Boilerplate
Рет қаралды 207 М.
Programming a multiplayer game from scratch in 7 DAYS
18:28
Introducing Clay - High Performance UI Layout in C
35:19
Nic Barker
Рет қаралды 183 М.
Advent of Code 2023 Day 5: If You Give A Seed A Fertilizer
17:02
HyperNeutrino
Рет қаралды 13 М.
OpenAI's o1 just hacked the system
26:31
AI Search
Рет қаралды 68 М.
Advent of Code 2023 - Day 8
25:13
Jonathan Paulson
Рет қаралды 8 М.
Tools EVERY Software Engineer Should Know
11:37
Tech With Tim
Рет қаралды 25 М.