starting 2018 in python

This commit is contained in:
Stefan Harmuth 2022-08-10 20:23:52 +02:00
parent 57a73a13f7
commit 2373b52af3
20 changed files with 4136 additions and 37 deletions

15
answer_cache.json Normal file
View File

@ -0,0 +1,15 @@
{
"4": {
"1": {
"wrong": [],
"correct": 95199
},
"2": {
"wrong": [
10923,
15226
],
"correct": 7887
}
}
}

View File

@ -1,19 +0,0 @@
import std/intsets
import std/sequtils
import tools/aoc
let test: AOCDay = initAOCDay(1)
printSolution(1, 1, test.getInputInt().foldl(a + b))
var seen: IntSet = initIntSet()
var freq: int = 0
var found: bool = false
while not found:
for x in test.getInputInt():
freq += x
if freq notin seen:
seen.incl(freq)
else:
printSolution(1, 2, freq)
found = true
break

View File

@ -1,17 +0,0 @@
import std/os
# Package
version = "0.1.0"
author = "Stefan Harmuth"
description = "aoc2018"
license = "GPL-3.0-or-later"
srcDir = "."
bin = @["aoc2018"]
# Dependencies
rmDir(joinPath(getHomeDir(), "/.nimble/pkgs/tools-#head"))
requires "nim >= 1.6.0"
requires "ssh://git@leeloo.drock.de/nim-tools.git#head"

View File

@ -1 +0,0 @@
switch("define", "release")

35
day01.py Normal file
View File

@ -0,0 +1,35 @@
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(522, "input1")
],
[
(73364, "input1")
]
]
def part1(self) -> Any:
freq = 0
for f in self.getInputListAsType(int):
freq += f
return freq
def part2(self) -> Any:
freq = 0
freq_set = set()
while True:
for f in self.getInputListAsType(int):
freq_set.add(freq)
freq += f
if freq in freq_set:
return freq
if __name__ == '__main__':
day = Day(2018, 1)
day.run(verbose=True)

52
day02.py Normal file
View File

@ -0,0 +1,52 @@
from collections import defaultdict
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(12, "test_input2_1"),
(7410, "input2")
],
[
('fgij', "test_input2_2"),
('cnjxoritzhvbosyewrmqhgkul', "input2")
]
]
def part1(self) -> Any:
twos, threes = 0, 0
for label in self.getInput():
count = defaultdict(int)
for x in label:
count[x] += 1
if 2 in count.values():
twos += 1
if 3 in count.values():
threes += 1
return twos * threes
def part2(self) -> Any:
for x, l1 in enumerate(self.getInput()):
for l2 in self.getInput()[x + 1:]:
miss = 0
miss_id = -1
for i, c in enumerate(l1):
if not c == l2[i]:
miss += 1
miss_id = i
if miss > 1:
break
if miss == 1:
return l1[:miss_id] + l1[miss_id + 1:]
if __name__ == '__main__':
day = Day(2018, 2)
day.run(verbose=True)

58
day03.py Normal file
View File

@ -0,0 +1,58 @@
from tools.aoc import AOCDay
from tools.coordinate import Square, Coordinate
from tools.grid import Grid
from typing import Any
class Day(AOCDay):
inputs = [
[
(121259, "input3")
],
[
(3, "test_input3"),
(239, "input3")
]
]
def getClaims(self) -> list:
claims = []
for line in self.getInput():
_, _, coord, size = line.split()
x, y = map(int, coord[:-1].split(","))
dx, dy = map(int, size.split("x"))
claims.append(Square(Coordinate(x, y), Coordinate(x + dx - 1, y + dy - 1)))
return claims
def part1(self) -> Any:
fabric = Grid()
for claim in self.getClaims():
fabric.add_shape(claim)
count = 0
for x in fabric.values():
if x > 1:
count += 1
return count
def part2(self) -> Any:
claims = self.getClaims()
for i, claim in enumerate(claims):
inter = False
for claim2 in claims:
if claim2 == claim:
continue
if claim.intersection(claim2):
inter = True
break
if not inter:
return i + 1
if __name__ == '__main__':
day = Day(2018, 3)
day.run(verbose=True)

79
day04.py Normal file
View File

