add 2025 day 6 part 2
This commit is contained in:
117
2025/day-06/part-two.py
Normal file
117
2025/day-06/part-two.py
Normal file
@@ -0,0 +1,117 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
# Logger setup
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
stream_handler = logging.StreamHandler()
|
||||
logger.addHandler(stream_handler)
|
||||
|
||||
class Operation:
|
||||
def __init__(self, operation, count):
|
||||
self._operation = operation
|
||||
self._count = count
|
||||
self.numbers = []
|
||||
|
||||
@property
|
||||
def operation(self):
|
||||
"""The operation property."""
|
||||
return self._operation
|
||||
|
||||
@property
|
||||
def count(self):
|
||||
"""The count property."""
|
||||
return self._count
|
||||
|
||||
class Problem:
|
||||
def __init__(self, operation, numbers):
|
||||
self._operation = operation
|
||||
self._numbers = numbers
|
||||
|
||||
@property
|
||||
def operation(self):
|
||||
"""The operation property."""
|
||||
return self._operation
|
||||
|
||||
@operation.setter
|
||||
def operation(self, value):
|
||||
self._operation = value
|
||||
|
||||
@property
|
||||
def numbers(self):
|
||||
"""The numbers property."""
|
||||
return self._numbers
|
||||
|
||||
@numbers.setter
|
||||
def numbers(self, value):
|
||||
self._numbers = value
|
||||
|
||||
def result(self):
|
||||
result = 0
|
||||
match self._operation:
|
||||
case "*":
|
||||
result = self._numbers[0]
|
||||
for i in range(1, len(self._numbers)):
|
||||
result = result * self._numbers[i]
|
||||
case "+":
|
||||
result = self._numbers[0]
|
||||
for i in range(1, len(self._numbers)):
|
||||
result = result + self._numbers[i]
|
||||
case _:
|
||||
logger.error("Undefined operator")
|
||||
return result
|
||||
|
||||
def main():
|
||||
output = 0
|
||||
operations = []
|
||||
number_lines = []
|
||||
problems = []
|
||||
with open("input.txt", "r") as input:
|
||||
lines = input.readlines()
|
||||
i = 0
|
||||
while i < len(lines[-1]):
|
||||
count = 0
|
||||
op = lines[-1][i]
|
||||
i += 1
|
||||
while i < len(lines[-1]) and lines[-1][i] == " ":
|
||||
count += 1
|
||||
i += 1
|
||||
if i == len(lines[-1]) - 1:
|
||||
count += 1
|
||||
if count == 0:
|
||||
continue
|
||||
operations.append(Operation(op, count))
|
||||
|
||||
for line in lines[:len(lines) - 1]:
|
||||
i = 0
|
||||
op_count = 0
|
||||
while i < len(line):
|
||||
logger.debug("op_count = " + str(op_count))
|
||||
logger.debug("'" + line[i:i+operations[op_count].count] + "'")
|
||||
operations[op_count].numbers.append(line[i:i+operations[op_count].count])
|
||||
i += operations[op_count].count + 1
|
||||
op_count += 1
|
||||
|
||||
for i in range(len(operations)):
|
||||
operation = operations[i]
|
||||
numbers = operation.numbers
|
||||
logger.debug("Operation = " + operation.operation)
|
||||
logger.debug(numbers)
|
||||
ns = []
|
||||
for j in range(len(numbers[0])):
|
||||
logger.debug("j=" + str(j))
|
||||
logger.debug("numbers[j]=" + numbers[j])
|
||||
n = ""
|
||||
for k in range(len(numbers)):
|
||||
logger.debug("k=" + str(k))
|
||||
n += numbers[k][j]
|
||||
ns.append(int(n))
|
||||
problems.append(Problem(operation.operation, ns))
|
||||
|
||||
for problem in problems:
|
||||
output += problem.result()
|
||||
|
||||
logger.info("Output: " + str(output))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user