Skip to content

Commit 572121b

Browse files
committed
check in 14, 15 and timer decorator
1 parent 4ac0009 commit 572121b

File tree

10 files changed

+121
-55
lines changed

10 files changed

+121
-55
lines changed

Diff for: 14/main.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import os
33
import re
44

5+
from aoc2020.utils.decorators import timer
6+
57

68
def read_input():
79
filename = os.path.join(os.path.dirname(__file__), "input.txt")
8-
with open(filename) as f:
10+
with open(filename) as f:
911
data = f.readlines()
1012

1113
return data
@@ -19,39 +21,40 @@ def _parse_input(records):
1921
if match:
2022
mask = match.group(1)
2123
ones = int(mask.replace("X", "0"), 2)
22-
zeros = int(mask.replace("1", "X").replace("0", "1").replace("X", "0"), 2)
24+
zeros = int(mask.replace("X", "1"), 2)
2325
floats = mask.replace("1", "0").replace("X", "1")
2426

2527
continue
2628

27-
match = re.match("mem\[(\d*)\] = (\d*)", line)
29+
match = re.match(r"mem\[(\d*)\] = (\d*)", line)
2830
mem_address, value = match.group(1), match.group(2)
2931

3032
data.append((int(mem_address), int(value), ones, zeros, floats))
31-
33+
3234
return data
3335

3436

37+
@timer
3538
def docking_data(records):
3639
mem = {}
3740
for key, value, ones, zeros, _ in records:
3841
mem[key] = (value, ones, zeros)
3942

4043
total = functools.reduce(
4144
lambda x, y: x + y,
42-
list(map(lambda v: (v[0] | v[1]) - ((v[0] | v[1]) & v[2]), mem.values())),
45+
list(map(lambda v: (v[0] | v[1]) & v[2], mem.values())),
4346
)
4447

4548
return total
4649

4750

51+
@timer
4852
def docking_data_2(records):
4953
mem = {}
5054

5155
for k, val, ones, zeros, floats in records:
5256
base = ((k | int(floats, 2)) ^ int(floats, 2)) | ones
5357
floats = list(floats)
54-
# print(floats)
5558
a = []
5659
for i, binary in enumerate(floats):
5760
if binary == "1":
@@ -69,15 +72,16 @@ def docking_data_2(records):
6972
a = ["0"]
7073
else:
7174
a = [x + "0" for x in a]
72-
75+
7376
for v in a:
7477
k_ = base | int(v, 2)
7578
mem[k_] = val
76-
79+
7780
return sum(mem.values())
7881

7982

8083
if __name__ == "__main__":
8184
records = _parse_input(read_input())
85+
8286
print(f"Part 1: {docking_data(records)}")
8387
print(f"Part 2: {docking_data_2(records)}")

Diff for: 15/__init__.py

Whitespace-only changes.

Diff for: 15/main.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from aoc2020.utils.decorators import timer
2+
3+
4+
@timer
5+
def rambunctious_recitation(records, count):
6+
tracking_dict = {}
7+
i = 1
8+
while len(records) > 1:
9+
last = records.pop(0)
10+
tracking_dict[last] = i
11+
12+
i += 1
13+
14+
for j in range(i, count):
15+
if records:
16+
last = records.pop(0)
17+
18+
if last in tracking_dict:
19+
temp = j - tracking_dict[last]
20+
tracking_dict[last] = j
21+
last = temp
22+
else:
23+
tracking_dict[last] = j
24+
last = 0
25+
26+
return last
27+
28+
29+
if __name__ == "__main__":
30+
input_ = [0, 13, 1, 16, 6, 17]
31+
32+
print(f"Part 1: {rambunctious_recitation(input_[:], 2020)}")
33+
print(f"Part 1: {rambunctious_recitation(input_[:], 30000000)}")

Diff for: __init__py

Whitespace-only changes.

Diff for: requirements.