@ -0,0 +1,79 @@
import datetime
from collections import defaultdict
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
inputs = [
[
(240, "test_input4_1"),
(95199, "input4")
],
[
(4455, "test_input4_1"),
(7887, "input4")
]
]
def get_guards(self) -> dict:
active_guard = 0
sleep_start = 0
guards = {}
for event in sorted(self.getInput()):
time, msg = event.split("] ")
timestamp = datetime.datetime.strptime(time, "[%Y-%m-%d %H:%M")
if msg.startswith("Guard"):
active_guard = int(msg.split()[1][1:])
if active_guard not in guards:
guards[active_guard] = {
'total': 0,
'minutes': defaultdict(int)
}
elif msg.startswith("falls asleep"):
if timestamp.hour == 0:
sleep_start = timestamp.minute
elif msg.startswith("wakes up"):
guards[active_guard]['total'] += timestamp.minute - sleep_start
for m in range(sleep_start, timestamp.minute):
guards[active_guard]['minutes'][m] += 1
return guards
def part1(self) -> Any:
guards = self.get_guards()
max_guard = 0
max_minutes = 0
most_minute = 0
for guard, data in guards.items():
if data['total'] < max_minutes:
continue
max_guard = guard
max_minutes = data['total']
most_minute = sorted(data['minutes'], key=lambda k: data['minutes'][k], reverse=True)[0]
return max_guard * most_minute
def part2(self) -> Any:
guards = self.get_guards()
max_guard = 0
max_minute = 0
max_time = 0
for guard, data in guards.items():
if not data['minutes']:
continue
most_minute = sorted(data['minutes'], key=lambda k: data['minutes'][k], reverse=True)[0]
if data['minutes'][most_minute] < max_time:
continue
max_guard = guard
max_time = data['minutes'][most_minute]
max_minute = most_minute
return max_guard * max_minute
if __name__ == '__main__':
day = Day(2018, 4)
day.run(verbose=True)

250
inputs/input2 Normal file
View File

