• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest Dec 3rd, 2019 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. const wire1 = 'Your input here'.split(',');
2. const wire2 = 'Your input here'.split(',');
3.
4. const convertToCoordinates = (wirePath) => {
5.     let currentX = 0;
6.     let currentY = 0;
7.
8.     const coordinates = [];
9.
10.     wirePath.forEach(step => {
11.         const direction = step.substring(0, 1);
12.         const distance = parseInt(step.substring(1));
13.
14.         for (var i = 1; i <= distance; i++) {
15.             switch (direction) {
16.                 case 'R':
17.                     currentY++;
18.                     coordinates.push({ x: currentX, y: currentY });
19.                     break;
20.                 case 'L':
21.                     currentY--;
22.                     coordinates.push({ x: currentX, y: currentY });
23.                     break;
24.                 case 'U':
25.                     currentX++;
26.                     coordinates.push({ x: currentX, y: currentY });
27.                     break;
28.                 case 'D':
29.                     currentX--;
30.                     coordinates.push({ x: currentX, y: currentY });
31.                     break;
32.             }
33.         }
34.     });
35.
36.     return coordinates;
37. };
38.
39. const findWireCrossings = (wire1Coordinates, wire2Coordinates) => {
40.     return wire1Coordinates.reduce((crossings, wire1Coord) => {
41.         const crossed = wire2Coordinates.find(wire2Coord => wire2Coord.x === wire1Coord.x && wire2Coord.y === wire1Coord.y);
42.
43.         if (crossed) {
44.             crossings.push(crossed);
45.             return crossings;
46.         }
47.
48.         return crossings;
49.     }, []);
50. };
51.
52. const findShortestDistance = (crossings) => {
53.     return crossings.reduce((shortestDistance, crossing) => {
54.         const distanceFromCentralPort = Math.abs(crossing.x) + Math.abs(crossing.y);
55.
56.         if (distanceFromCentralPort < shortestDistance) {
57.             return distanceFromCentralPort;
58.         }
59.
60.         return shortestDistance;
61.     }, Infinity);
62. };
63.
64. const stepsToCrossing = (wirePath, target) => {
65.     let currentX = 0;
66.     let currentY = 0;
67.     let steps = 0;
68.
69.     outer:
70.     for (var i = 0; i < wirePath.length; i++) {
71.         const direction = wirePath[i].substring(0, 1);
72.         const distance = parseInt(wirePath[i].substring(1));
73.
74.         for (var j = 1; j <= distance; j++) {
75.             if (currentX === target.x && currentY === target.y) {
76.                 break outer;
77.             }
78.
79.             steps++;
80.             switch (direction) {
81.                 case 'R':
82.                     currentY++;
83.                     break;
84.                 case 'L':
85.                     currentY--;
86.                     break;
87.                 case 'U':
88.                     currentX++;
89.                     break;
90.                 case 'D':
91.                     currentX--;
92.                     break;
93.             }
94.         }
95.     }
96.
97.     return steps;
98. };
99.
100. const findMinimalSignalDelay = (crossings, wire1Path, wire2Path) => {
101.     return crossings.reduce((steps, crossing) => {
102.         const totalSteps = stepsToCrossing(wire1Path, crossing) + stepsToCrossing(wire2Path, crossing);
103.
104.         if (totalSteps < steps) {
106.         }
107.
108.         return steps;
109.     }, Infinity);
110. };
111.
112. const program = () => {
113.     const wire1Coordinates = convertToCoordinates(wire1);
114.     const wire2Coordinates = convertToCoordinates(wire2);
115.     const crossings = findWireCrossings(wire1Coordinates, wire2Coordinates);
116.     const shortestDistance = findShortestDistance(crossings);
117.     console.log(shortestDistance);
118.     const minimalSignalDelay = findMinimalSignalDelay(crossings, wire1, wire2);
119.     console.log(minimalSignalDelay);
120. };
121.
122. program();
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top