day 6 + central testing

This commit is contained in:
Stefan Harmuth 2020-12-06 08:16:26 +01:00
parent d4380883b2
commit 6aa821c6f4
10 changed files with 2204 additions and 63 deletions

View File

@ -23,24 +23,29 @@ def getInputAsArray(day, return_type=None, test=False):
return input_array
def getMultiLineInputAsArray(day, return_type=None, test=False):
def getMultiLineInputAsArray(day, return_type=None, test=False, join_char=None):
"""
get input for day x as array, but all lines are concatenated,
except if split by an empty line
get input for day x as 2d array, split by empty lines
"""
lines = getInputAsArray(day=day, test=test)
lines.append('')
return_array = []
input_line = ''
line_array = []
for line in lines:
if not line:
return_array.append(input_line)
input_line = ''
if join_char:
return_array.append(join_char.join(line_array))
else:
return_array.append(line_array)
line_array = []
continue
input_line = (input_line + " " + line).strip()
if return_type:
line_array.append(return_type(line))
else:
line_array.append(line)
return_array.append(input_line)
return return_array

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3
import aoclib
import itertools
DAY = 1
TEST_SOLUTION_PART1 = 514579
TEST_SOLUTION_PART2 = 241861950
def getProductOf2020Sum(input_lines, combinations):
@ -19,18 +20,11 @@ def getProductOf2020Sum(input_lines, combinations):
@aoclib.print_execution_time
def part1(test_mode=False):
my_input = aoclib.getInputAsArray(day=1, return_type=int, test=test_mode)
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
return getProductOf2020Sum(my_input, 2)
@aoclib.print_execution_time
def part2(test_mode=False):
my_input = aoclib.getInputAsArray(day=1, return_type=int, test=test_mode)
my_input = aoclib.getInputAsArray(day=DAY, return_type=int, test=test_mode)
return getProductOf2020Sum(my_input, 3)
if __name__ == '__main__':
assert part1(test_mode=True) == 514579, "Part 1 TEST FAILED"
aoclib.printSolution(DAY, 1, part1())
assert part2(test_mode=True) == 241861950, "Part 2 TEST FAILED"
aoclib.printSolution(DAY, 2, part2())

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3
import aoclib
import re
DAY = 2
TEST_SOLUTION_PART1 = 2
TEST_SOLUTION_PART2 = 1
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
@ -30,10 +31,3 @@ def part2(test_mode=False):
valid_count = valid_count + 1
return valid_count
if __name__ == '__main__':
assert part1(test_mode=True) == 2, "Part 1 TEST FAILED"
aoclib.printSolution(DAY, 1, part1())
assert part2(test_mode=True) == 1, "Part 2 TEST FAILED"
aoclib.printSolution(DAY, 2, part2())

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python3
import aoclib
DAY = 3
TEST_SOLUTION_PART1 = 7
TEST_SOLUTION_PART2 = 336
def check_for_trees(right_step, down_step, treelines):
@ -34,10 +35,3 @@ def part2(test_mode=False):
tree_count4 = check_for_trees(7, 1, my_input)
tree_count5 = check_for_trees(1, 2, my_input)
return tree_count1 * tree_count2 * tree_count3 * tree_count4 * tree_count5
if __name__ == '__main__':
assert part1(test_mode=True) == 7, "Part 1 TEST FAILED"
aoclib.printSolution(DAY, 1, part1())
assert part2(test_mode=True) == 336, "Part 2 TEST FAILED"
aoclib.printSolution(DAY, 2, part2())

View File

@ -1,8 +1,9 @@
#!/usr/bin/env python3
import aoclib
import re
DAY = 4
TEST_SOLUTION_PART1 = 10
TEST_SOLUTION_PART2 = 6
required_fields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'] # 'cid' possible, but ignored
re_color_match = re.compile(r'#[a-z0-9]{6}')
@ -73,7 +74,7 @@ def verifyPassportData(passport_data, verify_data=False):
@aoclib.print_execution_time
def part1(test_mode=False):
valid = 0
for passport_line in aoclib.getMultiLineInputAsArray(day=4, test=test_mode):
for passport_line in aoclib.getMultiLineInputAsArray(day=4, join_char=" ", test=test_mode):
passport_data = getPassportData(passport_line)
if verifyPassportData(passport_data):
valid = valid + 1
@ -84,16 +85,9 @@ def part1(test_mode=False):
@aoclib.print_execution_time
def part2(test_mode=False):
valid = 0
for passport_line in aoclib.getMultiLineInputAsArray(day=4, test=test_mode):
for passport_line in aoclib.getMultiLineInputAsArray(day=4, join_char=" ", test=test_mode):
passport_data = getPassportData(passport_line)
if verifyPassportData(passport_data, True):
valid = valid + 1
return valid
if __name__ == '__main__':
assert part1(test_mode=True) == 10, "Part 1 TEST FAILED"
aoclib.printSolution(DAY, 1, part1())
assert part2(test_mode=True) == 6, "Part 2 TEST FAILED"
aoclib.printSolution(DAY, 2, part2())