@ -0,0 +1,250 @@
cnjxpritdzhubeseewfmqagkul
cwyxpgitdzhvbosyewfmqagkul
cnfxpritdzhebosywwfmqagkul
cnjxpritdzgvbosyawfiqagkul
cnkxpritdzhvbosyewfmgagkuh
gnjxprhtdzhebosyewfmqagkul
cnjxpriedzevbosyewfjqagkul
cnjxpritdzhpyosyewfsqagkul
cnjxprltdzhvbosyewfmhagkzl
cnjxfritdjhvbosyewfmiagkul
xnjxpritdzhvbosyewfmqagkgn
cnjxpritdzmvzosyewfhqagkul
cljxxritdzhvbosyewfmragkul
cnjxjritdzhvbovyewfmvagkul
cnjxprdtdzhpbosyewvmqagkul
cojxprdtdzhzbosyewfmqagkul
cnjxpritgzhvfgsyewfmqagkul
knjxprptdzhvbosyecfmqagkul
cnjxpritdzhvbvsyeyfmqagkuc
cnjxpritdzhvbosvewfmoagjul
cnjxpritdzhvbosyfwfmbagkjl
cnjxpjitazhvbosfewfmqagkul
cnjtpfitdzhvbosyewfmiagkul
ckjxpritdzhvbysyewfmqagoul
cnjxvritdzhvbfsyewfmqalkul
cnjipqitdzhvbosyewfeqagkul
cnjhpritdzhvbosyewymqjgkul
cnjxprrtdzhvbosyewfmlkgkul
cnjxnritdzhvbopyewfmqaskul
cxjxpritdzhvtosyewjmqagkul
cnjxpritdzhvbjsyewfrqagkwl
cnjxhritdzhubosyewfmqagvul
cnjxpritdzhvbosyyyfmeagkul
cnjxkritdzhvaoeyewfmqagkul
cnjxpritdzhvtotyewfmqazkul
cnjxoriadzhvbosyewfmqcgkul
cnjxpritdzhcbosyewfmkapkul
fnjxprtddzhvbosyewfmqagkul
cnjxmvitdzhvbosyewfmqagrul
cnjxpyitdzhibosyewfmqagktl
cyjxprxtdzhvbosyewbmqagkul
onjxpditdzhvbosyeofmqagkul
cnjxprixdzhvbosuewftqagkul
cnjxpritdrhvaosyewymqagkul
cnjxpritdzhhbokyewfvqagkul
cnjxpritczhvbosyewfmqwgxul
cnjxpribdzqvbnsyewfmqagkul
ynpxpritdzhvbvsyewfmqagkul
cnjxprirdzhvboerewfmqagkul
cnjxpritdxhvbosyewfmgavkul
cnwxprntdzhvbosyewfmqagkuk
cnjxpritzzhvbosyewfmcagktl
bbjxpritdzhvbosyetfmqagkul
cnjxpbitdzhvbosyewrmqagkui
cnjxwrildzcvbosyewfmqagkul
cnqxpoitdzhvbosnewfmqagkul
cnzxpritdzhvbosyewfmqazkfl
cnjxpriddzhvoosyewfmhagkul
znjxpritdzhvbosjewfmqagkur
cnjxpritdzhvbosyewcmfagkuk
cnjxpritdzhvbomyywnmqagkul
cnjxpgitjzhvbosyejfmqagkul
cnjxpkitdzjvbosyewfmqcgkul
cnjxpritduhvbosyewfmqfkkul
cnfxpritdzhvbgsyewfmqwgkul
cnjxpritdzhvbosywufmqaskul
cnjxprittzhvboryswfmqagkul
cndxpritpzrvbosyewfmqagkul
cnjxpritdzhvbosyewfwqazkum
cccxprmtdzhvbosyewfmqagkul
cnjxpzitdzhvlbsyewfmqagkul
cnjxdrigdzhvbosyewfmqagsul
fhjxpritdzhvbosyewfmqagkcl
cnjxpritdzhvdosyewffqagaul
cnjxprikdztvbosyekfmqagkul
cnjxpritdzhvbohiewfmqagkue
cnjxpritdzhvbowyetfmqegkul
cnuxpritdzhvbosyewmmqapkul
qnjxpritdzhvbosyewfmjakkul
cnjxpritdzlvbosyewiaqagkul
cnjxpritdzhpoosyewfmvagkul
cdjxpritdzhvboryewfbqagkul
cnjxppitxzhvbosyewymqagkul
cnjxpywtdzhvboiyewfmqagkul
cnjxpritdzpvbosyezfmqaqkul
cnjppritdghvbosyewfdqagkul
cmjxpritdzhvbosvewfmqagkup
cnjxoritdzhvbosylffmqagkul
cnjxfritdzhvbojyewfmqagkvl
cnjxpritdzhvbozyewgmqlgkul
cnjxlritdzhvbosyewfmqalkug
cnyxprittzhvbosyewfmsagkul
cnjxprytdzcvdosyewfmqagkul
ctjxpritdzhvbosyedfmqagkil
cnjxpvitdzhrbosyewfmqaekul
cnyxyritdzhvbospewfmqagkul
cnjxoritwzhvbosyewrmqhgkul
cnjxpritdzhjbosyqwsmqagkul
cnjzprindzhvbosyewfmqabkul
cnjspritdzhvbosysffmqagkul
cnxxpritdzhvbosyelfmqageul
bnjhpritdzhvbosyewfmzagkul
cnjxbhitdzhdbosyewfmqagkul
cnjxprktdzmvbosyewfmqagkuj
cnjxprixdzhvbqsyewfmqmgkul
cnjxpkitdzhvbosyewfmqagbum
cnjhpritdzhxbosyewfmqagjul
cnjxpritdzzvbosyewuqqagkul
cnjxprhtdzhvuopyewfmqagkul
cnjxpritdzhjqosyewfmqagkgl
cnzxpritdzhvbosyejfmuagkul
cnvxpritoohvbosyewfmqagkul
cnjxpmitdzwvbosyemfmqagkul
cnjoprittzzvbosyewfmqagkul
cnjxpgitdzhvbosytwfmqsgkul
cnjxprrtdehvbosyewfnqagkul
onjxpjitdzgvbosyewfmqagkul
cnjxpmitdzhvbopaewfmqagkul
cnjxpritqzhvbosoewfrqagkul
cnjxpnitdzhvbosyewfmqagkjy
cnsxpritdzhvbosyewfmqjykul
cnjxpriidzhvbosyewfmqxgkut
cnjxpyitdzhnbosyewfgqagkul
cnjxpritdzhbboyyewfmqagsul
cnjxpeitdzsvbosyewfmqabkul
cnjxpritdzhzvosyewfmragkul
cnjrpritdzhmbosyewfmqrgkul
cnjxpritdzhmbosyenfmqaglul
cnjxqrntdzhvboswewfmqagkul
cnjxprdtpzhvbosyewfmqagkcl
cnjxpritdzhvsdsyewfmqagkur
cnjxpritdzhvvosyewumqhgkul
cnzxpritdznvhosyewfmqagkul
ynjypritdzhvbosyewfmqagkuz
cnjxpnitdzhvbocyezfmqagkul
vnjxpritdzhvbfsyewfmjagkul
cnjfpritdzhvbosyewfmqagkzu
cnjxpritdzhbbosyewfmlegkul
cnjxpnitdzhvbosyesfmbagkul
cnjxpritezwvbosyewfmqagkgl
cujxpritdzhqbosyawfmqagkul
cnjxprindzhrbosyerfmqagkul
cntxpritdzhvbosyewfmqauxul
cnjxpvitdzhvbosyepfmqagkuy
cnjxdrqtdzhvbosdewfmqagkul
cnnxpritdzhvvosyenfmqagkul
lnjxphitdzhvbosyewfaqagkul
cngxpritdzhhbobyewfmqagkul
uncxphitdzhvbosyewfmqagkul
cnjxpribdehvbosfewfmqagkul
cnjxppitdqhvbmsyewfmqagkul
gnjxpritkzhvbosyewfbqagkul
znjxpritdzhvbowycwfmqagkul
cnjxpgitdzhvbosyewidqagkul
cnjxhritdzhvbowyswfmqagkul
injxkritdzhvbjsyewfmqagkul
cmjupritgzhvbosyewfmqagkul
cnjxpritdzbvjoeyewfmqagkul
cnjxpritdkhvbosyewlmuagkul
cnkxpritdzhebvsyewfmqagkul
cyjxptitdzhvbosyewfmqagkuv
cnjxpritdzhvbodrewflqagkul
cnjxpratdzhvbksyewfhqagkul
cnjxpoitdzhvbosjewxmqagkul
cnjxprhidzhvbasyewfmqagkul
cnjxpritdzhvbosqewvmqagmul
cnjxoritdzhvbosyzifmqagkul
mnjxpritdzhvbcsyeyfmqagkul
cnjhpritgzhvbosyewfmqngkul
cnjxprijdzevbesyewfmqagkul
cnexprqtdzhvbosyewvmqagkul
cnjxpxitdzhvbosyawfmqmgkul
cnjxpritdzhvbosyirfmqaxkul
cqjxpcitdzhvboslewfmqagkul
cmjxpqitdztvbosyewfmqagkul
cnbxpritdzhvfosyewfmuagkul
cnjxprrtdzhvbosumwfmqagkul
cnjxprttdvhvbossewfmqagkul
cnjxpritdzhvbcsuewfaqagkul
cbjxpritdzhvbosyewfhqalkul
cnjxprithzhvbosjcwfmqagkul
chjxpritdzhvbosyewftcagkul
cnjxprirdchvdosyewfmqagkul
cnjxpritdxhvbosyewfmqcgkal
cnjxpriidchvbosrewfmqagkul
cnjhprizdzhvbosyewfmqagvul
cnjwpritdzhpbosyewfmqaqkul
cnjxpgitfzhvbosyxwfmqagkul
cnjxpjiedzhvbosywwfmqagkul
cnjxpritdzhvbosyewfpqynkul
xnixlritdzhvbosyewfmqagkul
cnjxoritdznvbosyehfmqagkul
cnjxpritdzhvbjsyewsmqagcul
lnjxpritdzhvkosyewjmqagkul
cnjxpritdzhvbosyedfiqvgkul
cnjxpritdzhqbdsyerfmqagkul
cnjxpritdzavbosyywfmqagvul
dmjxprithzhvbosyewfmqagkul
cnjxpriqdzhvnosyeofmqagkul
cnjxpritdxhvboszewfmqkgkul
cnjxpritdzxvbosjewymqagkul
cnjxpritdzngbosyewfmqugkul
cajxpritdnhvbosyerfmqagkul
cnsxpritdzhvbosymwfmqagcul
cnjxoritdzhvbosyewrmqhgkul
cnjxpritdzhvposyewfmqagkwo
cnjxpriazzhvbosyeufmqagkul
cnjxrritdzhvbosymhfmqagkul
cnjxprztdzhvbosyewfmqtgkum
cnjxpritdzhvbmsyewfmqatkun
cnuxpritdzhvbosyewfmqagvur
ctjxxritdzhvbosyewfvqagkul
cnjxpritdzlvbosyevfmqagkll
cnjxpritdzhlbosyewfmqagasl
cnjxpritwzhvbosyewfcxagkul
cyjxpritdzhfbosyewfmqagcul
cnjxpritxghvkosyewfmqagkul
ctjxpritdjhvbosyewfmqkgkul
cnjxpritxzhvbosyewjmbagkul
unjxpritdzhkbosyewfmqaghul
cnjoprqtdzhvbosyewzmqagkul
rnjxprgtgzhvbosyewfmqagkul
cnjgpqitdzhvbosyewfaqagkul
cnjxpritdzuybosyewfmqagful
cnjxprqtdahvbosyewfnqagkul
cnjxpritdzhmkhsyewfmqagkul
wnjxpritdzhvbosiewfmqagkml
cnjmpritdzhvbosyjwfmqagkdl
cnjopritdzhvbksyewfmqrgkul
cnlxpritdzhvbosyewfmomgkul
cgjxpritdzhvbbsyewfmxagkul
cnaxpritdvhvnosyewfmqagkul
cnjxprijdzhvbkmyewfmqagkul
cnjxpritdzhvposyewzmqagkuz
cnuxpuitdzdvbosyewfmqagkul
cnjxprifdzjvbosyewfyqagkul
cnhspritdzhvbosyewfmqaghul
cnjxprcbdzfvbosyewfmqagkul
lnjapritdzhvbosyewfmqegkul
cnjxprisszhvbosyewqmqagkul
cnjxpritdzhvbosyeifmsagoul
cnjxpritrfhvbosyewfmqagkuz
cnjxkritdzmvboqyewfmqagkul
cnjxpritdzhvbosyedfmqzgkzl
cnjxprifdzhvbosyswfmqagksl
cnjxoritdzhvbosyxwfmhagkul
cnjhpritdzzvbosfewfmqagkul
cnjxprityjhvbomyewfmqagkul
cnjbpritdzhvbosyywfmqagkuf
cnjxprrtdzhvbosyewgmqagtul

