day 6 and 7

This commit is contained in:
Tristan 2019-12-08 01:16:47 +00:00
parent dd9f13cb0d
commit cbc02f04d5
7 changed files with 2130 additions and 0 deletions

1805
2019/06/input.txt Normal file

File diff suppressed because it is too large Load diff

30
2019/06/part1.js Normal file
View file

@ -0,0 +1,30 @@
const fs = require("fs");
const text = fs.readFileSync("2019/06/input.txt", "utf-8");
let inputArr = text.split('\n');
let objNet = {};
let objArr = [];
inputArr.forEach(input => {
points = input.split(')');
objNet[points[1]] = points[0];
objArr.push(points[1]);
})
console.log(objNet);
total = 0;
let i;
objArr.forEach(obj => {
i=0
getOrigin(obj)
total+=i
});
console.log(total)
function getOrigin(obj) {
center = objNet[obj]
if (center) {
i++
return getOrigin(center)
} else {
return i
}
}

31
2019/06/part2.js Normal file
View file

@ -0,0 +1,31 @@
const fs = require("fs");
const text = fs.readFileSync("2019/06/input.txt", "utf-8");
let inputArr = text.split('\n');
let objNet = {};
let objArr = [];
inputArr.forEach(input => {
points = input.split(')');
objNet[points[1]] = {orbits: points[0]};
objArr.push(points[1]);
})
function getOrigin(obj, track, transfers = 0) {
center = objNet[obj]
if (center) {
objNet[obj]["distFrom"+track] = transfers-1;
transfers++;
return getOrigin(center.orbits, track, transfers)
} else {
return transfers
}
}
getOrigin("SAN", "SAN")
getOrigin("YOU", "YOU")
min = Infinity;
objArr.forEach(obj => {
totalDist = objNet[obj].distFromSAN + objNet[obj].distFromYOU;
min = totalDist < min? totalDist:min;
})
console.log(min)

1
2019/07/input.txt Normal file
View file

@ -0,0 +1 @@
3,8,1001,8,10,8,105,1,0,0,21,30,47,64,81,98,179,260,341,422,99999,3,9,1001,9,5,9,4,9,99,3,9,1002,9,5,9,101,4,9,9,102,2,9,9,4,9,99,3,9,102,3,9,9,101,2,9,9,1002,9,3,9,4,9,99,3,9,1001,9,5,9,1002,9,3,9,1001,9,3,9,4,9,99,3,9,1002,9,3,9,101,2,9,9,102,5,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,101,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,99,3,9,1001,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,99

91
2019/07/part1.js Normal file
View file

@ -0,0 +1,91 @@
const fs = require("fs");
const text = fs.readFileSync("2019/07/input.txt", "utf-8");
let memory = text.split(',').map(opcode => parseInt(opcode));
function valueOf(index, paramMode) {
return paramMode == "0" ? memory[memory[index]] : memory[index];
}
function fiveDig(value) {
return value.toString().length < 5 ? fiveDig("0" + value) : value.toString();
}
function run(phase, input) {
let inputSequence = [phase, input];
let inputNo = 0;
let outputSequence = []
let i = 0;
while (true) {
instruction = fiveDig(memory[i]);
opcode = instruction.slice(3);
if (opcode == '01') {
memory[memory[i+3]] = valueOf(i+1, instruction[2]) + valueOf(i+2, instruction[1]);
i+=4;
} else if (opcode == '02') {
memory[memory[i+3]] = valueOf(i+1, instruction[2]) * valueOf(i+2, instruction[1]);
i+=4;
} else if (opcode == '03') {
userInput = inputSequence[inputNo % inputSequence.length];
inputNo++;
// console.log("IN "+userInput);
memory[memory[i+1]] = userInput;
i+=2;
} else if (opcode == '04') {
outputValue = valueOf(i+1, instruction[2])
// console.log("OUT "+outputValue);
outputSequence.push(outputValue);
i+=2;
} else if (opcode == '05') {
if (valueOf(i+1, instruction[2]) != 0) {
i = valueOf(i+2, instruction[1])
} else {
i+=3;
}
} else if (opcode == '06') {
if (valueOf(i+1, instruction[2]) == 0) {
i = valueOf(i+2, instruction[1])
} else {
i+=3;
}
} else if (opcode == '07') {
memory[memory[i+3]] = valueOf(i+1, instruction[2]) < valueOf(i+2, instruction[1]) ? 1 : 0;
i+=4;
} else if (opcode == '08') {
memory[memory[i+3]] = valueOf(i+1, instruction[2]) == valueOf(i+2, instruction[1]) ? 1 : 0;
i+=4;
} else if (opcode == '99') {
// console.log("HLT")
return outputSequence;
}
}
}
function test(phaseSequence) {
let output = [0]
phaseSequence.forEach(phase => {
output = run(phase, output[0]);
})
return output[0];
}
function arrayIsUnique(myArray) {
return myArray.length === new Set(myArray).size;
}
maxSignal = 0;
// inefficient af but idk how else to do it
for (let phase = 0; phase < Math.pow(5, 5); phase++){
phaseSequence = [phase % 5, Math.floor(phase/5)%5, Math.floor(phase/25)%5, Math.floor(phase/125)%5, Math.floor(phase/625)%5]
if (!arrayIsUnique(phaseSequence)) {
continue;
}
if (phaseSequence == [1, 0, 4, 3, 2]) {
console.log(phaseSequence);
}
thrustSignal = test(phaseSequence);
if (maxSignal < thrustSignal) {
maxSignal = thrustSignal;
}
}
console.log(maxSignal);

168
2019/07/part2.js Normal file
View file

