Day 03

# puzzle prompt: https://adventofcode.com/2025/day/3

import time

from ..base.advent import *


class Solution(InputAsLinesSolution):
    _year = 2025
    _day = 3

    _is_debugging = False

    def TurnOnBatteries(self, input, howmany):
        total = 0
        banks = [[int(joltage) for joltage in bank] for bank in input]

        for bank in banks:
            length = len(bank)
            slots_available = howmany
            on = []
            offset = -1

            # doing slices in the bank, ensuring there are enough batteries left
            #
            # bank is 987654321111111 and i need 2 batteries
            # I can make slices of 14 (pick max from them and leave 1 battery), so my slices will be
            # [9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1],[8, 7, 6, 5, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1]
            #
            # bank is 987654321111111 and i need 12 batteries
            # I can make slices of 4 (pick max from them and leave 11 batteries), so my slices will be
            # [9, 8, 7, 6],[8, 7, 6, 5],[7, 6, 5, 4],...
            #
            # after picking a battery from a slice, I discard the batteries before the battery position,
            # they cannot be rearranged
            while slots_available:
                max_joltage = max(bank[offset + 1 : length - slots_available + 1])

                on.append(max_joltage)

                slots_available -= 1

                # recalculate the position where to start next slices
                offset = bank.index(max_joltage, offset + 1)

            # this is first_digit * 10^position + second_digit * 10^position, etc
            # [3,4] => 3*10^1 + 4*10^0
            total += sum(digit * 10 ** (len(on) - i - 1) for i, digit in enumerate(on))

        return total

    def pt1(self, input):
        self.debug(input)

        res = self.TurnOnBatteries(input, 2)

        return res

    def pt2(self, input):
        self.debug(input)

        res = self.TurnOnBatteries(input, 12)

        return res

    def part_1(self):
        start_time = time.time()

        res = self.pt1(self.input)

        end_time = time.time()

        self.solve("1", res, (end_time - start_time))

    def part_2(self):
        start_time = time.time()

        res = self.pt2(self.input)

        end_time = time.time()

        self.solve("2", res, (end_time - start_time))


if __name__ == "__main__":
    solution = Solution()

    solution.part_1()

    solution.part_2()