1409
inputs/input3 Normal file

File diff suppressed because it is too large Load Diff

1044
inputs/input4 Normal file

File diff suppressed because it is too large Load Diff

7
inputs/test_input2_1 Normal file
View File

@ -0,0 +1,7 @@
abcdef
bababc
abbcde
abcccd
aabcdd
abcdee
ababab

7
inputs/test_input2_2 Normal file
View File

@ -0,0 +1,7 @@
abcde
fghij
klmno
pqrst
fguij
axcye
wvxyz

3
inputs/test_input3 Normal file
View File

@ -0,0 +1,3 @@
#1 @ 1,3: 4x4
#2 @ 3,1: 4x4
#3 @ 5,5: 2x2

1044
inputs/test_input4 Normal file

File diff suppressed because it is too large Load Diff

17
inputs/test_input4_1 Normal file
View File

@ -0,0 +1,17 @@
[1518-11-01 00:00] Guard #10 begins shift
[1518-11-01 00:05] falls asleep
[1518-11-01 00:25] wakes up
[1518-11-01 00:30] falls asleep
[1518-11-01 00:55] wakes up
[1518-11-01 23:58] Guard #99 begins shift
[1518-11-02 00:40] falls asleep
[1518-11-02 00:50] wakes up
[1518-11-03 00:05] Guard #10 begins shift
[1518-11-03 00:24] falls asleep
[1518-11-03 00:29] wakes up
[1518-11-04 00:02] Guard #99 begins shift
[1518-11-04 00:36] falls asleep
[1518-11-04 00:46] wakes up
[1518-11-05 00:03] Guard #99 begins shift
[1518-11-05 00:45] falls asleep
[1518-11-05 00:55] wakes up

