diff --git a/day16.py b/day16.py new file mode 100644 index 0000000..fff8003 --- /dev/null +++ b/day16.py @@ -0,0 +1,60 @@ +from tools.aoc import AOCDay +from typing import Any + + +class Day(AOCDay): + inputs = [ + [ + (24176176, "test_input16"), + (73745418, "test_input16_2"), + (52432133, "test_input16_3"), + (59522422, "input16"), + ], + [ + (84462026, "test_input16_4"), + (None, "input16"), + ] + ] + + def part1(self) -> Any: + base_pattern = [0, 1, 0, -1] + the_number = self.getInput() + + for r in range(100): + new_number = 0 + for l in range(len(the_number)): + mul_sum = 0 + for i, c in enumerate(the_number): + pattern_index = ((i + 1) // (l + 1)) % 4 + mul_sum += ((ord(c) - 48) * (2 - pattern_index if pattern_index else 0)) + + new_number = new_number * 10 + abs(mul_sum) % 10 + + the_number = "0" * (len(the_number) - len(str(new_number))) + str(new_number) + + return int(the_number[:8]) + + def part2(self) -> Any: + base_pattern = [0, 1, 0, -1] + the_number = 10000 * self.getInput() + + for r in range(100): + print("Round", r) + new_number = 0 + for l in range(len(the_number)): + mul_sum = 0 + for i, c in enumerate(the_number): + pattern_index = ((i + 1) // (l + 1)) % 4 + mul_sum += ((ord(c) - 48) * (2 - pattern_index if pattern_index else 0)) + + new_number = new_number * 10 + abs(mul_sum) % 10 + + the_number = "0" * (len(the_number) - len(str(new_number))) + str(new_number) + + offset = int(the_number[:8]) + return int(the_number[offset + 1:offset + 8]) + + +if __name__ == '__main__': + day = Day(2019, 16) + day.run(verbose=True) diff --git a/inputs/test_input16 b/inputs/test_input16 new file mode 100644 index 0000000..a0d6069 --- /dev/null +++ b/inputs/test_input16 @@ -0,0 +1 @@ +80871224585914546619083218645595 diff --git a/inputs/test_input16_2 b/inputs/test_input16_2 new file mode 100644 index 0000000..e62b53a --- /dev/null +++ b/inputs/test_input16_2 @@ -0,0 +1 @@ +19617804207202209144916044189917 diff --git a/inputs/test_input16_3 b/inputs/test_input16_3 new file mode 100644 index 0000000..fc400a2 --- /dev/null +++ b/inputs/test_input16_3 @@ -0,0 +1 @@ +69317163492948606335995924319873 diff --git a/inputs/test_input16_4 b/inputs/test_input16_4 new file mode 100644 index 0000000..7e44d27 --- /dev/null +++ b/inputs/test_input16_4 @@ -0,0 +1 @@ +03036732577212944063491565474664 diff --git a/skel_day.py b/skel_day.py index 2744f2a..bf3331b 100644 --- a/skel_day.py +++ b/skel_day.py @@ -3,11 +3,22 @@ from typing import Any class Day(AOCDay): - test_solutions_p1 = [] - test_solutions_p2 = [] + inputs = [ + [ + (None, "input%DAY%"), + ], + [ + (None, "input%DAY%"), + ] + ] def part1(self) -> Any: return "" def part2(self) -> Any: return "" + + +if __name__ == '__main__': + day = Day(%YEAR%, %DAY%) + day.run(verbose=True) diff --git a/start_day.py b/start_day.py new file mode 100755 index 0000000..5faa1f1 --- /dev/null +++ b/start_day.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +from argparse import ArgumentParser +from datetime import datetime +from os.path import exists +from platform import system +from subprocess import call +from time import sleep +import webbrowser + + +YEAR = 2019 +CHARMS = { + 'Linux': '/usr/local/bin/charm', + 'Windows': r'C:\somewhere\pycharm64.exe' +} + +arg_parser = ArgumentParser() +arg_parser.add_argument("-d", "--day", help="start a specific day (default: today)", type=int) +args = arg_parser.parse_args() + +DAY = args.day or datetime.now().day + +if YEAR < 2015 or not 1 <= DAY <= 25: + print("Invalid year or day for year: %d, day: %d" % (YEAR, DAY)) + exit() + +day_file = "day%02d.py" % DAY +if exists(day_file): + print(day_file, "already exists. Use that one!") + exit() + +with open("skel_day.py", "r") as IN: + with open(day_file, "w") as OUT: + while in_line := IN.readline(): + OUT.write(in_line.replace("%YEAR%", str(YEAR)).replace("%DAY%", str(DAY))) + +start = datetime(YEAR, 12, DAY, 6, 0, 0) +now = datetime.now() +if start > now: + time_wait = start - now + if time_wait.days > 0: + print("Do you really want to wait %d days?" % time_wait.days) + exit() + + for x in range(time_wait.seconds, -1, -1): + print("Day starts in %02ds.\r") + sleep(1) + +call([CHARMS[system()], day_file]) +webbrowser.open("https://adventofcode.com/%d/day/%d" % (YEAR, DAY)) +call(["git", "add", day_file])