Luna Lailatova 3 years ago
parent
commit
2c0c08d63c
  1. 172
      day18.py
  2. 100
      input18.txt
  3. 10
      testdata18-2.txt
  4. 2
      testdata18-3.txt
  5. 10
      testdata18.txt

172
day18.py

@ -0,0 +1,172 @@
import math
import sys
def check_nested(inp):
peg_counter = 0
found = False
found2 = False
pair = ''
high_peg = None
for i in range(len(inp)):
if inp[i] == '[':
peg_counter += 1
if peg_counter > 4:
found = True
high_peg = peg_counter
pair = ''
start = i
if inp[i] == ']':
peg_counter -= 1
if found:
pair += inp[i]
if found and peg_counter == high_peg - 1:
end = i
found2 = True
break
if found2:
#print('Exploding ', pair)
pair = pair[1:-1]
left, rigth = pair.split(',')
tempnum = ''
digit_check = False
for i in range(end, len(inp)):
if inp[i].isdigit():
digit_check = True
digit_start = i
tempnum += inp[i]
#print('tempnum: ', tempnum)
if (inp[i] == ',' or inp[i] == ']') and digit_check:
# print('Replacing ', inp[i - len(tempnum):i], ' with ', str(int(tempnum) + int(rigth)), ' (', str(tempnum), ' + ' , str(rigth) + ')')
inp = inp[:i - len(tempnum)] + str(int(tempnum) + int(rigth)) + inp[i:]
break
# print('Replacing ', inp[start:end+1], ' with zero')
# print('B: ', inp)
inp = inp[:start] + '0' + inp[end + 1:]
# print('A: ', inp)
tempnum = ''
digit_check = False
for i in range(start - 1, 0, -1):
if inp[i].isdigit():
digit_check = True
digit_start = i
tempnum = inp[i] + tempnum
if (inp[i] == ',' or inp[i] == '[' ) and digit_check:
# print('Replacing ', inp[i+1:i+len(tempnum)+1], ' with ', str(int(tempnum) + int(left)))
inp = inp[:i + 1] + str(int(tempnum) + int(left)) + inp[len(tempnum) + i + 1:]
break
# print(inp)
return (inp, found2)
def check_split(inp):
checkmarker = 0
found = False
for i in range(len(inp)):
if inp[i].isdigit():
checkmarker += 1
else:
checkmarker = 0
if checkmarker == 2:
number_pos = i
number = int(inp[i-1] + inp[i])
number = number/2
found = True
break
if found:
new_pair = '[' + str(math.trunc(number)) + ',' + str(math.ceil(number)) + ']'
#print('Replacing ', inp[number_pos - 1:number_pos+1], ' with ', new_pair)
inp = inp[:number_pos - 1] + new_pair + inp[number_pos + 1:]
#print(inp)
return (inp, found)
def magnitude(inp):
found = False
number = ''
start = None
end = None
for i in range(len(inp)):
if inp[i] == '[':
start = i
if inp[i] == ']':
end = i
break
if start is None:
start = 0
if end is None:
end = len(inp)
for i in range(start, end):
number += inp[i]
if ',' in number:
found = True
left, rigth = number.split(',')
left = left[1:]
number = str(int(left)*3 + int(rigth)*2)
inp = inp[:start] + number + inp[end + 1:]
else:
inp = number
return (inp, found)
def reduct(inp):
reduction = True
while reduction:
eplode = True
split = True
while eplode:
inp, eplode = check_nested(inp)
reduction = False
inp, split = check_split(inp)
if split:
reduction = True
return(inp)
inp = []
with open(sys.argv[1], 'r') as f:
for g in f.readlines():
g = g.strip()
inp.append(g)
if sys.argv[2] == 'part1':
imp = inp[0]
inp.pop(0)
while len(inp) > 0:
print('___________________')
print(imp)
print('+', inp[0])
imp = '[' + imp + ',' + inp[0] + ']'
inp.pop(0)
imp = reduct(imp)
print('=', imp)
print('___________________')
done_check = True
while done_check:
imp, done_check = magnitude(imp)
print(imp, done_check)
if sys.argv[2] == 'part2':
high_mag = 0
for i in inp:
for f in inp:
if f != i:
imp = '[' + i + ',' + f + ']'
imp = reduct(imp)
done_check = True
while done_check:
imp, done_check = magnitude(imp)
if int(imp) > high_mag:
high_mag = int(imp)
print(high_mag)