View File

@ -1,7 +1,11 @@
#!/usr/bin/env python3
import aoclib
DAY = 5
TEST_SOLUTION_PART1 = 820
# sadly there's no real test input for part 2
# so just check for the expected outcome with the test set
# which has nothing to do with the problem description; it's just min(seat_id) + 1
TEST_SOLUTION_PART2 = 120
rows_predefined = list(range(0, 128))
columns_predefined = list(range(0, 8))
@ -65,13 +69,3 @@ def part2(test_mode=False):
for seat_id in seat_list:
if seat_id > min_id:
return seat_id
if __name__ == '__main__':
assert part1(test_mode=True) == 820, "Part 1 TEST FAILED"
aoclib.printSolution(DAY, 1, part1())
# sadly there's no real test input for part 2
# so just check for the expected outcome with the test set
# which has nothing to do with the problem description; it's just min(seat_id) + 1
assert part2(test_mode=True) == 120, "Part 2 TEST FAILED"
aoclib.printSolution(DAY, 2, part2())

44
day06.py Normal file
View File

@ -0,0 +1,44 @@
#!/usr/bin/env python3
import aoclib
import numpy
DAY = 6
TEST_SOLUTION_PART1 = 11
TEST_SOLUTION_PART2 = 6
def getAnswerCount(answer_line):
return_dict = {}
answer_line = ''.join(answer_line)
for answer in answer_line:
return_dict[answer] = return_dict.get(answer, 0) + 1
return return_dict
@aoclib.print_execution_time
def part1(test_mode=False):
my_input = aoclib.getMultiLineInputAsArray(day=DAY, test=test_mode)
yes_count = 0
for answer_group in my_input:
answer_count = getAnswerCount(answer_group)
yes_count += len(answer_count)
return yes_count
@aoclib.print_execution_time
def part2(test_mode=False):
my_input = aoclib.getMultiLineInputAsArray(day=DAY, test=test_mode)
yes_count = 0
for answer_group in my_input:
answer_buffer = list(answer_group[0])
for answer_line in answer_group:
answer_line = list(answer_line)
answer_buffer = list(numpy.intersect1d(answer_buffer, answer_line))
yes_count = yes_count + len(answer_buffer)
return yes_count

2093
inputs/6 Normal file

File diff suppressed because it is too large Load Diff

15
inputs/6_test Normal file
View File

@ -0,0 +1,15 @@
abc
a
b
c
ab
ac
a
a
a
a
b

30
main.py
View File

@ -27,13 +27,27 @@ for _, _, files in os.walk(aoclib.BASE_PATH):
for lib in sorted(imported):
day = int(lib[-2:])
if flags.part:
aoclib.printSolution(
day=day,
part=flags.part,
solution=getattr(globals()[lib], "part%d" % flags.part)(test_mode=flags.test),
test=flags.test
)
else:
if not flags.test:
if not flags.part or flags.part == 1:
test_part_1 = getattr(globals()[lib], "part1")(test_mode=True)
test_solution_1 = getattr(globals()[lib], "TEST_SOLUTION_PART1")
if test_part_1 != test_solution_1:
print(
"TEST FAILED for Day %s Part 1: Expected %s; Got %s"
% (day, test_solution_1, test_part_1)
)
if not flags.part or flags.part == 2:
test_part_2 = getattr(globals()[lib], "part2")(test_mode=True)
test_solution_2 = getattr(globals()[lib], "TEST_SOLUTION_PART2")
if test_part_2 != test_solution_2:
print(
"TEST FAILED for Day %s Part 2: Expected %s; Got %s"
% (day, test_solution_2, test_part_2)
)
if not flags.part or flags.part == 1:
aoclib.printSolution(day, 1, globals()[lib].part1(test_mode=flags.test), test=flags.test)
if not flags.part or flags.part == 2:
aoclib.printSolution(day, 2, globals()[lib].part2(test_mode=flags.test), test=flags.test)