getting rid of exec() and ugly "TEST in globals()"
This commit is contained in:
parent
5f1e68270b
commit
d49d0bb8fe
38
day01.py
Normal file → Executable file
38
day01.py
Normal file → Executable file
@ -1,18 +1,30 @@
|
||||
#!/usr/bin/env python3
|
||||
import aoclib
|
||||
|
||||
if 'TEST' not in globals():
|
||||
TEST = False
|
||||
DAY = 1
|
||||
|
||||
my_input = aoclib.getInputLineAsArray(day=1, return_type=int, test=TEST)
|
||||
|
||||
globalBreak = False
|
||||
while len(my_input) > 0:
|
||||
try_value = my_input.pop()
|
||||
for value in my_input:
|
||||
if try_value + value == 2020:
|
||||
aoclib.printSolution(1, 1, try_value * value, test=TEST)
|
||||
globalBreak = True
|
||||
break
|
||||
def part1(test_mode=False):
|
||||
my_input = aoclib.getInputLineAsArray(day=1, return_type=int, test=test_mode)
|
||||
while len(my_input) > 0:
|
||||
try_value = my_input.pop()
|
||||
for value in my_input:
|
||||
if try_value + value == 2020:
|
||||
return try_value * value
|
||||
|
||||
if globalBreak:
|
||||
break
|
||||
|
||||
def part2(test_mode=False):
|
||||
my_input = aoclib.getInputLineAsArray(day=1, return_type=int, test=test_mode)
|
||||
while len(my_input) > 0:
|
||||
try_value = my_input.pop()
|
||||
for value_1 in my_input:
|
||||
for value_2 in my_input:
|
||||
if value_1 == value_2:
|
||||
continue
|
||||
if try_value + value_1 + value_2 == 2020:
|
||||
return try_value * value_1 * value_2
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
aoclib.printSolution(DAY, 1, part1())
|
||||
aoclib.printSolution(DAY, 2, part2())
|
||||
|
||||
24
day01_2.py
24
day01_2.py
@ -1,24 +0,0 @@
|
||||
import aoclib
|
||||
|
||||
if 'TEST' not in globals():
|
||||
TEST = False
|
||||
|
||||
my_input = aoclib.getInputLineAsArray(1, int, test=TEST)
|
||||
|
||||
globalBreak = False
|
||||
while len(my_input) > 0:
|
||||
try_value = my_input.pop()
|
||||
for value_1 in my_input:
|
||||
for value_2 in my_input:
|
||||
if value_1 == value_2:
|
||||
continue
|
||||
if try_value + value_1 + value_2 == 2020:
|
||||
aoclib.printSolution(1, 2, try_value * value_1 * value_2, test=TEST)
|
||||
globalBreak = True
|
||||
break
|
||||
|
||||
if globalBreak:
|
||||
break
|
||||
|
||||
if globalBreak:
|
||||
break
|
||||
43
day02.py
Normal file → Executable file
43
day02.py
Normal file → Executable file
@ -1,27 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
import aoclib
|
||||
import re
|
||||
|
||||
if 'TEST' not in globals():
|
||||
TEST = False
|
||||
|
||||
my_input = aoclib.getInputLineAsArray(day=2, test=TEST)
|
||||
DAY = 2
|
||||
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
|
||||
|
||||
|
||||
def getCharacterCount(password, character):
|
||||
count = 0
|
||||
for char in password:
|
||||
if char == character:
|
||||
count = count + 1
|
||||
def part1(test_mode=False):
|
||||
my_input = aoclib.getInputLineAsArray(day=2, test=test_mode)
|
||||
valid_count = 0
|
||||
for line in my_input:
|
||||
match = re.match(splitter, line)
|
||||
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
|
||||
if int(min_count) <= password.count(character) <= int(max_count):
|
||||
valid_count = valid_count + 1
|
||||
|
||||
return count
|
||||
return valid_count
|
||||
|
||||
|
||||
valid_count = 0
|
||||
for line in my_input:
|
||||
match = re.match(splitter, line)
|
||||
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
|
||||
if int(min_count) <= getCharacterCount(password, character) <= int(max_count):
|
||||
valid_count = valid_count + 1
|
||||
def part2(test_mode=False):
|
||||
my_input = aoclib.getInputLineAsArray(day=2, test=test_mode)
|
||||
valid_count = 0
|
||||
for line in my_input:
|
||||
match = re.match(splitter, line)
|
||||
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
|
||||
if (password[int(min_count) - 1] == character or password[int(max_count) - 1] == character) \
|
||||
and password[int(min_count) - 1] != password[int(max_count) - 1]:
|
||||
valid_count = valid_count + 1
|
||||
|
||||
aoclib.printSolution(2, 1, valid_count, TEST)
|
||||
return valid_count
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
aoclib.printSolution(DAY, 1, part1())
|
||||
aoclib.printSolution(DAY, 2, part2())
|
||||
|
||||
19
day02_2.py
19
day02_2.py
@ -1,19 +0,0 @@
|
||||
import aoclib
|
||||
import re
|
||||
|
||||
if 'TEST' not in globals():
|
||||
TEST = False
|
||||
|
||||
my_input = aoclib.getInputLineAsArray(day=2, test=TEST)
|
||||
splitter = re.compile(r'([0-9]+)-([0-9]+) ([a-z]): (.*)')
|
||||
|
||||
|
||||
valid_count = 0
|
||||
for line in my_input:
|
||||
match = re.match(splitter, line)
|
||||
(min_count, max_count, character, password) = match.group(1, 2, 3, 4)
|
||||
if (password[int(min_count) - 1] == character or password[int(max_count) - 1] == character) \
|
||||
and password[int(min_count) - 1] != password[int(max_count) - 1]:
|
||||
valid_count = valid_count + 1
|
||||
|
||||
aoclib.printSolution(2, 1, valid_count, TEST)
|
||||
38
main.py
38
main.py
@ -2,28 +2,38 @@
|
||||
|
||||
import argparse
|
||||
import aoclib
|
||||
import importlib
|
||||
import os
|
||||
|
||||
argument_parser = argparse.ArgumentParser()
|
||||
argument_parser.add_argument("day", help="specify day to process; leave empty for ALL days")
|
||||
argument_parser.add_argument("-t", "--test", help="also run test cases", action="store_true", default=False)
|
||||
argument_parser.add_argument("-d", "--debug", help="turn on debug mode", action="store_true", default=False)
|
||||
argument_parser.add_argument("-d", "--day", help="specify day to process; leave empty for ALL days", type=int)
|
||||
argument_parser.add_argument("-t", "--test", help="run test cases", action="store_true", default=False)
|
||||
argument_parser.add_argument("-p", "--part", help="run only part x", choices=[1, 2], type=int)
|
||||
flags = argument_parser.parse_args()
|
||||
|
||||
DEBUG = flags.debug
|
||||
|
||||
day = ""
|
||||
import_day = ""
|
||||
if flags.day:
|
||||
day = "%02d" % int(flags.day)
|
||||
import_day = "%02d" % flags.day
|
||||
|
||||
imported = []
|
||||
for _, _, files in os.walk(aoclib.BASE_PATH):
|
||||
for f in files:
|
||||
if f.startswith('day' + day) and f.endswith('.py'):
|
||||
if flags.test:
|
||||
TEST = True
|
||||
exec(open(f).read())
|
||||
else:
|
||||
TEST = False
|
||||
exec(open(f).read())
|
||||
if f.startswith('day' + import_day) and f.endswith('.py'):
|
||||
lib_name = f[:-3]
|
||||
globals()[lib_name] = importlib.import_module(lib_name)
|
||||
imported.append(lib_name)
|
||||
|
||||
break
|
||||
|
||||
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:
|
||||
aoclib.printSolution(day, 1, globals()[lib].part1(test_mode=flags.test), test=flags.test)
|
||||
aoclib.printSolution(day, 2, globals()[lib].part2(test_mode=flags.test), test=flags.test)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user