Skip to content

Instantly share code, notes, and snippets.

@odoe
Last active December 4, 2019 14:51
Show Gist options
  • Save odoe/cd30e6f3ccf7eb31e0d8c14b4814143c to your computer and use it in GitHub Desktop.
Save odoe/cd30e6f3ccf7eb31e0d8c14b4814143c to your computer and use it in GitHub Desktop.
AdventOfCode-2019
// Part 1
/**
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
**/
function fuelCounterUpper(mass: number): number {
return Math.floor(mass / 3) - 2;
}
const xs = [114106, 87170, 133060, 70662, 134140, 125874, 50081, 133117, 100409, 95098, 70251, 134043, 87501, 85034, 110678, 80615, 64647, 88555, 106387, 143755, 101246, 142348, 92684, 62051, 94894, 65873, 78473, 64042, 147982, 145898, 85591, 121413, 132163, 94351, 80080, 73554, 106598, 135174, 147951, 132517, 50925, 115752, 114022, 73448, 50451, 56205, 81474, 90028, 124879, 137452, 91036, 87221, 126590, 130592, 91503, 148689, 86526, 105924, 52411, 146708, 149280, 52100, 80024, 115412, 91204, 132726, 59837, 129863, 140980, 109574, 103013, 84105, 138883, 144861, 126708, 140290, 54417, 138154, 125187, 91537, 90338, 61150, 61702, 95888, 100484, 82115, 122141, 63986, 138234, 54150, 57651, 124570, 88460, 112144, 112334, 119114, 58220, 143221, 86568, 148706];
const total = xs.reduce((acc, curr) => acc + fuelCounterUpper(curr), 0)
console.log(total)
console.log(fuelCounterUpper(654));
// Part 2
/**
A module of mass 14 requires 2 fuel.
This fuel requires no further fuel (2 divided by 3 and rounded down is 0,
which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel.
Then, this fuel requires 216 more fuel (654 / 3 - 2).
216 then requires 70 more fuel, which requires 21 fuel,
which requires 5 fuel, which requires no further fuel.
So, the total fuel required for a module of mass 1969 is
654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is:
33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346
**/
function fuelWithMass(mass: number, previous: number = 0): number {
const fuel = fuelCounterUpper(mass);
if (fuel > 0) {
return fuelWithMass(fuel, fuel + previous)
}
return previous;
}
const totalFuel = xs.reduce((acc, curr) => acc + fuelWithMass(curr), 0)
console.log('total fuel', totalFuel);
// Day 2
// opcode 1 : add
// opcode 2: multiple
// opcode 99: halt
// 1, 10, 20, 30
// 1: add
// 10: read position 10
// 20: read position 20
// 30: add pos 10 and pos 20 and overwrite at pos 30
// go to next opcode, move forward 4 positions
// 1,9,10,3,2,3,11,0,99,30,40,50
const memory = [1, 0, 0, 3, 1, 1, 2, 3, 1, 3, 4, 3, 1, 5, 0, 3, 2, 1, 10, 19, 1, 19, 5, 23, 1, 6, 23, 27, 1, 27, 5, 31, 2, 31, 10, 35, 2, 35, 6, 39, 1, 39, 5, 43, 2, 43, 9, 47, 1, 47, 6, 51, 1, 13, 51, 55, 2, 9, 55, 59, 1, 59, 13, 63, 1, 6, 63, 67, 2, 67, 10, 71, 1, 9, 71, 75, 2, 75, 6, 79, 1, 79, 5, 83, 1, 83, 5, 87, 2, 9, 87, 91, 2, 9, 91, 95, 1, 95, 10, 99, 1, 9, 99, 103, 2, 103, 6, 107, 2, 9, 107, 111, 1, 111, 5, 115, 2, 6, 115, 119, 1, 5, 119, 123, 1, 123, 2, 127, 1, 127, 9, 0, 99, 2, 0, 14, 0];
const opMap: any = {
1: (a: number, b: number) => a + b,
2: (a: number, b: number) => a * b
};
function parseOpCodes(codes: number[], noun: number, verb: number) {
let ops = [...codes];
ops[1] = noun;
ops[2] = verb;
let i = 0;
while (ops[i] !== 99) {
const instruction = ops[i];
if (instruction === 99) break;
const func = opMap[instruction];
const parameter1 = ops[i + 1];
const parameter2 = ops[i + 2];
const parameter3 = ops[i + 3];
ops[parameter3] = func(ops[parameter1], ops[parameter2]);
i = i + 4;
}
// for (let i = 0; i < ops.length; i++) {
// const op = ops[i];
// if (op === 99) break;
// const func = opMap[op];
// const val1 = ops[i + 1];
// const val2 = ops[i + 2];
// const pos = ops[i + 3];
// ops[pos] = func(ops[val1], ops[val2]);
// i = i + 3;
// }
return ops;
}
const parsedCodes = parseOpCodes(memory, 12, 2);
console.log(parsedCodes);
const GOAL = 19690720;
function findValueAt(codes: number[], goal: number) {
let vals = [0, 0];
for (let i = 0; i < 99; i++) {
for (let j = 0; j < 99; j++) {
const parse2 = parseOpCodes(codes, i, j);
if (parse2[0] === goal) {
vals = [i, j];
break;
};
}
}
return vals;
}
const values = findValueAt(memory, GOAL);
console.log(values);
const check = parseOpCodes(memory, values[0], values[1]);
console.log('check', check);
const day2Answer = 100 * values[0] + values[1];
console.log('Day 2 answer: ', day2Answer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment