summaryrefslogtreecommitdiff
path: root/2021/day24/README.md
blob: 457c944c001bfdb3ba8dc55b60d62214b1a20627 (plain) (blame)
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
# Pen & paper solution

The input program is composed by 14 repeated iterations with slight variations on 3 particular instructions:

- I4: `div z A` where `A` can be either 1 or 26;
- I5: `add x B` where `B` can be any number;
- I15 `add y C` where `C` can be any number.

Below is the table of the three sets of values for each iteration:

   |  A |   B |  C
---+----+-----+----
 1 |  1 |  13 |  6 
 2 |  1 |  11 | 11
 3 |  1 |  12 |  5 
 4 |  1 |  10 |  6 
 5 |  1 |  14 |  8 
 6 | 26 |  -1 | 14
 7 |  1 |  14 |  9 
 8 | 26 | -16 |  4 
 9 | 26 |  -8 |  7 
10 |  1 |  12 | 13
11 | 26 | -16 | 11
12 | 26 | -13 | 11
13 | 26 |  -6 |  6 
14 | 26 |  -6 |  1 

Each iteration `i` can be modelled with a function `f_i(z,a,b,c)` where `a`, `b`, `c` are the parameters for the given iteration and `z` is the output from the previous iteration (or 0 otherwise).

Function `f_i` can be defined as follows:

```{#iteration}
f_i(z,1,b,c) = z                        if (z % 26) == i - a 
f_i(z,1,b,c) = 26*z + (i + b)           otherwise

f_i(z,26,b,c) = z/26                    if (z % 26) == i - a
f_i(z,26,b,c) = 26*(z/26) + (i + b)     otherwise
```

**Note:** since we are dealing with integer division we cannot simplify `26*(z/26)` with `z`.