-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.py
107 lines (95 loc) · 3.24 KB
/
Day18.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import itertools
import re
import ast
import math
def snailfishHW():
f = open("input day 18.txt", "r")
#f = open("test.txt", "r")
lines = f.readlines()
i = 0
while i < len(lines):
if lines[i][-1] == '\n':
lines[i] = lines[i][:-1]
i += 1
#print(lines)
snailfishNumbers = []
for line in lines:
line = ast.literal_eval(line)
snailfishNumbers.append(line)
#list of all the lists to be added
#print(snailfishNumbers)
line = 0
snailNumber = lines[0]
while line < len(lines) - 1:
Cont = True
snailNumber = '[' + snailNumber + ',' + lines[line + 1] + ']'
#print(snailNumber)
while Cont == True:
snailNumber, Cont = reduce(snailNumber)
#print(snailNumber)
line += 1
#print(snailNumber)
print(magnitude(ast.literal_eval(snailNumber)))
maximum = 0
for combination in itertools.combinations(snailfishNumbers, 2):
Cont = True
snailNumber = '[' + str(combination[0]) + ',' + str(combination[1]) + ']'
while Cont == True:
snailNumber, Cont = reduce(snailNumber)
M = magnitude(ast.literal_eval(snailNumber))
if M > maximum:
maximum = M
print(maximum)
def reduce(strList):
strList.replace(' ', '')
numbers = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
newStr = strList
depth = 0
i = 0
while i < len(strList):
if strList[i] == '[':
depth += 1
elif strList[i] == ']':
depth -= 1
#explode
if depth > 4:
endIndex = strList[i:].index(']') + i + 1
beginIndex = i
pair = strList[beginIndex:endIndex]
#print(pair)
numbersInPair = re.findall('[0-9]+', pair)
leftNumber = int(numbersInPair[0])
rightNumber = int(numbersInPair[1])
leftNumbers = re.search('[0-9]+(?!.*[0-9]+)', strList[:i])
rightNumbers = re.search('[0-9]+', strList[endIndex:])
if rightNumbers != None:
newRightNumber = rightNumber + int(rightNumbers.group())
newStr = newStr[:endIndex + rightNumbers.span()[0]] + str(newRightNumber) + newStr[endIndex + rightNumbers.span()[1]:]
#put '0' in new string
newStr = newStr[:i] + '0' + newStr[endIndex:]
if leftNumbers != None:
newLeftNumber = leftNumber + int(leftNumbers.group())
newStr = newStr[:leftNumbers.span()[0]] + str(newLeftNumber) + newStr[leftNumbers.span()[1]:]
return newStr, True
i += 1
i = 0
while i < len(strList):
if strList[i] in numbers and strList[i + 1] in numbers:
split = int(strList[i:i + 2])
#print(split)
newPair = '[' + str(split // 2) + ',' + str(math.ceil(split / 2)) + ']'
newStr = strList[:i] + newPair + strList[i + 2:]
return newStr, True
i += 1
return strList, False
def magnitude(list):
sum = 0
if isinstance(list[0], int):
sum += list[0] * 3
else:
sum += magnitude(list[0]) * 3
if isinstance(list[1], int):
sum += list[1] * 2
else:
sum += magnitude(list[1]) * 2
return sum