-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday.py
69 lines (54 loc) · 1.89 KB
/
day.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import argparse
from pathlib import Path
from functools import partial
import os, sys
from utils.parser import Parser
class Day(Parser):
def __init__(self, day: int):
self.day = day
self.data = []
self.debug = False
def load(
self, data: list = None, path: str = None, strip: bool = True) -> list:
"""Loads Data for Problem
File _must_ be named dayXX.txt
Returns data and makes it available as attribute "data"
Args:
data (list, optional):Load computed data not from file. Defaults to None.
path (str, optional): Path to data file. Defaults to None.
strip (bool, optional): Strip data. Defaults to True.
Returns:
list: Data for Problem
"""
if path is None:
path = f"data/day{self.day:02d}.txt"
if data is not None:
self.data = data
else:
with open(path) as f:
self.data = f.read()
if strip:
self.data = self.data.strip()
return self
def apply(self, func, *args, **kwargs) -> list:
"""Apply a function to every element.
Changes the original data.
Args:
func (function): Function to apply to every element in input
Returns:
list: Function applied to every element in input
"""
mapfunc = partial(func, *args, **kwargs)
try:
self.data = list(map(mapfunc, self.data))
except:
try:
self.data = [list(map(mapfunc, x)) for x in self.data]
except:
self.data = [[list(map(mapfunc, x)) for x in y] for y in self.data]
return self
def download(self):
from aocd import get_data
loc = Path("data", f"day{self.day:02d}.txt")
with open(loc, "w") as fp:
fp.write(get_data(day=self.day))