Restyled README and file structure

Signed-off-by: Ettore Dreucci <ettore.dreucci@gmail.com>
This commit is contained in:
2020-12-25 23:50:29 +01:00
parent 0ba35ffae1
commit d8db2fcd5d
102 changed files with 7 additions and 32 deletions

50
2015/solutions/day_1.py Normal file
View File

@@ -0,0 +1,50 @@
"""AOC 2015 Day 1"""
import pathlib
import time
TEST_INPUT = """))((((("""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def part1(input_data: str) -> int:
"""part1 solver take a str and return an int"""
return sum(1 if char == '(' else -1 for char in input_data)
def part2(input_data: str) -> int:
"""part2 solver take a dict of dicts and return an int"""
floor = 0
for index, char in enumerate(input_data, 1):
if char == '(':
floor += 1
else:
floor -=1
if floor == -1:
return index
return -1
def test_input_day_1():
"""pytest testing function"""
assert part1(TEST_INPUT) == 3
assert part2(TEST_INPUT) == 1
def test_bench_day_2(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
print("Part 1: %d" % part1(input_data))
print("Part 2: %d" % part2(input_data))
end_time = time.time()
print("Execution time: %f" % (end_time-start_time))
if __name__ == "__main__":
main()

66
2015/solutions/day_2.py Normal file
View File

@@ -0,0 +1,66 @@
"""AOC 2015 Day 2"""
import pathlib
import time
TEST_INPUT = """2x3x4
1x1x10"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def extract(input_data: str) -> list:
"""take input data and return the appropriate data structure"""
gifts = list()
for gift in input_data.split('\n'):
dims = tuple(int(dim) for dim in gift.split('x'))
gifts.append(dims)
return gifts
def part1(entries: list) -> int:
"""part1 solver take a list and return an int"""
feet = 0
for gift in entries:
(length, width, height) = gift
side1 = length*width
side2 = length*height
side3 = width*height
feet += 2 * (side1 + side2 + side3) + min(side1, side2, side3)
return feet
def part2(entries: list) -> int:
"""part2 solver take a list and return an int"""
feet = 0
for gift in entries:
(length, width, height) = gift
mins = list(gift)
mins.remove(max(gift))
feet += length*width*height + 2*sum(mins)
return feet
def test_input_day_2():
"""pytest testing function"""
entries = extract(TEST_INPUT)
assert part1(entries) == 101
assert part2(entries) == 48
def test_bench_day_2(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
entries = extract(input_data)
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()

69
2015/solutions/day_3.py Normal file
View File

@@ -0,0 +1,69 @@
"""AOC 2015 Day 3"""
import pathlib
import time
TEST_INPUT = """^v^v^v^v^v"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
moves = {
'^': (0, 1),
'v': (0, -1),
'>': (1, 0),
'<': (-1, 0),
}
def part1(entries: str) -> int:
"""part1 solver take a str and return an int"""
houses = {(0, 0): 1}
pos_x, pos_y = 0, 0
for direction in entries:
delta_x, delta_y = moves[direction]
pos_x += delta_x
pos_y += delta_y
houses[(pos_x, pos_y)] = houses.get((pos_x, pos_y), 0) + 1
return len(houses)
def part2(entries: str) -> int:
"""part2 solver take a str and return an int"""
houses = {(0, 0): 2}
pos_x_santa, pos_y_santa = 0, 0
pos_x_robot, pos_y_robot = 0, 0
for index, direction in enumerate(entries):
delta_x, delta_y = moves[direction]
if index%2 == 0:
pos_x_santa += delta_x
pos_y_santa += delta_y
houses[(pos_x_santa, pos_y_santa)] = houses.get((pos_x_santa, pos_y_santa), 0) + 1
else:
pos_x_robot += delta_x
pos_y_robot += delta_y
houses[(pos_x_robot, pos_y_robot)] = houses.get((pos_x_robot, pos_y_robot), 0) + 1
return len(houses)
def test_input_day_3():
"""pytest testing function"""
assert part1(TEST_INPUT) == 2
assert part2(TEST_INPUT) == 11
def test_bench_day_3(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
print("Part 1: %d" % part1(input_data))
print("Part 2: %d" % part2(input_data))
end_time = time.time()
print("Execution time: %f" % (end_time-start_time))
if __name__ == "__main__":
main()

55
2015/solutions/day_4.py Normal file
View File

@@ -0,0 +1,55 @@
"""AOC 2015 Day 4"""
import pathlib
import time
import hashlib
TEST_INPUT = """abcdef"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def part1(secret: str) -> int:
"""part1 solver"""
number = 1
while True:
key = secret + str(number)
md5 = hashlib.md5(key.encode()).hexdigest()
if md5.startswith('00000'):
return number
number += 1
def part2(secret: str) -> int:
"""part2 solver"""
number = 1
while True:
key = secret + str(number)
md5 = hashlib.md5(key.encode()).hexdigest()
if md5.startswith('000000'):
return number
number += 1
def test_input_day_4():
"""pytest testing function"""
assert part1(TEST_INPUT) == 609043
assert part2(TEST_INPUT) == 6742839
def test_bench_day_4(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
print("Part 1: %d" % part1(input_data))
print("Part 2: %d" % part2(input_data))
end_time = time.time()
print("Execution time: %f" % (end_time-start_time))
if __name__ == "__main__":
main()

82
2015/solutions/day_5.py Normal file
View File

@@ -0,0 +1,82 @@
"""AOC 2015 Day 5"""
import pathlib
import time
TEST_INPUT = """ugknbfddgicrmopn
aaa
jchzalrnumimnmhp
haegwjzuvuyypxyu
dvszwmarrgswjxmb"""
TEST_INPUT_2 = """qjhvhtzxzqqjkmpb
xxyxx
uurcxstgmygtbstg
ieodomkazucvgmuy"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def extract(input_data: str) -> list:
entries = list()
for line in input_data.split('\n'):
entries.append(line)
return entries
def part1(entries: list) -> int:
"""part1 solver"""
nice_strings = 0
for string in entries:
vowels_count = 0
doubles = False
forbidden = False
for index, char in enumerate(string):
if char in 'aeiou':
vowels_count += 1
if not doubles and index < len(string)-1 and char == string[index+1]:
doubles = True
for forbidden_substring in ('ab', 'cd', 'pq', 'xy'):
if forbidden:
break
if forbidden_substring in string:
forbidden = True
if vowels_count >= 3 and doubles and not forbidden:
nice_strings += 1
return nice_strings
def part2(entries: list) -> int:
"""part2 solver"""
nice_strings = 0
for string in entries:
if any([string.count(string[i:i+2]) >= 2 for i in range(len(string)-2)]) and any([string[i] == string[i+2] for i in range(len(string)-2)]):
nice_strings += 1
return nice_strings
def test_input_day_5():
"""pytest testing function"""
entries = extract(TEST_INPUT)
assert part1(entries) == 2
entries = extract(TEST_INPUT_2)
assert part2(entries) == 2
def test_bench_day_5(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
entries = extract(input_data)
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()

74
2015/solutions/day_6.py Normal file
View File

@@ -0,0 +1,74 @@
"""AOC 2015 Day 6"""
import pathlib
import time
import re
import numpy
TEST_INPUT = """turn on 0,0 through 999,999
toggle 0,0 through 999,0
turn off 499,499 through 500,500"""
def read_input(input_path: str) -> str:
"""take input file path and return a str with the file's content"""
with open(input_path, 'r') as input_file:
input_data = input_file.read().strip()
return input_data
def extract(input_data: str) -> list:
entries = list()
for line in input_data.split('\n'):
instructions = re.match(r"(\D+)(\d+),(\d+)[\D]+(\d+),(\d+)", line).groups()
entries.append((instructions[0].strip(), int(instructions[1]), int(instructions[2]), int(instructions[3]), int(instructions[4])))
return entries
def part1(entries: list) -> int:
"""part1 solver"""
grid = numpy.zeros((1000, 1000), 'int32')
for instruction in entries:
cmd, x1, y1, x2, y2 = instruction
if cmd == "turn on":
grid[x1:x2+1, y1:y2+1] = 1
elif cmd == "turn off":
grid[x1:x2+1, y1:y2+1] = 0
elif cmd == "toggle":
grid[x1:x2+1, y1:y2+1] = numpy.logical_not(grid[x1:x2+1, y1:y2+1])
return numpy.sum(grid)
def part2(entries: list) -> int:
"""part2 solver"""
grid = numpy.zeros((1000, 1000), 'int32')
for instruction in entries:
cmd, x1, y1, x2, y2 = instruction
if cmd == "turn on":
grid[x1:x2+1, y1:y2+1] += 1
elif cmd == "turn off":
grid[x1:x2+1, y1:y2+1] -= 1
grid[grid < 0] = 0
elif cmd == "toggle":
grid[x1:x2+1, y1:y2+1] += 2
return numpy.sum(grid)
def test_input_day_6():
"""pytest testing function"""
entries = extract(TEST_INPUT)
assert part1(entries) == 998996
assert part2(entries) == 1001996
def test_bench_day_6(benchmark):
"""pytest-benchmark function"""
benchmark(main)
def main():
"""main function"""
input_path = str(pathlib.Path(__file__).resolve().parent.parent) + "/inputs/" + str(pathlib.Path(__file__).stem)
start_time = time.time()
input_data = read_input(input_path)
entries = extract(input_data)
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()