From d25c3b26d9afaa970c9265ed0cd77e0b4de686ad Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 5 Dec 2019 17:08:18 +0000 Subject: [PATCH] couple more days of 2019 --- 2019/03/input.txt | 2 ++ 2019/03/part1.js | 39 ++++++++++++++++++++++++++++++++++ 2019/04/part1.js | 18 ++++++++++++++++ 2019/04/part2.js | 28 ++++++++++++++++++++++++ 2019/05/input.txt | 1 + 2019/05/part1.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 2019/README.md | 9 +++++++- 7 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 2019/03/input.txt create mode 100644 2019/03/part1.js create mode 100644 2019/04/part1.js create mode 100644 2019/04/part2.js create mode 100644 2019/05/input.txt create mode 100644 2019/05/part1.js diff --git a/2019/03/input.txt b/2019/03/input.txt new file mode 100644 index 0000000..3207492 --- /dev/null +++ b/2019/03/input.txt @@ -0,0 +1,2 @@ +R75,D30,R83,U83,L12,D49,R71,U7,L72 +U62,R66,U55,R34,D71,R55,D58,R83 \ No newline at end of file diff --git a/2019/03/part1.js b/2019/03/part1.js new file mode 100644 index 0000000..3ccc4cf --- /dev/null +++ b/2019/03/part1.js @@ -0,0 +1,39 @@ +const fs = require('fs'); +const text = fs.readFileSync("./2019/03/input.txt", "utf-8"); + +let lineIns = text.split('\n').map(line => line.split(',')) +let linePoints = []; + +for (let line = 0; line < 2; line++){ + let points = [[0,0]]; + for (let i = 0; i < lineIns[line].length; i++){ + let segment = lineIns[line][i]; + let dist = parseInt(segment.slice(1)); + let axis = ((segment[0] == "R") || (segment[0] == "L")) ? 0 : 1; + let dir = ((segment[0] == "R") || (segment[0] == "U")) ? 1 : -1; + let coord = points[i]; + coord[axis] += dir * dist; + coord.axis = axis; + points.push(coord.slice()); + } + linePoints.push(points); +} + +let closest = Infinity; +for (let i = 0; i < linePoints[0].length-1; i++) { + let segment0 = linePoints[0].slice(i, i+2); + let axis0 = linePoints[0][i].axis; + segment0.sort((a, b) => a[(axis0+1)%2] - b[(axis0+1)%2]); + for (let j = 0; j < linePoints[1].length-1; j++) { + let axis1 = linePoints[1][j].axis; + if (axis0 == axis1) continue; + let segment1 = linePoints[1].slice(j, j+2); + segment1.sort((a, b) => a[(axis1+1)%2] - b[(axis1+1)%2]); + if (segment0[0][axis0] >= segment1[0][axis0] && segment0[1][axis0] <= segment1[1][axis0] && + segment1[0][axis1] >= segment0[0][axis1] && segment1[1][axis1] <= segment0[1][axis1]) { + let cross = Math.abs(segment0[0][axis0]) + Math.abs(segment1[0][axis1]); + closest = closest < cross ? closest : cross; + } + } +} +console.log(closest); diff --git a/2019/04/part1.js b/2019/04/part1.js new file mode 100644 index 0000000..ca46b6f --- /dev/null +++ b/2019/04/part1.js @@ -0,0 +1,18 @@ +// puzzle input +const inputMin = 172930; +const inputMax = 683082; + +amt = 0; +for (let i = inputMin; i < inputMax; i++) { + adjChars = false; + increasing = false; + let prev; + for (char of String(i)) { + if (char == prev) {adjChars = true} + if (char < prev) {increasing = true} + prev = char; + } + + if (adjChars && !increasing) {amt++;console.log(i)} +} +console.log(amt); \ No newline at end of file diff --git a/2019/04/part2.js b/2019/04/part2.js new file mode 100644 index 0000000..6268af8 --- /dev/null +++ b/2019/04/part2.js @@ -0,0 +1,28 @@ +const inputMin = 172930; +const inputMax = 683082; + + +amt = 0; +for (let i = inputMin; i < inputMax; i++) { + adjChars = 1; + pair = false; + decreasing = false; + let prev; + for (char of String(i)) { + if (char == prev) { + adjChars++; + } else if (adjChars == 2) { + pair = true; + } else {adjChars = 1} + + if (char < prev) {decreasing = true} + prev = char; + } + + if (adjChars == 2) { + pair = true; + } + + if (pair && !decreasing) {amt++;console.log(i)} +} +console.log(amt); \ No newline at end of file diff --git a/2019/05/input.txt b/2019/05/input.txt new file mode 100644 index 0000000..2408091 --- /dev/null +++ b/2019/05/input.txt @@ -0,0 +1 @@ +3,225,1,225,6,6,1100,1,238,225,104,0,1102,45,16,225,2,65,191,224,1001,224,-3172,224,4,224,102,8,223,223,1001,224,5,224,1,223,224,223,1102,90,55,225,101,77,143,224,101,-127,224,224,4,224,102,8,223,223,1001,224,7,224,1,223,224,223,1102,52,6,225,1101,65,90,225,1102,75,58,225,1102,53,17,224,1001,224,-901,224,4,224,1002,223,8,223,1001,224,3,224,1,224,223,223,1002,69,79,224,1001,224,-5135,224,4,224,1002,223,8,223,1001,224,5,224,1,224,223,223,102,48,40,224,1001,224,-2640,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,1101,50,22,225,1001,218,29,224,101,-119,224,224,4,224,102,8,223,223,1001,224,2,224,1,223,224,223,1101,48,19,224,1001,224,-67,224,4,224,102,8,223,223,1001,224,6,224,1,223,224,223,1101,61,77,225,1,13,74,224,1001,224,-103,224,4,224,1002,223,8,223,101,3,224,224,1,224,223,223,1102,28,90,225,4,223,99,0,0,0,677,0,0,0,0,0,0,0,0,0,0,0,1105,0,99999,1105,227,247,1105,1,99999,1005,227,99999,1005,0,256,1105,1,99999,1106,227,99999,1106,0,265,1105,1,99999,1006,0,99999,1006,227,274,1105,1,99999,1105,1,280,1105,1,99999,1,225,225,225,1101,294,0,0,105,1,0,1105,1,99999,1106,0,300,1105,1,99999,1,225,225,225,1101,314,0,0,106,0,0,1105,1,99999,7,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,8,226,677,224,1002,223,2,223,1005,224,344,101,1,223,223,8,226,226,224,1002,223,2,223,1006,224,359,101,1,223,223,1008,677,226,224,1002,223,2,223,1005,224,374,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,389,1001,223,1,223,1107,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,1008,226,226,224,102,2,223,223,1006,224,419,1001,223,1,223,7,677,226,224,1002,223,2,223,1005,224,434,101,1,223,223,1108,226,226,224,1002,223,2,223,1005,224,449,101,1,223,223,7,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,108,677,226,224,102,2,223,223,1005,224,479,1001,223,1,223,1007,677,226,224,1002,223,2,223,1006,224,494,1001,223,1,223,1007,677,677,224,1002,223,2,223,1006,224,509,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,524,101,1,223,223,1108,226,677,224,102,2,223,223,1006,224,539,1001,223,1,223,8,677,226,224,102,2,223,223,1005,224,554,101,1,223,223,1007,226,226,224,102,2,223,223,1006,224,569,1001,223,1,223,107,677,226,224,102,2,223,223,1005,224,584,1001,223,1,223,108,226,226,224,102,2,223,223,1006,224,599,1001,223,1,223,107,226,226,224,1002,223,2,223,1006,224,614,1001,223,1,223,1108,677,226,224,1002,223,2,223,1005,224,629,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,644,1001,223,1,223,1008,677,677,224,102,2,223,223,1005,224,659,101,1,223,223,1107,677,226,224,1002,223,2,223,1006,224,674,101,1,223,223,4,223,99,226 \ No newline at end of file diff --git a/2019/05/part1.js b/2019/05/part1.js new file mode 100644 index 0000000..be7fda9 --- /dev/null +++ b/2019/05/part1.js @@ -0,0 +1,54 @@ +const fs = require("fs"); +const text = fs.readFileSync("2019/05/input.txt", "utf-8"); +const userInput = 5; + +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(); +} + +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') { + console.log("IN "+userInput); + memory[memory[i+1]] = userInput; + i+=2; + } else if (opcode == '04') { + console.log("OUT "+valueOf(i+1, instruction[2])); + 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") + break; + } +} diff --git a/2019/README.md b/2019/README.md index 4c46cd1..4ae406f 100644 --- a/2019/README.md +++ b/2019/README.md @@ -2,9 +2,16 @@ 1 | [:star: :star:](https://adventofcode.com/2019/day/1 "see puzzle") --: | :-- 2 | [:star: :star:](https://adventofcode.com/2019/day/2 "see puzzle") +3 | [:star:](https://adventofcode.com/2019/day/3 "see puzzle") +4 | [:star: :star:](https://adventofcode.com/2019/day/4 "see puzzle") +5 | [:star: :star:](https://adventofcode.com/2019/day/5 "see puzzle") # Languages Used ### Node JS 1 | :star: :star: -2 | :star: :star: \ No newline at end of file +--: | :-- +2 | :star: :star: +3 | :star: +4 | :star: :star: +5 | :star: :star: \ No newline at end of file