-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepair_report.py
102 lines (78 loc) · 2.54 KB
/
repair_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
"""
Advent of Code 2020 - Day 1: Report Repair
"""
import argparse
import bisect
import os
import sys
from pathlib import Path
from typing import List
NULL_SEARCH = (None, None)
HALF_VALUE = 2020 / 2
def search_expenses(expense_list: List[int]) -> tuple:
"""
Search expense list for pair matching the sum requirement
:param expense_list: list of expenses
:return: null or pair of integers matching required total
"""
mid_index = bisect.bisect(expense_list, HALF_VALUE)
if not mid_index or mid_index == len(expense_list):
return NULL_SEARCH
lower_half = expense_list[:mid_index]
upper_half = expense_list[mid_index:]
for lower in lower_half:
for higher in upper_half:
if 2020 == lower + higher:
return lower, higher
return NULL_SEARCH
def repair(file: Path) -> any:
"""
Repair the given file by finding the incorrect expense entry pair
:param file: report file to repair
:return: tuple representing the incorrect expenses
"""
with open(file) as fp:
even_amount_expenses = []
odd_amount_expenses = []
even = lambda a: True == (a % 2)
def product(tuple_): return tuple_[0] * tuple_[1]
for line in fp.readlines():
if not len(line.strip()):
continue
amount = int(line)
if even(amount):
bisect.insort(even_amount_expenses, amount)
pair = search_expenses(even_amount_expenses)
if NULL_SEARCH != pair:
result = product(pair)
return result
else:
bisect.insort(odd_amount_expenses, amount)
pair = search_expenses(odd_amount_expenses)
if NULL_SEARCH != pair:
result = product(pair)
return result
return None
def main() -> int:
"""
Main function
:return: Shell exit code
"""
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('report_file', type=str, help='Report file to repair')
args = parser.parse_args()
report_file = Path(args.report_file)
if not os.path.isfile(report_file):
raise FileNotFoundError(f'File {report_file} does not exists')
product = repair(file=report_file)
if not product:
print(f'Failed to locate pair')
return 1
print(f'Found product: {product}')
return 0
if __name__ == '__main__':
"""
Command line entry-point
"""
sys.exit(main())