mirror of
https://github.com/Noettore/AdventOfCode.git
synced 2025-10-15 11:46:39 +02:00
AoC 2020: day4
Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
43
2020-python/solutions/day_1.py
Normal file
43
2020-python/solutions/day_1.py
Normal file
@@ -0,0 +1,43 @@
|
||||
"""AOC Day 1"""
|
||||
|
||||
import pathlib
|
||||
import time
|
||||
|
||||
def read_input(input_path: str) -> list:
|
||||
"""take input file path and return appropriate data structure"""
|
||||
with open(input_path, 'r') as input_file:
|
||||
entries = list()
|
||||
for entry in input_file.readlines():
|
||||
entries.append(int(entry))
|
||||
return entries
|
||||
|
||||
def part1(entries: list) -> int:
|
||||
"""part1 solver take a list of int and return an int"""
|
||||
for x in entries:
|
||||
complement = 2020 - x
|
||||
if complement in entries:
|
||||
return x * complement
|
||||
return None
|
||||
|
||||
def part2(entries: list) -> int:
|
||||
"""part2 solver take a list of int and return an int"""
|
||||
for x, i in enumerate(entries):
|
||||
for y in entries[i:]:
|
||||
complement = 2020 - x - y
|
||||
if complement in entries:
|
||||
return x * y * complement
|
||||
return None
|
||||
|
||||
|
||||
def main():
|
||||
"""main function"""
|
||||
input_path = str(pathlib.Path.cwd()) + "/inputs/" + str(pathlib.Path(__file__).stem)
|
||||
entries = read_input(input_path)
|
||||
start_time = time.time()
|
||||
print("Part 1: %d" % part1(entries))
|
||||
print("Part 2: %d" % part2(entries))
|
||||
end_time = time.time()
|
||||
print("Execution time: %f" % (end_time-start_time))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user