41
main.py Normal file
View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
import tools.aoc
import argparse
import importlib
import os
YEAR = 2021
TIMEIT_NUMBER = 50
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument("-d", "--day", help="specify day to process; leave empty for ALL days", type=int)
argument_parser.add_argument("-p", "--part", help="run only part x", choices=[1, 2], type=int)
argument_parser.add_argument("--timeit", help="measure execution time", action="store_true", default=False)
argument_parser.add_argument(
"--timeit-number",
help="build average time over this many executions",
type=int,
default=TIMEIT_NUMBER
)
argument_parser.add_argument("-v", "--verbose", help="show test case outputs", action="store_true", default=False)
flags = argument_parser.parse_args()
import_day = ""
if flags.day:
import_day = "%02d" % flags.day
imported = []
for _, _, files in os.walk(tools.aoc.BASE_PATH):
for f in files:
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:])
day_class = getattr(globals()[lib], "Day")(YEAR, day)
day_class.run(flags.part if flags.part else 3, flags.verbose, flags.timeit, flags.timeit_number)

24
skel_day.py Normal file
View File

@ -0,0 +1,24 @@
from tools.aoc import AOCDay
from typing import Any
class Day(AOCDay):
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 Normal 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 = 2018
CHARMS = {
'Linux': '/usr/local/bin/charm',
'Windows': r'C:\Users\pennywise\AppData\Local\JetBrains\Toolbox\scripts\pycharm.cmd'
}
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])