# Advent of Code 2021
## Day 24 Advent of Code
```python=
# Analyser les 14 petits programmes, trop tricky pour moi 😢
# Evidement plus facile de coder la recherche lorsqu'on a l'explication !
# https://www.reddit.com/r/adventofcode/comments/rnejv5/2021_day_24_solutions/
# type 1 type 2
# Type 1 programs modify the value of z by
# z = 26 * z + input + some_number
# Type 2 programs modify the value of z by the integer division
# z = z / 26
# if and only if
# z % 26 + some_number == input
prog = [
(1,0),
(1,3),
(1,8),
(2,-5),
(1,13),
(1,9),
(1,6),
(2,-14),
(2,-8),
(1,2),
(2,0),
(2,-5),
(2,-9),
(2,-1)
]
def recherche(cle,z0,iprog):
if len(iprog) == 0 :
if z0 == 0 :
return cle
else:
return ""
prog = iprog.copy()
(type,some_number) = prog.pop(0)
for i in range(1,10) :
if type == 1 :
z = z0 * 26 + i + some_number
else:
if z0 % 26 + some_number == i :
z = z0 // 26
else:
continue
print(cle+str(i),z)
cle2 = recherche(cle+str(i),z,prog)
if cle2 != "" :
return cle2
return ""
print(recherche("",0,prog))
```
## Day 22 Advent of Code
```python=
import re
class reactor:
init = []
def load(self,file):
f = open(file,"r")
lines = [ x.rstrip("\n") for x in f.readlines() ]
for l in lines:
m = re.match('(.+) x=(-?\d+)\.\.(-?\d+),y=(-?\d+)\.\.(-?\d+),z=(-?\d+)\.\.(-?\d+)',l)
act = m.group(1)
a = (int(m.group(2)),int(m.group(4)),int(m.group(6)))
b = (int(m.group(3)),int(m.group(5)),int(m.group(7)))
print(act,a,b)
self.init.append((act,a,b))
def field(self,pos):
value = 0
for op in self.init:
(act,a,b) = op
if a[0] <= pos[0] and a[1] <= pos[1] and a[2] <= pos[2] and b[0] >= pos[0] and b[1] >= pos[1] and b[2] >= pos[2]:
value = 1 if act == 'on' else 0
return value
def integrate(self,pos=[]):
dimension = len(pos)
if len(pos) == 3:
return self.field(pos)
if len(pos) == 1:
print("integrating at x = {}".format(pos[0]))
# find ranges to integrate
rng = set({})
for op in self.init:
(act,a,b) = op
# optimization
if len(pos) == 1 and not (a[0] <= pos[0] and b[0] >= pos[0]) :
continue
if len(pos) == 2 and not (a[0] <= pos[0] and a[1] <= pos[1] and b[0] >= pos[0] and b[1] >= pos[1]):
continue
rng.update([a[dimension],b[dimension]+1])
r=list(sorted(rng))
value = 0
for i in range(0,len(r)-1):
sz = r[i+1]-r[i]
v = self.integrate(pos+[r[i]])
value += sz*v
return value
r = reactor()
r.load("data\day22_test.txt")
v = r.integrate()
# 2758514936282235
print (v)
```