this post was submitted on 07 Dec 2023
16 points (100.0% liked)

Advent Of Code

158 readers
1 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2023

Solution Threads

M T W T F S S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 7: Camel Cards

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


๐Ÿ”’ Thread is locked until there's at least 100 2 star entries on the global leaderboard

๐Ÿ”“ Thread has been unlocked after around 20 mins

you are viewing a single comment's thread
view the rest of the comments
[โ€“] mykl 4 points 11 months ago* (last edited 11 months ago)

Dart

I'm glad I took the time to read the directions very carefully before starting coding :-)

Top Tip: my ranking of hand types relies on the fact that if you count instances of each face and sort the resulting list from high to low, you get a list that when compared with lists from other hands gives an exact correspondence with the order of the hand types as defined, so no need for a bunch of if/thens, just

  var type = Multiset.from(hand).counts.sorted(descending).join('');

Otherwise it should all be pretty self-explanatory apart from where I chose to map card rank to hex digits in order to facilitate sorting, so 'b' means 'J'!

int descending(T a, T b) => b.compareTo(a);
var cToH = "  23456789TJQKA"; // used to map card rank to hex for sorting.

handType(List hand, {wildcard = false}) {
  var type = Multiset.from(hand).counts.sorted(descending).join('');
  var i = hand.indexOf('b');
  return (!wildcard || i == -1)
      ? type
      : '23456789acde'
          .split('')
          .map((e) => handType(hand.toList()..[i] = e, wildcard: true))
          .fold(type, (s, t) => s.compareTo(t) >= 0 ? s : t);
}

solve(List lines, {wildcard = false}) => lines
    .map((e) {
      var l = e.split(' ');
      var hand =
          l.first.split('').map((e) => cToH.indexOf(e).toRadixString(16));
      var type = handType(hand.toList(), wildcard: wildcard);
      if (wildcard) hand = hand.map((e) => e == 'b' ? '0' : e);
      return (hand.join(), type, int.parse(l.last));
    })
    .sorted((a, b) {
      var c = a.$2.compareTo(b.$2);
      return (c == 0) ? a.$1.compareTo(b.$1) : c;
    })
    .indexed(offset: 1)
    .map((e) => e.value.$3 * e.index)
    .sum;

part1(List lines) => solve(lines);

part2(List lines) => solve(lines, wildcard: true);