@ -0,0 +1,168 @@
const fs = require("fs");
const text = fs.readFileSync("2019/07/input.txt", "utf-8");
let instructions = text.split(',').map(opcode => parseInt(opcode));
function fiveDig(value) {
return value.toString().length < 5 ? fiveDig("0" + value) : value.toString();
}
function IntcodeAmp(verbose) {
this.verbose = verbose;
this.reset = function(){
this.active = false;
this.memory = instructions.slice();
this.memoryAdress = 0;
if (this.verbose) {
console.log("RESET");
}
}
this.reset();
this.valueOf = function(index, paramMode) {
return paramMode == "0" ? this.memory[this.memory[index]] : this.memory[index];
}
this.add = function (a, b){
this.memory[this.memory[this.memoryAdress+3]] =
this.valueOf(this.memoryAdress+1, b) +
this.valueOf(this.memoryAdress+2, a);
this.memoryAdress+=4;
}
this.mult = function (a, b){
this.memory[this.memory[this.memoryAdress+3]] =
this.valueOf(this.memoryAdress+1, b) *
this.valueOf(this.memoryAdress+2, a);
this.memoryAdress+=4;
}
this.input = function () {
let userInput = this.inputSequence[this.inputNo];
this.inputNo++;
if (this.verbose){
console.log("IN "+userInput);
}
this.memory[this.memory[this.memoryAdress+1]] = userInput;
this.memoryAdress+=2;
}
this.jmpIfNot0 = function(a, b) {
if (this.valueOf(this.memoryAdress+1, b) != 0) {
this.memoryAdress = this.valueOf(this.memoryAdress+2, a)
} else {
this.memoryAdress+=3;
}
}
this.jmpIf0 = function(a, b) {
if (this.valueOf(this.memoryAdress+1, b) == 0) {
this.memoryAdress = this.valueOf(this.memoryAdress+2, a)
} else {
this.memoryAdress+=3;
}
}
this.jmpIfLessThan = function(a, b) {
this.memory[this.memory[this.memoryAdress+3]] =
this.valueOf(this.memoryAdress+1, b) <
this.valueOf(this.memoryAdress+2, a) ? 1 : 0;
this.memoryAdress+=4;
}
this.jmpIfEqual = function(a, b) {
this.memory[this.memory[this.memoryAdress+3]] =
this.valueOf(this.memoryAdress+1, b) ==
this.valueOf(this.memoryAdress+2, a) ? 1 : 0;
this.memoryAdress+=4;
}
this.run = function (inputSequence){
this.active = true;
this.inputSequence = inputSequence;
this.inputNo = 0;
while (true) {
instruction = fiveDig(this.memory[this.memoryAdress]);
opcode = instruction.slice(3);
if (opcode == '01') {
this.add(instruction[1], instruction[2]);
} else if (opcode == '02') {
this.mult(instruction[1], instruction[2])
} else if (opcode == '03') {
if (this.inputNo >= this.inputSequence.length) {
return "input required";
}
this.input();
} else if (opcode == '04') {
outputValue = this.valueOf(this.memoryAdress+1, instruction[2])
this.memoryAdress+=2;
return outputValue;
} else if (opcode == '05') {
this.jmpIfNot0(instruction[1], instruction[2]);
} else if (opcode == '06') {
this.jmpIf0(instruction[1], instruction[2]);
} else if (opcode == '07') {
this.jmpIfLessThan(instruction[1], instruction[2]);
} else if (opcode == '08') {
this.jmpIfEqual(instruction[1], instruction[2]);
} else if (opcode == '99') {
this.reset();
return;
}
}
}
}
amps = [
new IntcodeAmp(),
new IntcodeAmp(),
new IntcodeAmp(),
new IntcodeAmp(),
new IntcodeAmp()
]
function test(phaseSequence) {
output = 0;
for (let i = 0; i < phaseSequence.length; i++){
amps[i].run([phaseSequence[i]]);
}
do {
for (amp of amps) {
let ampOut = amp.run([output]);
if (!ampOut) {
break;
} else {
output = ampOut;
}
}
} while (amps[0].active)
for (amp of amps) {
amp.reset();
}
return output;
}
maxSignal = 0;
// inefficient af but idk how else to do it
function arrayIsUnique(myArray) {
return myArray.length === new Set(myArray).size;
}
for (let phase = 0; phase < Math.pow(5, 5); phase++){
phaseSequence = [(phase%5)+5,
(Math.floor(phase/5)%5)+5,
(Math.floor(phase/25)%5)+5,
(Math.floor(phase/125)%5)+5,
(Math.floor(phase/625)%5)+5]
if (!arrayIsUnique(phaseSequence)) {
continue;
}
thrustSignal = test(phaseSequence);
if (maxSignal < thrustSignal) {
maxSignal = thrustSignal;
}
}
console.log(maxSignal);

View file

@ -5,6 +5,8 @@
3 | [:star: :star:](https://adventofcode.com/2019/day/3 "see puzzle") 3 | [:star: :star:](https://adventofcode.com/2019/day/3 "see puzzle")
4 | [:star: :star:](https://adventofcode.com/2019/day/4 "see puzzle") 4 | [:star: :star:](https://adventofcode.com/2019/day/4 "see puzzle")
5 | [:star: :star:](https://adventofcode.com/2019/day/5 "see puzzle") 5 | [:star: :star:](https://adventofcode.com/2019/day/5 "see puzzle")
6 | [:star: :star:](https://adventofcode.com/2019/day/6 "see puzzle")
7 | [:star: :star:](https://adventofcode.com/2019/day/7 "see puzzle")
# Languages Used # Languages Used
### Node JS ### Node JS
@ -15,6 +17,8 @@
3 | :star: 3 | :star:
4 | :star: :star: 4 | :star: :star:
5 | :star: :star: 5 | :star: :star:
6 | :star: :star:
7 | :star: :star:
### Python 3 ### Python 3