-
Notifications
You must be signed in to change notification settings - Fork 0
/
aoc.py
54 lines (47 loc) · 1.49 KB
/
aoc.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
import re
def readFile(filename):
with open(filename,'r') as file:
return file.read().rstrip('\n')
def parseString(string):
try:
return int(string)
except:
try:
return float(string)
except:
return string
def parse(content,regex):
matched=[]
for match in re.finditer(regex,content):
matched.append(tuple(parseString(group) for group in match.groups()))
return matched
def parseLines(content,regex=None):
if regex is None:
return content.split('\n')
parsed=[]
multiple=False
for line in content.split('\n'):
matched=[]
for match in re.finditer(regex,line):
groups=match.groups()
if len(groups)==0:
matched.append(parseString(match.group(0)))
elif len(groups)==1:
matched.append(parseString(groups[0]))
else:
matched.append(tuple(parseString(group) for group in match.groups()))
if len(matched)>1 and not multiple:
parsed=[[parse] for parse in parsed]
multiple=True
if multiple:
parsed.append(matched)
elif len(matched)==1:
parsed.append(matched[0])
return parsed
return [line for line in content.split('\n')]
def parseSections(content):
return content.split('\n\n')
def parseGrid(content):
return [[character for character in line] for line in content.split('\n')]
def parseGrids(content):
return [[[character for character in line] for line in grid] for grid in content.split('\n\n')]