100
input18.txt

@ -0,0 +1,100 @@
[[[3,[8,6]],[6,1]],[[[1,1],2],[[1,0],0]]]
[[[1,[7,3]],1],9]
[[[2,6],[[3,1],[0,9]]],[[7,[4,8]],[[2,7],3]]]
[[[3,[0,4]],[[8,4],[1,9]]],[7,[2,[5,7]]]]
[[[4,5],[[0,7],1]],[9,[0,4]]]
[[5,[[1,5],[3,6]]],8]
[[3,[[9,3],9]],9]
[2,[[[2,1],[0,5]],[9,9]]]
[[2,[6,9]],[[[4,1],0],[3,4]]]
[[[[6,8],0],[[8,8],9]],[[[4,2],3],[3,[7,3]]]]
[[3,7],9]
[[[[2,5],8],[2,5]],[[0,[5,7]],[[2,5],4]]]
[[[8,[6,6]],0],[4,[[5,6],[8,4]]]]
[[[1,[8,2]],[[0,4],[2,6]]],[[3,4],0]]
[[1,[[9,2],[6,0]]],[[[0,9],5],[[8,0],[1,5]]]]
[[2,[[2,3],[1,8]]],[3,[[7,2],[0,7]]]]
[[5,4],5]
[[[[4,2],[4,8]],[7,3]],[0,[[8,9],6]]]
[[[6,7],0],5]
[[2,[[9,0],[8,4]]],[[[7,4],[3,4]],0]]
[[[9,[8,9]],1],[[5,[6,7]],3]]
[[2,[0,0]],[3,[[2,5],[1,4]]]]
[[0,1],[0,[[8,8],[8,3]]]]
[[[0,2],[2,8]],[1,[[7,0],0]]]
[[[[5,4],3],[[7,5],[2,6]]],[[5,8],[0,1]]]
[0,[0,0]]
[[5,[[5,6],0]],[[[2,7],9],[7,9]]]
[[[[0,8],2],[[2,5],[7,6]]],[[9,7],[[8,7],[9,2]]]]
[[[0,[4,6]],[[6,3],[4,4]]],[8,[[4,8],[4,8]]]]
[[[[8,9],[3,8]],8],[[[7,9],6],[9,[2,7]]]]
[[[[8,9],[1,6]],0],[[[8,7],4],[9,[1,4]]]]
[5,7]
[[[[1,5],[3,6]],[[5,5],4]],[[3,3],[4,[4,0]]]]
[[[0,6],[5,[5,3]]],[[4,[0,0]],8]]
[7,[6,8]]
[[[[8,5],9],[[3,2],7]],[[[6,6],5],2]]
[[[[4,4],[0,4]],9],0]
[[0,[3,[9,3]]],[9,[[8,0],[0,9]]]]
[[[[4,0],0],[1,[1,7]]],[[3,[3,0]],[[1,3],6]]]
[[9,4],[3,[[7,1],6]]]
[[[[3,7],7],1],[[4,3],[[6,9],[6,9]]]]
[[[8,[2,5]],[[8,4],4]],[[[3,4],[6,7]],[5,[8,5]]]]
[2,[4,[[3,2],7]]]
[[[[3,1],[5,6]],[[2,7],7]],[4,[8,[7,4]]]]
[[7,8],[[[3,9],7],2]]
[[[[8,8],[5,8]],[[1,0],[6,0]]],[[[1,2],6],[[4,2],[5,5]]]]
[[1,[0,9]],[[[2,1],1],1]]
[[6,[8,1]],[4,[[7,8],5]]]
[[[1,[1,6]],[1,[5,7]]],[[[2,8],6],0]]
[9,1]
[[[0,[6,5]],[[8,5],2]],[[[2,4],[7,3]],[[1,5],[9,2]]]]
[[[2,7],[0,[3,6]]],[[[1,0],[9,6]],[1,[0,4]]]]
[6,[[[5,9],8],[0,2]]]
[7,[[[9,4],[8,6]],[[1,1],1]]]
[[[2,1],0],8]
[1,[[6,[1,4]],[[0,0],[1,9]]]]
[[[1,[7,9]],2],8]
[[[[0,9],2],[[8,4],9]],[0,[[7,7],[4,8]]]]
[[1,[2,[1,8]]],[[[3,6],[2,1]],[3,[5,0]]]]
[[3,3],[3,5]]
[[[[9,3],[4,3]],[5,[8,1]]],[[6,[5,0]],9]]
[0,[[9,[3,5]],3]]
[[[9,1],0],[[[5,9],[8,0]],[7,[4,8]]]]
[[[[7,7],8],3],[[[6,6],[6,5]],[6,4]]]
[[[[3,7],1],[9,[4,2]]],[[9,[2,5]],[[9,0],5]]]
[5,[[0,2],6]]
[[[[2,7],[5,3]],[1,8]],2]
[[[8,[7,7]],[9,[0,0]]],4]
[[[4,[1,4]],0],[[[8,7],8],[[4,1],7]]]
[[[[0,6],0],[[3,2],[9,8]]],[[9,[4,5]],[[7,7],[0,8]]]]
[[[[6,3],3],[[1,5],7]],[[0,1],[7,7]]]
[[[[2,0],2],[3,[3,5]]],[[[0,8],[8,2]],[[0,6],5]]]
[[[6,[5,3]],[[5,5],9]],[[5,9],[[8,7],[3,7]]]]
[[[[1,7],[3,4]],[9,2]],1]
[[[[8,2],6],1],[[5,[2,7]],[3,9]]]
[5,[5,7]]
[[[[9,8],[3,4]],[[2,5],[5,6]]],[[[2,7],7],[9,[8,7]]]]
[[[1,4],[[6,1],[1,3]]],[1,[7,[1,7]]]]
[[[[1,4],8],[[5,1],8]],[[[1,3],[6,9]],[6,[3,3]]]]
[[[[4,0],[0,7]],[4,5]],[4,2]]
[3,8]
[7,[[[7,6],5],[[6,6],5]]]
[[[5,[0,5]],[4,4]],[3,[[4,2],[7,0]]]]
[[[[7,9],8],[9,6]],[5,0]]
[[[[3,0],[5,2]],1],[[[6,9],[5,3]],[[2,5],[6,3]]]]
[7,[[[7,7],[4,5]],[9,2]]]
[[7,[[4,2],[9,3]]],[7,[6,1]]]
[7,9]
[[[8,[8,1]],[[7,3],1]],[[9,8],[2,[8,3]]]]
[[[9,3],3],3]
[[[8,[5,7]],[[2,1],[1,3]]],[[[3,5],2],0]]
[[[8,8],0],[[1,4],[[8,6],9]]]
[[9,[3,[3,0]]],[1,7]]
[1,[[[8,8],1],[2,[0,5]]]]
[[0,[1,5]],[9,[0,[9,0]]]]
[1,[[[1,1],[8,3]],[1,8]]]
[[5,[[7,7],[3,3]]],[[[6,6],[7,8]],[1,[0,0]]]]
[[[[6,7],1],[0,2]],[[[4,2],[7,6]],[[8,4],[4,9]]]]
[[6,[[3,3],[9,0]]],[1,[[4,5],4]]]
[[[[3,4],7],[9,0]],[[[4,5],1],[[5,1],[9,3]]]]

