Created
January 3, 2018 03:27
-
-
Save devNoiseConsulting/4c522604f8ec6b7f8b2cfaffade2c7ae to your computer and use it in GitHub Desktop.
A Series of Tubes - Advent of Code - 20171219
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const path = require('./path'); | |
| let changeDirection = function(path, x, y, dX, dY) { | |
| let newDirections = [[dY, dX], [-1 * dY, -1 * dX]]; | |
| let direction = -1; | |
| newDirections.forEach((d, i) => { | |
| let location = path[x + d[0]][y + d[1]]; | |
| if (location !== ' ' && location !== undefined) { | |
| direction = i; | |
| } | |
| }); | |
| return newDirections[direction]; | |
| }; | |
| let followPath = function(path) { | |
| let x = 1; | |
| let y = path[x].indexOf('|'); | |
| let dX = 1; | |
| let dY = 0; | |
| let letters = []; | |
| let steps = 1; | |
| while (dX || dY) { | |
| x += dX; | |
| y += dY; | |
| let location = path[x][y]; | |
| if (location == ' ') { | |
| [dX, dY] = [false, false]; | |
| continue; | |
| } | |
| if (location.match(/[A-Z]/)) { | |
| letters.push(location); | |
| } | |
| if (location == '+') { | |
| let direction = changeDirection(path, x, y, dX, dY); | |
| if (direction) { | |
| [dX, dY] = direction; | |
| } else { | |
| [dX, dY] = [false, false]; | |
| } | |
| } | |
| steps++; | |
| } | |
| return [letters.join(''), steps]; | |
| }; | |
| let result; | |
| result = followPath(path); | |
| console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment