Last active
December 7, 2023 21:21
-
-
Save haakonvt/7f8ea1e419a3c5c4b95c009e514869c9 to your computer and use it in GitHub Desktop.
AOC 2023, day 7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Part 1: | |
from collections import Counter | |
ranks = [] | |
vals = dict(zip("AKQJT98765432", reversed(range(13)))) | |
for hand, bid in ((h, int(b)) for h, b in (l.split() for l in inp.splitlines())): | |
match Counter(hand).most_common(2): | |
case [(c, 5), *_]: | |
ranks.append((7, *map(vals.get, hand), bid)) | |
case [(c, 4), *_]: | |
ranks.append((6, *map(vals.get, hand), bid)) | |
case [(c1, 3), (c2, 2)]: | |
ranks.append((5, *map(vals.get, hand), bid)) | |
case [(c, 3), *_]: | |
ranks.append((4, *map(vals.get, hand), bid)) | |
case [(c1, 2), (c2, 2)]: | |
ranks.append((3, *map(vals.get, hand), bid)) | |
case [(c1, 2), *_]: | |
ranks.append((2, *map(vals.get, hand), bid)) | |
case _: | |
ranks.append((1, *map(vals.get, hand), bid)) | |
sum(r*bid for r, (*_, bid) in enumerate(sorted(ranks), 1)) | |
# Part 2 | |
ranks = [] | |
vals["J"] = -1 | |
to = lambda hand: tuple(vals[c] for c in hand) | |
for hand, bid in ((h, int(b)) for h, b in (l.split() for l in inp.splitlines())): | |
match hand.count("J"), Counter(hand.replace("J", "")).most_common(2): | |
case 5, []: | |
ranks.append((7, *to(hand), bid)) | |
case (j, [(_, n), *_]) if j + n == 5: | |
ranks.append((7, *to(hand), bid)) | |
case (j, [(_, n), *_]) if j + n == 4: | |
ranks.append((6, *to(hand), bid)) | |
case (j, [(_, n), (_, m)]) if j + n + m == 5: | |
ranks.append((5, *to(hand), bid)) | |
case (j, [(_, n), *_]) if j + n == 3: | |
ranks.append((4, *to(hand), bid)) | |
case (j, [(_, n), (_, m)]) if j + n + m == 4: | |
ranks.append((3, *to(hand), bid)) | |
case (0, [(_, 2), *_]) | (1, [(_, 1), *_]): | |
ranks.append((2, *to(hand), bid)) | |
case _: | |
ranks.append((1, *to(hand), bid)) | |
sum(r*bid for r, (*_, bid) in enumerate(sorted(ranks), 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment