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 Жыл бұрын
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 Жыл бұрын
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 Жыл бұрын
Why do you fill a new int array with zeros? It's initialised with zeros by default. @10:26