feat: '25 Day 3
This commit is contained in:
parent
16e0308cfa
commit
f0032ec222
60
25/3/main.py
Normal file
60
25/3/main.py
Normal file
@ -0,0 +1,60 @@
|
||||
# from itertools import combinations
|
||||
|
||||
def get_highest(input: str) -> int:
|
||||
highest = 0
|
||||
|
||||
for index, num in enumerate(input):
|
||||
for subindex, subnum in enumerate(input):
|
||||
if subindex > index:
|
||||
highest = max(highest, int(num + subnum))
|
||||
|
||||
return highest
|
||||
|
||||
|
||||
def get_highest_dynamic(bank_size: int, input: str) -> int:
|
||||
# highest = 0
|
||||
|
||||
# Brute and "works", but is too expensive
|
||||
# combos = combinations(input, 12)
|
||||
|
||||
# for c in combos:
|
||||
# highest = max(highest, int(''.join(c)))
|
||||
|
||||
highest = ''
|
||||
curr_index = 0
|
||||
|
||||
for i in range(bank_size, 0, -1):
|
||||
curr_highest = 0
|
||||
highest_index = 0
|
||||
limit = len(input) - i + 1
|
||||
|
||||
for j in range(curr_index, limit):
|
||||
found = int(input[j])
|
||||
|
||||
if found > curr_highest:
|
||||
curr_highest = found
|
||||
highest_index = j
|
||||
|
||||
highest += str(curr_highest)
|
||||
curr_index = highest_index + 1
|
||||
|
||||
return int(highest)
|
||||
|
||||
|
||||
def main():
|
||||
'''Entrypoint'''
|
||||
with open('input.txt', encoding='utf-8') as fh:
|
||||
sum = 0
|
||||
sum2 = 0
|
||||
|
||||
for line in fh:
|
||||
# sum += get_highest(line) # Old style
|
||||
sum += get_highest_dynamic(2, line.strip())
|
||||
sum2 += get_highest_dynamic(12, line.strip())
|
||||
|
||||
print(f'Part 1: {sum}')
|
||||
print(f'Part 2: {sum2}')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
25
25/3/test_main.py
Normal file
25
25/3/test_main.py
Normal file
@ -0,0 +1,25 @@
|
||||
import pytest
|
||||
|
||||
from main import get_highest, get_highest_dynamic
|
||||
|
||||
@pytest.mark.parametrize('given, expected',
|
||||
[
|
||||
('987654321111111', 98),
|
||||
('811111111111119', 89),
|
||||
('234234234234278', 78),
|
||||
('818181911112111', 92)
|
||||
])
|
||||
def test_get_highest(given, expected):
|
||||
assert get_highest(given) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize('given, expected',
|
||||
[
|
||||
('987654321111111', 987654321111),
|
||||
('811111111111119', 811111111119),
|
||||
('234234234234278', 434234234278),
|
||||
('818181911112111', 888911112111),
|
||||
('4123535244222342322334342233754335452333242522124322242423331132232242422443224231234323332243364522', 755554464522)
|
||||
])
|
||||
def test_get_even_highester(given, expected):
|
||||
assert get_highest_dynamic(12, given) == expected
|
||||
Loading…
x
Reference in New Issue
Block a user