Skip to content

Commit 9fee9e1

Browse files
committed
day01
0 parents  commit 9fee9e1

File tree

9 files changed

+4178
-0
lines changed

9 files changed

+4178
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

adventofcode

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
2+
from cleo import Application, Command as BaseCommand
3+
from importlib import import_module
4+
from pathlib import Path
5+
from traceback import print_exc
6+
from time import perf_counter
7+
from functools import wraps
8+
9+
10+
def perf(f):
11+
@wraps(f)
12+
def wrapper(*args, **kwds):
13+
start_time = perf_counter()
14+
result = f(*args, **kwds)
15+
print(perf_counter() - start_time)
16+
return result
17+
return wrapper
18+
19+
20+
ROOT_PATH = Path(__file__).resolve().parent
21+
22+
23+
class RunCommand(BaseCommand):
24+
"""
25+
adventofcode
26+
run
27+
{day : day}
28+
{star? : star}
29+
{--o|output : show output}
30+
"""
31+
32+
def handle(self):
33+
day = f'{self.argument("day"):0>2}'
34+
star = self.argument('star')
35+
stars = (star,) if star else ('1', '2')
36+
for star in stars:
37+
print(f'days/{day}/__init__.py: s{star}')
38+
try:
39+
self.run(day, star)
40+
except BaseException:
41+
print_exc()
42+
print('_'*80)
43+
44+
def run(self, day, star):
45+
module = getattr(import_module(f'days.{day}'), f's{star}')
46+
with open(ROOT_PATH/f'days/{day}/s{star}.input') as f:
47+
input_data = f.read().split('\n')
48+
output = self._run(module, input_data)
49+
if self.option('output'):
50+
print(output)
51+
with open(ROOT_PATH/f'days/{day}/s{star}.output', 'w') as f:
52+
f.write(str(output))
53+
54+
@perf
55+
def _run(self, module, input_data):
56+
return module(input_data)
57+
58+
59+
if __name__ == '__main__':
60+
application = Application()
61+
application.add(RunCommand())
62+
application.run()

days/01/__init__.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def old_s1(input_):
2+
prev = int(input_[0])
3+
res = 0
4+
for v in input_[1:]:
5+
v = int(v)
6+
if v > prev:
7+
res += 1
8+
prev = v
9+
return res
10+
11+
12+
def s2(input_, offset=3):
13+
res = 0
14+
for i in range(offset):
15+
input_[i] = int(input_[i])
16+
for i in range(len(input_)-offset):
17+
input_[i+offset] = int(input_[i+offset])
18+
if input_[i+offset] > input_[i]:
19+
res += 1
20+
return res
21+
22+
23+
def s1(input_):
24+
return s2(input_, offset=1)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy