day7.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import os
  2. from collections import Counter, defaultdict
  3. with open(os.path.join(os.path.dirname(__file__), "example.txt")) as example:
  4. example_data = example.read().splitlines()
  5. with open(os.path.join(os.path.dirname(__file__), "input.txt")) as example:
  6. input_data = example.read().splitlines()
  7. def solve1(data):
  8. hands = {}
  9. cc = {}
  10. groups = defaultdict(list)
  11. ordered = []
  12. mapping = str.maketrans({"T": "A", "J": "B", "Q": "C", "K": "D", "A": "E"})
  13. for ind, line in enumerate(data):
  14. hand, bid = line.split()
  15. hands[ind] = [hand, bid]
  16. for i, (hand, bid) in hands.items():
  17. cc[i] = sorted(Counter(hand).items(), key=lambda x: (x[1]), reverse=True)
  18. rotveller = sorted(
  19. cc.items(),
  20. key=lambda x: (x[1][0][1], x[1][1][1]) if len(x[1]) > 1 else (x[1][0][1], 1),
  21. reverse=True,
  22. )
  23. for ind, rot in rotveller:
  24. if len(rot) == 1:
  25. groups[f"{rot[0][1]}"].append((ind, hands[ind]))
  26. else:
  27. groups[f"{rot[0][1]}_{rot[1][1]}"].append((ind, hands[ind]))
  28. for k, v in groups.items():
  29. s_g = sorted(v, key=lambda x: x[1][0].translate(mapping), reverse=True)
  30. for el in s_g:
  31. ordered.append((k, el))
  32. s = 0
  33. for rank, hand in zip(reversed(range(1, len(ordered) + 1)), ordered):
  34. k, hand = hand
  35. s += int(hands[hand[0]][1]) * rank
  36. print(s)
  37. return s
  38. def solve2(data):
  39. hands = {}
  40. cc = {}
  41. groups = defaultdict(list)
  42. ordered = []
  43. mapping = str.maketrans({"T": "A", "J": "1", "Q": "C", "K": "D", "A": "E"})
  44. for ind, line in enumerate(data):
  45. hand, bid = line.split()
  46. hands[ind] = [hand, bid]
  47. for i, (hand, bid) in hands.items():
  48. c = sorted(Counter(hand).items(), key=lambda x: (x[1]), reverse=True)
  49. if c[0][0] == "J":
  50. if len(c) > 1:
  51. a = c[1][0]
  52. else:
  53. a = "J"
  54. else:
  55. a = c[0][0]
  56. cc[i] = sorted(
  57. Counter(hand.replace("J", a)).items(), key=lambda x: (x[1]), reverse=True
  58. )
  59. rotveller = sorted(
  60. cc.items(),
  61. key=lambda x: (x[1][0][1], x[1][1][1]) if len(x[1]) > 1 else (x[1][0][1], 1),
  62. reverse=True,
  63. )
  64. for ind, rot in rotveller:
  65. if len(rot) == 1:
  66. groups[f"{rot[0][1]}"].append((ind, hands[ind]))
  67. else:
  68. groups[f"{rot[0][1]}_{rot[1][1]}"].append((ind, hands[ind]))
  69. for k, v in groups.items():
  70. s_g = sorted(v, key=lambda x: x[1][0].translate(mapping), reverse=True)
  71. for el in s_g:
  72. ordered.append((k, el))
  73. s = 0
  74. for rank, hand in zip(reversed(range(1, len(ordered) + 1)), ordered):
  75. k, hand = hand
  76. s += int(hands[hand[0]][1]) * rank
  77. print(s)
  78. return s
  79. solve1(example_data)
  80. solve1(input_data)
  81. solve2(example_data)
  82. solve2(input_data)