10
testdata18-2.txt

@ -0,0 +1,10 @@
[[[0,[5,8]],[[1,7],[9,6]]],[[4,[1,2]],[[1,4],2]]]
[[[5,[2,8]],4],[5,[[9,9],0]]]
[6,[[[6,2],[5,6]],[[7,6],[4,7]]]]
[[[6,[0,7]],[0,9]],[4,[9,[9,0]]]]
[[[7,[6,4]],[3,[1,3]]],[[[5,5],1],9]]
[[6,[[7,3],[3,2]]],[[[3,8],[5,7]],4]]
[[[[5,4],[7,7]],8],[[8,3],8]]
[[9,3],[[9,9],[6,[4,9]]]]
[[2,[[7,7],7]],[[5,8],[[9,3],[0,2]]]]
[[[[5,2],5],[8,[3,7]]],[[5,[7,5]],[4,4]]]

2
testdata18-3.txt

@ -0,0 +1,2 @@
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]

10
testdata18.txt

@ -0,0 +1,10 @@
[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
[7,[5,[[3,8],[1,4]]]]
[[2,[2,2]],[8,[8,1]]]
[2,9]
[1,[[[9,3],9],[[9,0],[0,7]]]]
[[[5,[7,4]],7],1]
[[[[4,2],2],6],[8,7]]
Loading…
Cancel
Save