+16-47
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,30 @@
1-
appnope==0.1.0
2-
argon2-cffi==20.1.0
3-
async-generator==1.10
4-
attrs==20.3.0
1+
appdirs==1.4.4
2+
appnope==0.1.2
53
backcall==0.2.0
6-
bleach==3.2.1
7-
cffi==1.14.4
8-
cycler==0.10.0
94
decorator==4.4.2
10-
defusedxml==0.6.0
11-
entrypoints==0.3
12-
importlib-metadata==3.1.0
13-
ipykernel==5.3.4
5+
distlib==0.3.1
6+
filelock==3.0.12
7+
flake8==3.8.4
8+
importlib-metadata==2.0.0
9+
ipdb==0.13.4
1410
ipython==7.19.0
1511
ipython-genutils==0.2.0
16-
ipywidgets==7.5.1
1712
jedi==0.17.2
18-
Jinja2==2.11.2
19-
jsonschema==3.2.0
20-
jupyter==1.0.0
21-
jupyter-client==6.1.7
22-
jupyter-console==6.2.0
23-
jupyter-core==4.7.0
24-
jupyterlab-pygments==0.1.2
25-
kiwisolver==1.3.1
26-
MarkupSafe==1.1.1
27-
matplotlib==3.3.3
28-
mistune==0.8.4
29-
nbclient==0.5.1
30-
nbconvert==6.0.7
31-
nbformat==5.0.8
32-
nest-asyncio==1.4.3
33-
notebook==6.1.5
34-
numpy==1.19.4
35-
packaging==20.7
36-
pandocfilters==1.4.3
13+
mccabe==0.6.1
3714
parso==0.7.1
15+
pbr==5.5.1
3816
pexpect==4.8.0
3917
pickleshare==0.7.5
40-
Pillow==8.0.1
41-
prometheus-client==0.9.0
4218
prompt-toolkit==3.0.8
4319
ptyprocess==0.6.0
44-
pycparser==2.20
45-
Pygments==2.7.2
46-
pyparsing==2.4.7
47-
pyrsistent==0.17.3
48-
python-dateutil==2.8.1
49-
pyzmq==20.0.0
50-
qtconsole==5.0.1
51-
QtPy==1.9.0
52-
Send2Trash==1.5.0
20+
pycodestyle==2.6.0
21+
pyflakes==2.2.0
22+
Pygments==2.7.3
5323
six==1.15.0
54-
terminado==0.9.1
55-
testpath==0.4.4
56-
tornado==6.1
24+
stevedore==3.2.2
5725
traitlets==5.0.5
26+
virtualenv==20.1.0
27+
virtualenv-clone==0.5.4
28+
virtualenvwrapper==4.8.4
5829
wcwidth==0.2.5
59-
webencodings==0.5.1
60-
widgetsnbextension==3.5.1
6130
zipp==3.4.0

Diff for: utils/__init__.py

Whitespace-only changes.

Diff for: utils/decorators.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import functools
2+
import timeit
3+
from aoc2020.utils.log.base import get_logger
4+
5+
logger = get_logger(__name__)
6+
7+
8+
def timer(func):
9+
@functools.wraps(func)
10+
def timer_wrapper(*args, **kwargs):
11+
start = timeit.default_timer()
12+
13+
result = func(*args, **kwargs)
14+
15+
elapsed = timeit.default_timer() - start
16+
logger.info(f"Completed {func.__name__} in {elapsed:4f} seconds.")
17+
18+
return result
19+
20+
return timer_wrapper

Diff for: utils/log/__init__.py

Whitespace-only changes.

Diff for: utils/log/base.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import logging
2+
import logging.config
3+
import os
4+
5+
6+
def get_logger(name):
7+
filename = os.path.join(os.path.dirname(__file__), "logging.conf")
8+
logging.config.fileConfig(filename)
9+
10+
return logging.getLogger(name)

Diff for: utils/log/logging.conf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[loggers]
2+
keys=root,dev
3+
4+
[handlers]
5+
keys=consoleHandler
6+
7+
[formatters]
8+
keys=extend,simple
9+
10+
[logger_root]
11+
level=INFO
12+
handlers=consoleHandler
13+
14+
[logger_dev]
15+
level=INFO
16+
handlers=consoleHandler
17+
qualname=dev
18+
propagate=0
19+
20+
[handler_consoleHandler]
21+
class=StreamHandler
22+
level=INFO
23+
formatter=extend
24+
args=(sys.stdout,)
25+
26+
[formatter_extend]
27+
format=%(asctime)s - %(name)s - %(levelname)s - %(message)s
28+
29+
[formatter_simple]
30+
format=%(asctime)s - %(message)s

0 commit comments

Comments
 (0)