-
Notifications
You must be signed in to change notification settings - Fork 0
/
day5.py
54 lines (44 loc) · 1.29 KB
/
day5.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 time
CHARS = list("abcdefghijklmnopqrstuvwxyz")
def react(data):
done = False
oldPoly = data
while done == False:
for char in CHARS:
data = data.replace(char + char.upper(), "").replace(char.upper() + char, "")
# If no change, return
if oldPoly == data:
done = True
break
oldPoly = data
return data
def part1(inputData = "input/input5.txt"):
with open(inputData) as f:
data = f.read()
data = data.rstrip()
data = react(data)
print("day 5, part 1: " + str(len(data)))
return data
def part2(inputData = "input/input5.txt", data = 0):
if data == 0:
with open(inputData) as f:
data = f.read()
data = data.rstrip()
data = react(data)
original = str(data)
bestLength = len(data)
bestChar = 0
for char in CHARS:
data = data.replace(char, "").replace(char.upper(), "")
data = react(data)
length = len(data)
if length < bestLength:
bestLength = length
bestChar = char
data = str(original)
print("day 5, part 2: " + str(bestLength))
def main(inputData = "input/input5.txt"):
dataArr = part1(inputData)
part2(inputData, dataArr)
if __name__ == "__main__":
main()