import * as fs from 'fs'; class Line { constructor(line) { this.x = 0; for(let i=0; i= 0 && y < this.height) { const line = this.lines[y]; if (x >= line.x && x < line.x + line.data.length) { return true; } } return false; } step(d) { for(; d>0; --d) { let line; switch(this.direction) { case RIGHT: line = this.lines[this.y]; if (this.isIn(this.x+1, this.y)) { if (line.data[this.x+1-line.x] === '.') { ++this.x; } } else { if (line.data[0] === '.') { this.x = line.x; } } break; case LEFT: line = this.lines[this.y]; if (this.isIn(this.x-1, this.y)) { if (line.data[this.x-1-line.x] === '.') { --this.x; } } else { if (line.data[line.data.length - 1] === '.') { this.x = line.x + line.data.length - 1; } } break; case DOWN: if (this.isIn(this.x, this.y+1)) { line = this.lines[this.y+1]; if (line.data[this.x-line.x] === '.') { ++this.y; } } else { let y = this.y; while (this.isIn(this.x, y - 1)) { --y; } line = this.lines[y]; if (line.data[this.x - line.x] === '.') { this.y = y; } } break; case UP: if (this.isIn(this.x, this.y-1)) { line = this.lines[this.y-1]; if (line.data[this.x-line.x] === '.') { --this.y; } } else { let y = this.y; while (this.isIn(this.x, y + 1)) { ++y; } line = this.lines[y]; if (line.data[this.x - line.x] === '.') { this.y = y; } } break; default: throw "unreachable"; } } } walk() { let d = 0; for (let i=0; i new Line(line)), lines[lines.length - 2] ); } let example = load('example'); let input = load('input'); function solve(input) { return input.walk(); } const exampleOutput = solve(example); if (exampleOutput !== 6032) { console.log('Example failed with ' + exampleOutput); process.exit(1); // eslint-disable-line } console.log(solve(input));