-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.py
61 lines (47 loc) · 1.49 KB
/
3.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
import re
pattern = r'mul\((\d{1,3},\d{1,3})\)'
pattern2 = r"do\(\)|don't\(\)"
def data(filename):
fulltext = ''
for line in open( filename):
text = line.strip()
fulltext += text
return fulltext
def part1(filename):
fulltext = data(filename)
res = 0
for temp in re.findall(pattern, fulltext):
parsed = [int(i) for i in temp.split(',')]
first, second = parsed[0], parsed[1]
res += first * second
print("Part 1 for ", filename, " is ", res)
def part2(filename):
fulltext = data(filename)
ranges = {}
for i in find(fulltext, "do()") + [0]:
ranges[i] = 1
for i in find(fulltext, "don't()"):
ranges[i] = 0
ranges = sorted(ranges.items())
res = 0
for match in re.finditer(pattern, fulltext, re.IGNORECASE):
# check the closest range that is below
for i in range(len(ranges) - 1, -1 , -1 ):
start, enabled = ranges[i]
if match.start() >= start:
if enabled:
temp = match.group(0)
a, b = [int(t) for t in temp.split("(")[1].split(")")[0].split(",")]
res += a * b
break
print("Part 2 for ", filename, " is ", res)
def find(text, tofind):
res = []
for i in range(len(text) - len(tofind)):
if text[i:i+len(tofind)] == tofind:
res.append(i)
return res
part1("data/3demo.txt")
part1("data/3.txt")
part2("data/3demo2.txt")
part2("data/3.txt")