Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fsPromises = require('fs').promises;
- function getLimits(array) {
- let xmax = 0;
- let xmin = 0;
- let ymax = 0;
- let ymin = 0;
- let x = 0;
- let y = 0;
- array.forEach(elem => {
- let direction = elem[0];
- let distance = parseInt(elem.substr(1), 10);
- let xdifference = 0;
- let ydifference = 0;
- switch(direction) {
- case "R":
- xdifference += distance;
- x += xdifference;
- xmax = Math.max(x, xmax);
- xdifference += distance;
- break;
- case "L":
- xdifference -= distance;
- x -= xdifference;
- xmin = Math.min(x, xmin);
- break;
- case "U":
- ydifference += distance;
- y += ydifference;
- ymax = Math.max(y, ymax);
- case "D":
- ydifference -= distance;
- y -= ydifference;
- ymin = Math.min(y, ymin);
- }
- })
- return {
- xmax, xmin, ymax, ymin
- };
- }
- function combineLimits(limits1, limits2) {
- return {
- xmax: Math.max(limits1.xmax, limits2.xmax),
- xmin: Math.min(limits1.xmin, limits2.xmin),
- ymax: Math.max(limits1.ymax, limits2.ymax),
- ymin: Math.min(limits1.ymin, limits2.ymin)
- }
- }
- function drawMap(limits) {
- let map = [];
- let ylength = Math.abs(limits.ymin - limits.ymax - 1);
- let xlength = Math.abs(limits.xmin - limits.xmax - 1);
- let xline = [];
- for (i = 0; i < xlength; i++) {
- xline.push('0');
- }
- for (i = 0; i < ylength; i++) {
- map.push(xline.slice(0));
- }
- map[limits.ymax][limits.xmin] = 'W';
- for (i = 0; i < map.length; i++) {
- console.log(map[i].join(''))
- }
- return map;
- }
- fsPromises.readFile('/Users/Elture/Desktop/projects/puzzles/puzzle4input.txt', 'utf-8')
- .then((string) => {
- let firstCable = string.split('\r\n')[0].split(',');
- let secondCable = string.split('\r\n')[1].split(',');
- let limitsFirst = getLimits(firstCable);
- let limitsSecond = getLimits(secondCable);
- let fullLimits = combineLimits(limitsFirst, limitsSecond);
- drawMap(fullLimits)
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement