AoC 2020: day4

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-12-04 11:38:40 +01:00
parent 12c90115a7
commit 5e8eb10c92
46 changed files with 1174 additions and 0 deletions

View File

@@ -1,43 +0,0 @@
"""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()

View File

@@ -1,49 +0,0 @@
"""AOC Day 2"""
import pathlib
import time
import re
def read_input(input_path: str) -> list:
"""take input file path and return appropriate data structure"""
with open(input_path, 'r') as input_file:
rules = list()
for entry in input_file.readlines():
splitted = re.split('-| |: ', entry)
rule = [int(splitted[0]), int(splitted[1]), splitted[2], splitted[3]]
rules.append(rule)
return rules
def part1(entries: list) -> int:
"""part1 solver take a list of tuples and return an int"""
correct_passwords = 0
for entry in entries:
occurrences = entry[3].count(entry[2])
if occurrences in range(entry[0], entry[1]+1):
correct_passwords += 1
return correct_passwords
def part2(entries: list) -> int:
"""part2 solver take a list of tuples and return an int"""
correct_passwords = 0
for entry in entries:
pos_1, pos_2, letter, password = entry[:]
if (password[pos_1-1] == letter) != (password[pos_2-1] == letter):
correct_passwords += 1
return correct_passwords
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/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()

View File

@@ -1,59 +0,0 @@
"""AOC Day 3"""
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:
lines = list()
for line in input_file.readlines():
lines.append(line.strip())
return lines
def slope_tree_check(lines: list, dx: int, dy: int) -> int:
"""check how many trees would be encountered with a specific slope"""
x_pos = 0
y_pos = 0
trees_encountered = 0
line_length = len(lines[0])
while y_pos < len(lines):
if lines[y_pos][x_pos] == '#':
trees_encountered += 1
x_pos = (x_pos + dx) % line_length
y_pos += dy
return trees_encountered
def part1(entries: list) -> int:
"""part1 solver take a list of strings and return an int"""
return slope_tree_check(entries, 3, 1)
def part2(entries: list) -> int:
"""part2 solver take a list of tuples and return an int"""
slopes = [
(1, 1),
(3, 1),
(5, 1),
(7, 1),
(1, 2),
]
prod = 1
for slope in slopes:
prod *= slope_tree_check(entries, slope[0], slope[1])
return prod
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/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()