Browse Source

day21, lost part 1, and part2 is... ugly

main
Luna Lailatova 2 years ago
parent
commit
fcf72efd51
  1. 83
      day21.py
  2. 22
      in2.txt
  3. 1933
      input21.txt

83
day21.py

@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
with open('input21.txt','r') as f:
inp = f.read().splitlines(keepends=False)
_monkeys = {}
for line in inp:
monkey, value = line.split(': ')
_monkeys[monkey] = value
Equation1 = _monkeys['root'].split()[0]
Equation2 = _monkeys['root'].split()[2]
print(Equation1, Equation2)
n = 0
done = False
monkeys = _monkeys.copy()
monkeys['humn'] = 'humn'
while not done:
done = True
for item in Equation1.split():
if item not in ['-','+','/','*','humn','(',')'] and not item.isnumeric():
if monkeys[item].isnumeric():
insert = monkeys[item]
else:
insert = '( ' + monkeys[item] + ' )'
Equation1 = Equation1.replace(item, insert)
done = False
break
done = False
while not done:
done = True
for item in Equation2.split():
if item not in ['-','+','/','*','humn','(',')'] and not item.isnumeric():
if monkeys[item].isnumeric():
insert = monkeys[item]
else:
insert = '( ' + monkeys[item] + ' )'
Equation2 = Equation2.replace(item, insert)
done = False
break
if 'humn' in Equation1:
Equation1 = Equation1.replace('humn', 'x')
Equation = eval('lambda x:' + Equation1)
else:
Equation1 = eval('lambda x:' + Equation1)
number = Equation1(1)
if 'humn' in Equation2:
Equation2 = Equation2.replace('humn', 'x')
Equation = eval('lambda x:' + Equation2)
else:
Equation2 = eval('lambda x:' + Equation2)
number = Equation2(1)
if Equation(0) < number:
increase = False
else:
increase = True
step = 100000000
while not Equation(n) == number:
print(n, Equation(n), number)
if increase:
if Equation(n) < number:
increase = False
step /= 10
else:
n += step
else:
if Equation(n) > number:
increase = True
step /= 10
else:
n -= step
print(n)
print(Equation1,'\n', Equation2)

22
in2.txt

@ -1,7 +1,15 @@ @@ -1,7 +1,15 @@
1
2
-3
3
-2
0
4
root: pppw + sjmn
dbpl: 5
cczh: sllz + lgvd
zczc: 2
ptdq: humn - dvpt
dvpt: 3
lfqf: 4
humn: 5
ljgn: 2
sjmn: drzm * dbpl
sllz: 4
pppw: cczh / lfqf
lgvd: ljgn * ptdq
drzm: hmdt - zczc
hmdt: 32

1933
input21.txt

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save