├── README.md ├── day_1.js ├── day_2.js ├── day_4.js ├── day_5.js └── day_3.js /README.md: -------------------------------------------------------------------------------- 1 | # Advent of Code 2019 2 | My solutions of the programming puzzles of [Advent of Code 2019](https://adventofcode.com/2019) 3 | 4 | - [Day 1](https://github.com/ilhan-mstf/advent_of_code_2019/blob/master/day_1.js) 5 | - [Day 2](https://github.com/ilhan-mstf/advent_of_code_2019/blob/master/day_2.js) 6 | - [Day 3](https://github.com/ilhan-mstf/advent_of_code_2019/blob/master/day_3.js) 7 | - [Day 4](https://github.com/ilhan-mstf/advent_of_code_2019/blob/master/day_4.js) 8 | - [Day 5](https://github.com/ilhan-mstf/advent_of_code_2019/blob/master/day_5.js) 9 | -------------------------------------------------------------------------------- /day_1.js: -------------------------------------------------------------------------------- 1 | // Question: https://adventofcode.com/2019/day/1 2 | // Input file: https://adventofcode.com/2019/day/1/input 3 | 4 | // Run this code on browser's console 5 | 6 | // Part 1 7 | let modules = document.querySelector('pre').innerText.split('\n').filter(m => m !== '') 8 | 9 | function calculateFuel(mass) { 10 | return Math.floor(Number(mass) / 3) - 2; 11 | } 12 | 13 | modules.map(m => calculateFuel(m)).reduce((total, m) => total + m); 14 | 15 | 16 | // Part 2 17 | function calculateIncludingFuel(mass) { 18 | let total = 0; 19 | while (true) { 20 | mass = calculateFuel(mass); 21 | if (mass > 0) { 22 | total += mass; 23 | } else { 24 | break; 25 | } 26 | } 27 | return total; 28 | } 29 | 30 | modules.map(m => calculateIncludingFuel(m)).reduce((total, m) => total + m); 31 | -------------------------------------------------------------------------------- /day_2.js: -------------------------------------------------------------------------------- 1 | let states = document.querySelector("pre").innerText.split(",").map(state => Number(state)); 2 | states[1] = 12 3 | states[2] = 2 4 | 5 | function sum(states, index) { 6 | states[states[index + 3]] = states[states[index + 1]] + states[states[index + 2]] 7 | } 8 | 9 | function multiply(states, index) { 10 | states[states[index + 3]] = states[states[index + 1]] * states[states[index + 2]] 11 | } 12 | 13 | function calculateGravity(states) { 14 | let index = 0; 15 | let operator; 16 | 17 | while (true) { 18 | operator = states[index]; 19 | if (operator === 1) { 20 | sum(states, index) 21 | } else if (operator === 2) { 22 | multiply(states, index) 23 | } else if (operator === 99) { 24 | break; 25 | } else { 26 | throw Error("Unhandled operator " + operator); 27 | } 28 | index +=4; 29 | console.log(states); 30 | } 31 | return states[0]; 32 | } 33 | 34 | // Part 1 35 | calculateGravity(states); 36 | 37 | // Part 2 38 | states = document.querySelector("pre").innerText.split(",").map(state => Number(state)); 39 | 40 | // Diff between goal and first run 19690720 - 3562624 = 16128096 41 | // When states[1] increased to 13 from 12, the output increases 230400 42 | 43 | // When diff / increase of first index will give us the value of first index 44 | // 16128096 / 230400 = 70.00041666666667 45 | // states[1] += 70 46 | 47 | // When you run it with updated states[1] value should be 19690624 48 | // Diff between goal and final state is 19690720 - 19690624 = 96 49 | // states[2] += 96 50 | -------------------------------------------------------------------------------- /day_4.js: -------------------------------------------------------------------------------- 1 | function getDigitsAsArray(number) { 2 | let digits = []; 3 | let divisor = 10; 4 | 5 | while (true) { 6 | digits.push(number % divisor); 7 | number = Math.floor(number / divisor); 8 | if (number === 0) { 9 | break; 10 | } 11 | } 12 | 13 | return digits.reverse(); 14 | } 15 | 16 | function digitsAreEqualOrIncreasing(digits) { 17 | let prev = digits[0]; 18 | 19 | for (let i = 1, ii = digits.length; i != ii; i++) { 20 | if (digits[i] < prev) { 21 | return false; 22 | } 23 | prev = digits[i]; 24 | } 25 | 26 | return true; 27 | } 28 | 29 | function thereAreSameAdjecentDigits(digits) { 30 | let prev = digits[0]; 31 | 32 | for (let i = 1, ii = digits.length; i != ii; i++) { 33 | if (digits[i] == prev) { 34 | return true; 35 | } 36 | prev = digits[i]; 37 | } 38 | 39 | return false; 40 | } 41 | 42 | function findPossibilities() { 43 | let possible = 0; 44 | 45 | for (let i = 244444; i <= 788999; i++) { 46 | let digits = getDigitsAsArray(i); 47 | if (digitsAreEqualOrIncreasing(digits) 48 | && thereAreSameAdjecentDigits(digits)) { 49 | possible++; 50 | } 51 | } 52 | 53 | return possible; 54 | } 55 | 56 | //getDigitsAsArray(1231231); 57 | //findPossibilities(); 58 | 59 | function getFreqMap(digits) { 60 | let freqMap = {}; 61 | 62 | digits.forEach(digit => { 63 | if (!freqMap[digit]) { 64 | freqMap[digit] = 0; 65 | } 66 | freqMap[digit] += 1; 67 | }); 68 | 69 | return freqMap; 70 | } 71 | 72 | function havingAtLeastOneTwoSameAdjacentGroup(freqMap) { 73 | for (const [key, freq] of Object.entries(freqMap)) { 74 | if (freq === 2) { 75 | return true; 76 | } 77 | } 78 | 79 | return false; 80 | } 81 | 82 | function findPossibilitiesOfPart2() { 83 | let possible = 0; 84 | 85 | for (let i = 244444; i <= 788999; i++) { 86 | let digits = getDigitsAsArray(i); 87 | if (digitsAreEqualOrIncreasing(digits) 88 | && thereAreSameAdjecentDigits(digits) 89 | && havingAtLeastOneTwoSameAdjacentGroup(getFreqMap(digits))) { 90 | possible++; 91 | } 92 | } 93 | 94 | return possible; 95 | } 96 | 97 | console.log(havingAtLeastOneTwoSameAdjacentGroup(getFreqMap(getDigitsAsArray(112233)))); 98 | console.log(havingAtLeastOneTwoSameAdjacentGroup(getFreqMap(getDigitsAsArray(123444)))); 99 | console.log(havingAtLeastOneTwoSameAdjacentGroup(getFreqMap(getDigitsAsArray(111122)))); 100 | 101 | findPossibilitiesOfPart2(); 102 | -------------------------------------------------------------------------------- /day_5.js: -------------------------------------------------------------------------------- 1 | function getDigitsAsArray(number) { 2 | let digits = []; 3 | let divisor = 10; 4 | 5 | while (true) { 6 | digits.push(number % divisor); 7 | number = Math.floor(number / divisor); 8 | if (number === 0) { 9 | break; 10 | } 11 | } 12 | 13 | return digits.reverse(); 14 | } 15 | 16 | function getArgsConsideringModes(opts) { 17 | if (!opts.parameterModes) { 18 | opts.parameterModes = [0, 0]; 19 | } 20 | 21 | let arg1 = opts.parameterModes[1] ? opts.states[opts.index + 1] : opts.states[opts.states[opts.index + 1]]; 22 | let arg2 = opts.parameterModes[0] ? opts.states[opts.index + 2] : opts.states[opts.states[opts.index + 2]]; 23 | 24 | return [arg1, arg2]; 25 | } 26 | 27 | function sum(opts) { 28 | let args = getArgsConsideringModes(opts); 29 | 30 | opts.states[opts.states[opts.index + 3]] = args[0] + args[1]; 31 | opts.index += 4; 32 | return opts; 33 | } 34 | 35 | function multiply(opts) { 36 | let args = getArgsConsideringModes(opts); 37 | 38 | opts.states[opts.states[opts.index + 3]] = args[0] * args[1]; 39 | opts.index += 4; 40 | return opts; 41 | } 42 | 43 | function read(opts) { 44 | opts.states[opts.states[opts.index + 1]] = 5; 45 | opts.index += 2; 46 | return opts; 47 | } 48 | 49 | function print(opts) { 50 | let args = getArgsConsideringModes(opts); 51 | 52 | console.log(args[0]); 53 | opts.index += 2; 54 | return opts; 55 | } 56 | 57 | function jumpIfTrue(opts) { 58 | let args = getArgsConsideringModes(opts); 59 | 60 | if (args[0] !== 0) { 61 | opts.index = args[1]; 62 | } else { 63 | opts.index += 3; 64 | } 65 | 66 | return opts; 67 | } 68 | 69 | function jumpIfFalse(opts) { 70 | let args = getArgsConsideringModes(opts); 71 | 72 | if (args[0] === 0) { 73 | opts.index = args[1]; 74 | } else { 75 | opts.index += 3; 76 | } 77 | 78 | return opts; 79 | } 80 | 81 | function lessThan(opts) { 82 | let args = getArgsConsideringModes(opts); 83 | 84 | opts.states[opts.states[opts.index + 3]] = args[0] < args[1] ? 1 : 0; 85 | 86 | opts.index += 4; 87 | return opts; 88 | } 89 | 90 | function equals(opts) { 91 | let args = getArgsConsideringModes(opts); 92 | 93 | opts.states[opts.states[opts.index + 3]] = args[0] === args[1] ? 1 : 0; 94 | 95 | opts.index += 4; 96 | return opts; 97 | } 98 | 99 | function parseOperator(opts) { 100 | opts.operator = opts.states[opts.index]; 101 | opts.parameterModes = []; 102 | 103 | if (opts.operator > 99) { 104 | opts.parameterModes = getDigitsAsArray(opts.operator); 105 | if (opts.parameterModes.length == 3) { 106 | opts.parameterModes.unshift(0); 107 | } 108 | opts.operator = opts.parameterModes[opts.parameterModes.length - 1]; 109 | } 110 | 111 | return opts; 112 | } 113 | 114 | function handleOperator(opts) { 115 | if (opts.operator === 1) { 116 | sum(opts); 117 | } else if (opts.operator === 2) { 118 | multiply(opts); 119 | } else if (opts.operator === 3) { 120 | read(opts); 121 | } else if (opts.operator === 4) { 122 | print(opts); 123 | } else if (opts.operator === 5) { 124 | jumpIfTrue(opts); 125 | } else if (opts.operator === 6) { 126 | jumpIfFalse(opts); 127 | } else if (opts.operator === 7) { 128 | lessThan(opts); 129 | } else if (opts.operator === 8) { 130 | equals(opts); 131 | } else if (opts.operator === 99) { 132 | console.log("halt"); 133 | opts.halt = true; 134 | } else { 135 | throw Error("Unhandled operator " + opts.operator); 136 | } 137 | return opts; 138 | } 139 | 140 | function calculateGravity(states) { 141 | let opts = { 142 | index: 0, 143 | operator: 0, 144 | parameterModes: [], 145 | states: states, 146 | halt: false 147 | } 148 | 149 | while (!opts.halt) { 150 | parseOperator(opts); 151 | handleOperator(opts); 152 | } 153 | 154 | return opts.states[0]; 155 | } 156 | 157 | //calculateGravity([3,0,4,0,99]); 158 | calculateGravity([3,225,1,225,6,6,1100,1,238,225,104,0,1102,91,92,225,1102,85,13,225,1,47,17,224,101,-176,224,224,4,224,1002,223,8,223,1001,224,7,224,1,223,224,223,1102,79,43,225,1102,91,79,225,1101,94,61,225,1002,99,42,224,1001,224,-1890,224,4,224,1002,223,8,223,1001,224,6,224,1,224,223,223,102,77,52,224,1001,224,-4697,224,4,224,102,8,223,223,1001,224,7,224,1,224,223,223,1101,45,47,225,1001,43,93,224,1001,224,-172,224,4,224,102,8,223,223,1001,224,1,224,1,224,223,223,1102,53,88,225,1101,64,75,225,2,14,129,224,101,-5888,224,224,4,224,102,8,223,223,101,6,224,224,1,223,224,223,101,60,126,224,101,-148,224,224,4,224,1002,223,8,223,1001,224,2,224,1,224,223,223,1102,82,56,224,1001,224,-4592,224,4,224,1002,223,8,223,101,4,224,224,1,224,223,223,1101,22,82,224,1001,224,-104,224,4,224,1002,223,8,223,101,4,224,224,1,223,224,223,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,8,226,677,224,102,2,223,223,1005,224,329,1001,223,1,223,1007,226,226,224,1002,223,2,223,1006,224,344,101,1,223,223,108,226,226,224,1002,223,2,223,1006,224,359,1001,223,1,223,107,226,677,224,102,2,223,223,1006,224,374,101,1,223,223,8,677,677,224,102,2,223,223,1006,224,389,1001,223,1,223,1008,226,677,224,1002,223,2,223,1006,224,404,101,1,223,223,7,677,677,224,1002,223,2,223,1005,224,419,101,1,223,223,1108,226,677,224,1002,223,2,223,1005,224,434,101,1,223,223,1108,226,226,224,102,2,223,223,1005,224,449,1001,223,1,223,107,226,226,224,102,2,223,223,1005,224,464,101,1,223,223,1007,677,677,224,102,2,223,223,1006,224,479,101,1,223,223,1007,226,677,224,102,2,223,223,1005,224,494,1001,223,1,223,1008,226,226,224,1002,223,2,223,1005,224,509,1001,223,1,223,1108,677,226,224,1002,223,2,223,1006,224,524,1001,223,1,223,108,677,677,224,1002,223,2,223,1005,224,539,101,1,223,223,108,226,677,224,1002,223,2,223,1005,224,554,101,1,223,223,1008,677,677,224,1002,223,2,223,1006,224,569,1001,223,1,223,1107,677,677,224,102,2,223,223,1005,224,584,1001,223,1,223,7,677,226,224,102,2,223,223,1005,224,599,1001,223,1,223,8,677,226,224,1002,223,2,223,1005,224,614,1001,223,1,223,7,226,677,224,1002,223,2,223,1006,224,629,101,1,223,223,1107,677,226,224,1002,223,2,223,1005,224,644,1001,223,1,223,1107,226,677,224,102,2,223,223,1006,224,659,1001,223,1,223,107,677,677,224,1002,223,2,223,1005,224,674,101,1,223,223,4,223,99,226]); 159 | 160 | // Part 2 161 | //calculateGravity([3,9,8,9,10,9,4,9,99,-1,8]); 162 | //calculateGravity([3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99]); 163 | -------------------------------------------------------------------------------- /day_3.js: -------------------------------------------------------------------------------- 1 | function drawLines(step, wire) { 2 | let nextPoint = Number(step.substr(1)); 3 | 4 | if (step[0] === "R") { 5 | wire.lines.horizontal.push({ 6 | x: [wire.currentPoint[0], wire.currentPoint[0] + nextPoint], 7 | y: wire.currentPoint[1] 8 | }); 9 | wire.currentPoint[0] += nextPoint; 10 | } else if (step[0] === "L") { 11 | wire.lines.horizontal.push({ 12 | x: [wire.currentPoint[0], wire.currentPoint[0] - nextPoint], 13 | y: wire.currentPoint[1] 14 | }); 15 | wire.currentPoint[0] -= nextPoint; 16 | } else if (step[0] === "D") { 17 | wire.lines.vertical.push({ 18 | x: wire.currentPoint[0], 19 | y: [wire.currentPoint[1], wire.currentPoint[1] - nextPoint] 20 | }); 21 | wire.currentPoint[1] -= nextPoint; 22 | } else if (step[0] === "U") { 23 | wire.lines.vertical.push({ 24 | x: wire.currentPoint[0], 25 | y: [wire.currentPoint[1], wire.currentPoint[1] + nextPoint] 26 | }); 27 | wire.currentPoint[1] += nextPoint; 28 | } else { 29 | throw Error("not supported step " + step); 30 | } 31 | 32 | wire.corners.push([wire.currentPoint[0], wire.currentPoint[1]]); 33 | 34 | //console.log(wire.currentPoint); 35 | return wire; 36 | } 37 | 38 | function initWire() { 39 | return { 40 | currentPoint: [0, 0], 41 | corners: [], 42 | lines: { 43 | vertical: [], 44 | horizontal: [] 45 | } 46 | } 47 | } 48 | 49 | function drawWirePath(wirePath) { 50 | let wire = initWire(); 51 | let steps = wirePath.split(","); 52 | 53 | steps.forEach(step => drawLines(step, wire)); 54 | return wire; 55 | } 56 | 57 | function doesIntersect(verticalLine, horizontalLine) { 58 | //console.log(verticalLine, horizontalLine); 59 | return ((verticalLine.x >= horizontalLine.x[0] 60 | && verticalLine.x <= horizontalLine.x[1]) 61 | || 62 | (verticalLine.x >= horizontalLine.x[1] 63 | && verticalLine.x <= horizontalLine.x[0])) 64 | && 65 | ((horizontalLine.y >= verticalLine.y[0] 66 | && horizontalLine.y <= verticalLine.y[1]) 67 | || 68 | (horizontalLine.y >= verticalLine.y[1] 69 | && horizontalLine.y <= verticalLine.y[0])); 70 | } 71 | 72 | function findIntersectionPointsOfLines(verticalLines, horizontalLines, intersectionPoints) { 73 | verticalLines.forEach(verticalLine => { 74 | horizontalLines.forEach(horizontalLine => { 75 | if (doesIntersect(verticalLine, horizontalLine)) { 76 | intersectionPoints.push([verticalLine.x, horizontalLine.y]); 77 | } 78 | }); 79 | }); 80 | 81 | return intersectionPoints; 82 | } 83 | 84 | function findIntersectionPointsOfTwoWires(wires) { 85 | let intersectionPoints = []; 86 | 87 | findIntersectionPointsOfLines( 88 | wires[0].lines.vertical, 89 | wires[1].lines.horizontal, 90 | intersectionPoints); 91 | findIntersectionPointsOfLines( 92 | wires[1].lines.vertical, 93 | wires[0].lines.horizontal, 94 | intersectionPoints); 95 | 96 | return intersectionPoints; 97 | } 98 | 99 | function findMinManhattanDistanceOfIntersectionPoints(intersectionPoints) { 100 | let minDistance = Number.MAX_SAFE_INTEGER; 101 | 102 | intersectionPoints.forEach(point => { 103 | let distance = Math.abs(point[0]) + Math.abs(point[1]); 104 | if (distance !== 0 && distance < minDistance) { 105 | minDistance = distance; 106 | } 107 | }); 108 | 109 | return minDistance; 110 | } 111 | 112 | function findMinManhattanDistanceOfWires(wires) { 113 | wires = wires.map(drawWirePath); 114 | console.log(JSON.stringify(wires)); 115 | 116 | let intersectionPoints = findIntersectionPointsOfTwoWires(wires); 117 | console.log(JSON.stringify(intersectionPoints)); 118 | 119 | let minDistance = findMinManhattanDistanceOfIntersectionPoints(intersectionPoints); 120 | console.log(minDistance); 121 | 122 | return minDistance; 123 | } 124 | 125 | 126 | /* 127 | findMinManhattanDistanceOfWires( 128 | ["R8,U5,L5,D3", 129 | "U7,R6,D4,L4"]); 130 | 131 | findMinManhattanDistanceOfWires( 132 | ["R75,D30,R83,U83,L12,D49,R71,U7,L72", 133 | "U62,R66,U55,R34,D71,R55,D58,R83"]); 134 | findMinManhattanDistanceOfWires( 135 | ["R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", 136 | "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7"]); 137 | 138 | findMinManhattanDistanceOfWires(["R990,U408,L583,U275,R483,U684,R437,U828,R108,U709,R378,U97,R252,D248,R413,U750,R428,D545,R570,D795,L204,D975,L557,U160,L861,U106,R436,U934,R81,D237,R660,U704,L451,U135,R282,D391,R39,D109,R125,U918,R214,U481,R853,U825,L91,D763,R335,U868,R42,U218,R152,D429,R414,D607,R28,U436,R7,U770,L215,D373,R209,U440,L536,U120,R900,D46,R635,D75,R58,U267,L581,U474,L858,U172,R725,U54,R291,D274,L583,D743,L130,U563,R137,U524,R659,D997,R131,D364,R883,D222,R628,U579,R801,D890,L519,D749,L620,U60,L759,D759,R376,U769,L910,D570,L814,U954,L153,D42,L784,D66,L844,U29,L794,D342,L924,U825,R447,U828,R404,D52,L330,D876,R125,U203,R245,U936,R866,D804,L186,U693,L620,D722,L32,D735,L191,D217,R68,U209,L736,U365,R280,U608,L450,D240,L282,U434,R589,U94,R470,D5,R49,U407,R552,D651,L69,U518,L358,D130,L710,D929,L315,U345,L511,D229,L557,U44,L890,D702,L181,D61,L208,U553,R878,U354,R787,U624,L961,D92,L891,U70,R203,U255,R532,U154,R299,U934,L609,D985,R115,U757,L13,D368,R936,D742,L412,U346,R56,D67,R371,D175,R868,U107,R806,D530,L40,U153,R374,D223,R517,D481,L194,U545,L356,U906,L999,D885,R967,U407,L141,U927,L489,U959,L992,U638,R332,U51,R256,U901,L891,U803,L885,U804,L242,U180,R277,U693,R935,D253,L68,D153,L614,D596,L999,D633,R995,D803,R17,U303,L569,U231,R737,D970,L45,D860,L225,D65,R41,D313,R698,D340,R599,D531,R55,D568,L911,D547,R196,D228,R868,D227,R262,U525,R104,D625,R570,U968,L276,D586,R690,D73,L336,U287,R294,U148,R781,D395,R478,D804,L429,U872,L351,D910,L597,U726,L320,D964,R928,U2,R540,D325,L222", 139 | "L998,U662,R342,U104,R140,U92,R67,D102,L225,U265,R641,U592,L295,D77,R415,U908,L640,D381,R312,U44,R424,D847,R892,D625,L337,D344,L917,D914,R127,D273,L627,U812,L200,D262,R226,U273,R911,U597,L888,U28,R921,U464,R254,U771,R818,D808,L239,D225,L280,U785,R322,D831,L622,U506,R139,U12,L491,D572,L172,U685,R54,U747,L812,D717,R874,U428,L867,U174,R360,D36,R217,D539,R210,D791,L82,D665,L190,D313,R649,U849,R63,U385,R105,U806,L207,U697,L823,D272,R830,D952,L386,U987,R775,U517,R139,D756,R545,D973,L743,D286,R261,U448,R946,U884,L903,D142,R28,D374,R259,U403,R689,D245,L302,D134,R710,U762,L67,D561,R801,D140,L887,U346,L227,U682,L350,D218,L711,U755,R226,D277,R114,D61,R992,U602,L191,U640,R733,D329,R862,U242,R754,D161,L52,D974,L251,D444,L552,U977,R174,U483,R869,D955,R925,U693,R610,D353,L843,U148,L866,D167,R412,D31,L847,D979,L282,D797,L837,U473,L402,U193,L332,D603,R48,D589,L760,D673,L843,U428,R779,D592,L688,D141,R851,D642,R559,U939,R999,D64,L297,U817,R670,U322,L768,D936,L39,U95,L342,U849,L692,U714,L732,D734,L373,U66,L577,D453,R336,U760,L217,U542,R920,U24,R529,D594,L34,D79,R877,D965,R932,U460,R879,U26,R803,U876,L780,U956,L235,D270,L315,D577,R835,U750,R414,D584,L828,U335,L563,U238,L815,U780,L550,U18,R743,D54,L816,U344,L806,D197,L518,D682,L835,U255,L666,U442,L286,D543,R102,D52,L570,D787,L763,D223,R279,D892,L828,D111,L554,D452,R575,D299,R932,D187,L439,U616,L278,D701,L360,D524,L891,U953,L896,U788,R776,U782,L71,D741,L652,U121,R669,D809,L662,U319,R392,D313,R870,U794,R937,D469,R571,D761,R947"]); 140 | */ 141 | 142 | function isOnTheLine(point, corner) { 143 | return (point[0] == corner[0] && point[1] > corner[1]) 144 | || (point[1] == corner[1] && point[0] > corner[0]); 145 | } 146 | 147 | function getDelayBetweenCorners(prev, curr) { 148 | return Math.abs(curr[0] - prev[0]) + Math.abs(curr[1] - prev[1]); 149 | } 150 | 151 | // Part 2 152 | function findSignalDelay(point, wire) { 153 | let totalDelay = 0; 154 | let prevCorner = [0, 0]; 155 | for (let i = 0, ii = wire.corners.length; i != ii; i++) { 156 | let corner = wire.corners[i]; 157 | let delay = 0; 158 | if (isOnTheLine(point, corner)) { 159 | delay = getDelayBetweenCorners(prevCorner, point); 160 | totalDelay += delay; 161 | //console.log(delay); 162 | break; 163 | } else { 164 | delay = getDelayBetweenCorners(prevCorner, corner); 165 | prevCorner = corner 166 | //console.log(delay); 167 | totalDelay += delay; 168 | } 169 | } 170 | 171 | //console.log("totalDelay", totalDelay); 172 | return totalDelay; 173 | } 174 | 175 | function findMinDelay(wires) { 176 | wires = wires.map(drawWirePath); 177 | //console.log(JSON.stringify(wires)); 178 | 179 | let intersectionPoints = findIntersectionPointsOfTwoWires(wires); 180 | //console.log(JSON.stringify(intersectionPoints)); 181 | 182 | let minDelay = Number.MAX_SAFE_INTEGER; 183 | intersectionPoints.forEach(point => { 184 | //console.log(point); 185 | let delay = wires.map(wire => findSignalDelay(point, wire)).reduce((total, delay) => total + delay); 186 | if (delay < minDelay) { 187 | minDelay = delay; 188 | } 189 | }); 190 | 191 | console.log(minDelay); 192 | return minDelay; 193 | } 194 | 195 | findMinDelay( 196 | ["R8,U5,L5,D3", 197 | "U7,R6,D4,L4"]); 198 | 199 | findMinDelay( 200 | ["R75,D30,R83,U83,L12,D49,R71,U7,L72", 201 | "U62,R66,U55,R34,D71,R55,D58,R83"]); 202 | findMinDelay( 203 | ["R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", 204 | "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7"]); 205 | findMinDelay(["R990,U408,L583,U275,R483,U684,R437,U828,R108,U709,R378,U97,R252,D248,R413,U750,R428,D545,R570,D795,L204,D975,L557,U160,L861,U106,R436,U934,R81,D237,R660,U704,L451,U135,R282,D391,R39,D109,R125,U918,R214,U481,R853,U825,L91,D763,R335,U868,R42,U218,R152,D429,R414,D607,R28,U436,R7,U770,L215,D373,R209,U440,L536,U120,R900,D46,R635,D75,R58,U267,L581,U474,L858,U172,R725,U54,R291,D274,L583,D743,L130,U563,R137,U524,R659,D997,R131,D364,R883,D222,R628,U579,R801,D890,L519,D749,L620,U60,L759,D759,R376,U769,L910,D570,L814,U954,L153,D42,L784,D66,L844,U29,L794,D342,L924,U825,R447,U828,R404,D52,L330,D876,R125,U203,R245,U936,R866,D804,L186,U693,L620,D722,L32,D735,L191,D217,R68,U209,L736,U365,R280,U608,L450,D240,L282,U434,R589,U94,R470,D5,R49,U407,R552,D651,L69,U518,L358,D130,L710,D929,L315,U345,L511,D229,L557,U44,L890,D702,L181,D61,L208,U553,R878,U354,R787,U624,L961,D92,L891,U70,R203,U255,R532,U154,R299,U934,L609,D985,R115,U757,L13,D368,R936,D742,L412,U346,R56,D67,R371,D175,R868,U107,R806,D530,L40,U153,R374,D223,R517,D481,L194,U545,L356,U906,L999,D885,R967,U407,L141,U927,L489,U959,L992,U638,R332,U51,R256,U901,L891,U803,L885,U804,L242,U180,R277,U693,R935,D253,L68,D153,L614,D596,L999,D633,R995,D803,R17,U303,L569,U231,R737,D970,L45,D860,L225,D65,R41,D313,R698,D340,R599,D531,R55,D568,L911,D547,R196,D228,R868,D227,R262,U525,R104,D625,R570,U968,L276,D586,R690,D73,L336,U287,R294,U148,R781,D395,R478,D804,L429,U872,L351,D910,L597,U726,L320,D964,R928,U2,R540,D325,L222", 206 | "L998,U662,R342,U104,R140,U92,R67,D102,L225,U265,R641,U592,L295,D77,R415,U908,L640,D381,R312,U44,R424,D847,R892,D625,L337,D344,L917,D914,R127,D273,L627,U812,L200,D262,R226,U273,R911,U597,L888,U28,R921,U464,R254,U771,R818,D808,L239,D225,L280,U785,R322,D831,L622,U506,R139,U12,L491,D572,L172,U685,R54,U747,L812,D717,R874,U428,L867,U174,R360,D36,R217,D539,R210,D791,L82,D665,L190,D313,R649,U849,R63,U385,R105,U806,L207,U697,L823,D272,R830,D952,L386,U987,R775,U517,R139,D756,R545,D973,L743,D286,R261,U448,R946,U884,L903,D142,R28,D374,R259,U403,R689,D245,L302,D134,R710,U762,L67,D561,R801,D140,L887,U346,L227,U682,L350,D218,L711,U755,R226,D277,R114,D61,R992,U602,L191,U640,R733,D329,R862,U242,R754,D161,L52,D974,L251,D444,L552,U977,R174,U483,R869,D955,R925,U693,R610,D353,L843,U148,L866,D167,R412,D31,L847,D979,L282,D797,L837,U473,L402,U193,L332,D603,R48,D589,L760,D673,L843,U428,R779,D592,L688,D141,R851,D642,R559,U939,R999,D64,L297,U817,R670,U322,L768,D936,L39,U95,L342,U849,L692,U714,L732,D734,L373,U66,L577,D453,R336,U760,L217,U542,R920,U24,R529,D594,L34,D79,R877,D965,R932,U460,R879,U26,R803,U876,L780,U956,L235,D270,L315,D577,R835,U750,R414,D584,L828,U335,L563,U238,L815,U780,L550,U18,R743,D54,L816,U344,L806,D197,L518,D682,L835,U255,L666,U442,L286,D543,R102,D52,L570,D787,L763,D223,R279,D892,L828,D111,L554,D452,R575,D299,R932,D187,L439,U616,L278,D701,L360,D524,L891,U953,L896,U788,R776,U782,L71,D741,L652,U121,R669,D809,L662,U319,R392,D313,R870,U794,R937,D469,R571,D761,R947"]); 207 | 208 | --------------------------------------------------------------------------------