-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday03.py
More file actions
23 lines (19 loc) · 832 Bytes
/
day03.py
File metadata and controls
23 lines (19 loc) · 832 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def day03(inp, part2=False):
banks = inp.strip().splitlines()
num_digits = 12 if part2 else 2
result = 0
for bank in banks:
digits = []
remaining_bank = bank
for digit_ind in range(num_digits, 0, -1):
# look for first digit in first (total - num_digits + 1) candidates
digits.append(max(remaining_bank[: len(remaining_bank) - digit_ind + 1]))
# ignore digits before just found local maximum for next digit
remaining_bank = remaining_bank[remaining_bank.index(digits[-1]) + 1 :]
result += int(''.join(digits))
return result
if __name__ == "__main__":
testinp = open('day03.testinp').read()
print(day03(testinp), day03(testinp, part2=True))
inp = open('day03.inp').read()
print(day03(inp), day03(inp, part2=True))