-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDayTwo.re
73 lines (62 loc) · 1.78 KB
/
DayTwo.re
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
let copy = Array.copy;
open Tablecloth;
module Memory = {
let update = (mem, pos, value) => mem |> Array.set(~index=pos, ~value);
let make = input => input |> copy;
};
module Computer = {
let rec make = (input, ~noun, ~verb, ~offset=0, ()) => {
let memory = Memory.make(input);
let memoryUpdate = Memory.update(memory);
memoryUpdate(1, noun);
memoryUpdate(2, verb);
switch (offset <= Array.length(memory)) {
| false => memory
| true =>
switch (memory |> Array.slice(~to_=offset + 4, ~from=offset)) {
| [|cmd, p1, p2, pos|] =>
let v1 = memory |> Array.get(~index=p1);
let v2 = memory |> Array.get(~index=p2);
switch (cmd, v1, v2) {
| (1, Some(v1), Some(v2)) => memoryUpdate(pos, v1 + v2)
| (2, Some(v1), Some(v2)) => memoryUpdate(pos, v1 * v2)
| _ => ()
};
| _ => ()
};
make(memory, ~noun, ~verb, ~offset=offset + 4, ());
};
};
};
module PartOne = {
let make = input =>
Computer.make(input, ~noun=12, ~verb=2, ()) |> Array.get(~index=0);
};
module PartTwo = {
let make = input => {
let nouns = Array.range(99);
let verbs = Array.range(99);
let res =
nouns
|> Array.map(~f=noun => {
verbs
|> Array.map(~f=verb =>
switch (
Computer.make(input, ~noun, ~verb, ())
|> Array.get(~index=0)
) {
| Some(19690720) =>
Some((noun |> string_of_int) ++ (verb |> string_of_int))
| None
| Some(_) => None
}
)
})
|> Array.concatenate
|> Array.filter(~f=Option.isSome);
switch (res |> Array.get(~index=0)) {
| Some(v) => v
| None => None
};
};
};