start_day + day16 (p1 autosubmitted \o/; p2 suxx and will take some more time)

This commit is contained in:
Stefan Harmuth 2021-12-28 18:04:18 +01:00
parent 5ea244cfd9
commit 895ad08565
7 changed files with 128 additions and 2 deletions

60
day16.py Normal file
View File

@ -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)

1
inputs/test_input16 Normal file
View File

@ -0,0 +1 @@
80871224585914546619083218645595

1
inputs/test_input16_2 Normal file
View File

@ -0,0 +1 @@
19617804207202209144916044189917

1
inputs/test_input16_3 Normal file
View File

@ -0,0 +1 @@
69317163492948606335995924319873

1
inputs/test_input16_4 Normal file
View File

@ -0,0 +1 @@
03036732577212944063491565474664

View File

@ -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)

51
start_day.py Executable file
View File

@ -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])