Created
December 3, 2019 07:36
-
-
Save cornedor/55a7618de98be3b08d7944f713e68999 to your computer and use it in GitHub Desktop.
Advent of Code 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// # Day 1 | |
[@bs.val] external document: Js.t({..}) = "document"; | |
let container = document##createElement("div"); | |
let () = document##body##appendChild(container); | |
ReactDOMRe.render(<RocketFuelCalculator />, container); | |
// # Day 2 | |
let solveIntCodeProgram = final => { | |
let rec solver = (a, b) => | |
if (!(a > 99 && b > 99)) { | |
let result = IntCode.runProgram(IntCode.createProgram(a, b)); | |
if (result === final) { | |
[a, b]; | |
} else if (a === 99) { | |
solver(0, b + 1); | |
} else { | |
solver(a + 1, b); | |
}; | |
} else { | |
[0, 0]; | |
}; | |
solver(0, 0); | |
}; | |
Js.log(solveIntCodeProgram(19690720)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let template = [ | |
3, | |
1, | |
1, | |
2, | |
3, | |
1, | |
3, | |
4, | |
3, | |
1, | |
5, | |
0, | |
3, | |
2, | |
13, | |
1, | |
19, | |
1, | |
6, | |
19, | |
23, | |
2, | |
6, | |
23, | |
27, | |
1, | |
5, | |
27, | |
31, | |
2, | |
31, | |
9, | |
35, | |
1, | |
35, | |
5, | |
39, | |
1, | |
39, | |
5, | |
43, | |
1, | |
43, | |
10, | |
47, | |
2, | |
6, | |
47, | |
51, | |
1, | |
51, | |
5, | |
55, | |
2, | |
55, | |
6, | |
59, | |
1, | |
5, | |
59, | |
63, | |
2, | |
63, | |
6, | |
67, | |
1, | |
5, | |
67, | |
71, | |
1, | |
71, | |
6, | |
75, | |
2, | |
75, | |
10, | |
79, | |
1, | |
79, | |
5, | |
83, | |
2, | |
83, | |
6, | |
87, | |
1, | |
87, | |
5, | |
91, | |
2, | |
9, | |
91, | |
95, | |
1, | |
95, | |
6, | |
99, | |
2, | |
9, | |
99, | |
103, | |
2, | |
9, | |
103, | |
107, | |
1, | |
5, | |
107, | |
111, | |
1, | |
111, | |
5, | |
115, | |
1, | |
115, | |
13, | |
119, | |
1, | |
13, | |
119, | |
123, | |
2, | |
6, | |
123, | |
127, | |
1, | |
5, | |
127, | |
131, | |
1, | |
9, | |
131, | |
135, | |
1, | |
135, | |
9, | |
139, | |
2, | |
139, | |
6, | |
143, | |
1, | |
143, | |
5, | |
147, | |
2, | |
147, | |
6, | |
151, | |
1, | |
5, | |
151, | |
155, | |
2, | |
6, | |
155, | |
159, | |
1, | |
159, | |
2, | |
163, | |
1, | |
9, | |
163, | |
0, | |
99, | |
2, | |
0, | |
14, | |
0, | |
]; | |
let createProgram = (a, b) => { | |
[1, a, b, ...template]; | |
}; | |
let execute = (program, memory) => { | |
switch (program) { | |
| [1, a, b, pointer, ...rest] => | |
// add a to b, save to pointer | |
memory[pointer] = memory[a] + memory[b]; | |
rest; | |
| [2, a, b, pointer, ...rest] => | |
// multiply a with b, save to pointer | |
memory[pointer] = memory[a] * memory[b]; | |
rest; | |
| [3, pointer, ...rest] => | |
// Example of extra instruction, increase value at pointer | |
memory[pointer] = memory[pointer] + 1; | |
rest; | |
| [_, ...rest] => rest | |
| [] => [] | |
}; | |
}; | |
let rec compute = (program, memory) => { | |
let restProgram = execute(program, memory); | |
let isDone = | |
switch (restProgram) { | |
| [99, ..._] => true // if the program is at the end tag | |
| [] => true // or if there is nothing left | |
| _ => false | |
}; | |
if (!isDone) { | |
compute(restProgram, memory); | |
}; | |
}; | |
let runProgram = program => { | |
let memory = Array.of_list(program); | |
compute(program, memory); | |
memory[0]; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let minFuel = 6.0; | |
let rec calculateFuel = mass => { | |
let fuel = floor(mass /. 3.0) -. 2.0; | |
fuel > minFuel ? fuel +. calculateFuel(fuel) : fuel; | |
}; | |
[@react.component] | |
let make = () => { | |
let (input, setInput) = React.useState(() => ""); | |
let style = | |
ReactDOMRe.Style.make( | |
~background="#eee", | |
~maxWidth="1200px", | |
~margin="0 auto", | |
(), | |
); | |
let ships = Js_string.split("\n", input); | |
let totalFuel = | |
Belt.Array.reduce(ships, 0.0, (i, j) => | |
switch (j) { | |
| "" => i | |
| _ => | |
let shipMass = | |
try (float_of_string(j)) { | |
| Failure(_) => minFuel | |
}; | |
i +. calculateFuel(shipMass); | |
} | |
); | |
let shipsCounter = | |
React.string("Ships: " ++ string_of_int(Array.length(ships))); | |
let fuelString = | |
React.string("Total fuel: " ++ Js.Float.toString(totalFuel)); | |
<div style> | |
<br /> | |
<textarea | |
name="mass" | |
onChange={_e => { | |
let input = _e->ReactEvent.Form.target##value; | |
setInput(_ => input); | |
}} | |
/> | |
<hr /> | |
<strong> fuelString </strong> | |
<br /> | |
<em> shipsCounter </em> | |
</div>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment