JonathanGupton

Advent of Code 2024 - Day 07 - Python

Dec 7th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.89 KB | None | 0 0
  1. from typing import Sequence
  2. from operator import add
  3. from operator import mul
  4.  
  5.  
  6. def parse_data(fp: str) -> list[tuple[int, tuple[int, ...]], ...]:
  7.     out = []
  8.     with open(fp, "r") as f:
  9.         for line in f.readlines():
  10.             result, nums = line.split(": ")
  11.             result = int(result)
  12.             nums = tuple(map(int, nums.split(" ")))
  13.             out.append((result, nums))
  14.     return out
  15.  
  16.  
  17. def conc(x: int, y: int) -> int:
  18.     """Concatenate two integers"""
  19.     return int(str(x) + str(y))
  20.  
  21.  
  22. def count_valid_operator_configurations(
  23.     equations: list[tuple[int, tuple[int, ...]], ...]
  24. ) -> int:
  25.     def _valid_configuration(
  26.         target: int, lop: int, rops: Sequence[int], total: int = 0
  27.     ) -> int:
  28.         if target == lop and not rops:
  29.             return 1
  30.         if lop > target or not rops:
  31.             return False
  32.         for op in [add, mul]:
  33.             new_lop = op(lop, rops[0])
  34.             new_rops = tuple(rops[1:])
  35.             total += _valid_configuration(target, new_lop, new_rops)
  36.         return total
  37.  
  38.     total_valid_configurations = 0
  39.     for eq in equations:
  40.         target, nums = eq
  41.         lops, nums = nums[0], tuple(nums[1:])
  42.         if _valid_configuration(target, lops, nums):
  43.             total_valid_configurations += target
  44.         # total_valid_configurations += _valid_configuration(target, lops, nums)
  45.     return total_valid_configurations
  46.  
  47.  
  48. def count_valid_operator_configurations2(
  49.     equations: list[tuple[int, tuple[int, ...]], ...]
  50. ) -> int:
  51.     def _valid_configuration(
  52.         target: int, lop: int, rops: Sequence[int], total: int = 0
  53.     ) -> int:
  54.         if target == lop and not rops:
  55.             return 1
  56.         if lop > target or not rops:
  57.             return False
  58.         for op in [add, mul, conc]:
  59.             new_lop = op(lop, rops[0])
  60.             new_rops = tuple(rops[1:])
  61.             total += _valid_configuration(target, new_lop, new_rops)
  62.         return total
  63.  
  64.     total_valid_configurations = 0
  65.     for eq in equations:
  66.         target, nums = eq
  67.         lops, nums = nums[0], tuple(nums[1:])
  68.         if _valid_configuration(target, lops, nums):
  69.             total_valid_configurations += target
  70.     return total_valid_configurations
  71.  
  72.  
  73. def part_a_example1():
  74.     fp = "./example/day07-example01.txt"
  75.     data = parse_data(fp)
  76.     print(count_valid_operator_configurations(data))
  77.  
  78.  
  79. def part_a():
  80.     fp = "./data/day07.txt"
  81.     data = parse_data(fp)
  82.     print(count_valid_operator_configurations(data))
  83.  
  84.  
  85. def part_b_example1():
  86.     fp = "./example/day07-example01.txt"
  87.     data = parse_data(fp)
  88.     print(count_valid_operator_configurations2(data))
  89.  
  90.  
  91. def part_b():
  92.     fp = "./data/day07.txt"
  93.     data = parse_data(fp)
  94.     print(count_valid_operator_configurations2(data))
  95.  
  96.  
  97. if __name__ == "__main__":
  98.     part_a_example1()
  99.     part_a()
  100.     part_b_example1()
  101.     part_b()
  102.  
Advertisement
Add Comment
Please, Sign In to add comment