Last active
December 8, 2019 18:59
-
-
Save icsaba/b2427b1723e0cd7949f3683ba562c12d 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
const fs = require('fs'); | |
const buffer = fs.readFileSync('input'); | |
const file = buffer.toString().split('\n'); | |
function calcConsumption(amount) { | |
const tmp = Math.floor(+amount / 3) - 2; | |
return (tmp > 0) ? tmp + calcConsumption(tmp) : 0; | |
} | |
const result = file.reduce((acc, item) => acc + calcConsumption(item), 0); | |
console.log(result) |
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
const fs = require('fs'); | |
const buffer = fs.readFileSync('input'); | |
let content = buffer.toString().split(','); | |
content = content.map( i => +i); | |
content[1] = 12; | |
content[2] = 2; | |
const operations = { | |
1: (x ,y) => x + y, | |
2: (x, y) => x * y, | |
99: () => { throw new Error('done')} | |
}; | |
function calculate(inp) { | |
const tmp = [...inp]; | |
for(let i = 0; i < tmp.length; i++) { | |
try { | |
const op = tmp[i]; | |
const a = tmp[tmp[++i]]; | |
const b = tmp[tmp[++i]]; | |
const placeToPut = tmp[++i]; | |
const result = operations[op](a, b); | |
// console.log(op, a, b, placeToPut, result, i); | |
tmp[placeToPut] = result; | |
} catch(e) { | |
if (e.toString() === 'Error: done') break; | |
else throw e; | |
} | |
} | |
return tmp; | |
} | |
function getExpectedResult(){ | |
const expectedResult = 19690720; | |
const a = 12; | |
const b = 2; | |
let noun, verb; | |
for (let i=0; i<content.length; i++) { | |
for(let j=0; j<content.length; j++) { | |
const tmp = [...content]; | |
noun = a + i; | |
verb = b + j; | |
tmp[1] = noun; | |
tmp[2] = verb; | |
const result = calculate(tmp); | |
if (result[0] === expectedResult) { | |
return 100 * noun + verb; | |
} | |
} | |
} | |
} | |
const result = getExpectedResult(); | |
console.log(result); | |
const mainResult = calculate(content); | |
console.log(mainResult[0]); | |
function isEqual(a, b) { | |
return a.reduce( (acc, item, index) => acc && (item === b[index]), true ); | |
} | |
let test = [1,0,0,0,99]; | |
let expected = [2,0,0,0,99] | |
let result = calculate(test); | |
console.log(isEqual(expected, result)); | |
test = [2,3,0,3,99]; | |
expected = [2,3,0,6,99]; | |
result = calculate(test); | |
console.log(isEqual(expected, result)); | |
test = [2,4,4,5,99,0]; | |
expected = [2,4,4,5,99,9801]; | |
result = calculate(test); | |
console.log(isEqual(expected, result)); | |
test = [1,1,1,4,99,5,6,0,99]; | |
expected = [30,1,1,4,2,5,6,0,99]; | |
result = calculate(test); | |
console.log(isEqual(expected, result)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment