-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine.js
96 lines (79 loc) · 2.33 KB
/
mine.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
class MineCart {
constructor (x, y, direction) {
this.x = x;
this.y = y;
this.dx = direction === '<' ? -1 : direction === '>' ? 1 : 0;
this.dy = direction === '^' ? -1 : direction === 'v' ? 1 : 0;
this._behavior = 0;
this._behaviors = [
'left',
'straight',
'right',
];
}
move (nextSection) {
this.x += this.dx;
this.y += this.dy;
if (/[/\\]/.test(nextSection)) {
/* eslint-disable indent */
const direction =
nextSection === '\\' && this.dx !== 0 ? 'right' :
nextSection === '/' && this.dx !== 0 ? 'left' :
nextSection === '\\' && this.dy !== 0 ? 'left' :
nextSection === '/' && this.dy !== 0 ? 'right' :
null;
/* eslint-enable indent */
this.turn(direction);
} else if (nextSection === '+') {
const direction = this._behaviors[this._behavior];
this._behavior = this._behavior + 1 === this._behaviors.length
? 0
: this._behavior + 1;
if (direction !== 'straight') {
this.turn(direction);
}
}
}
turn (direction) {
if (this.dx !== 0) {
this.dy = this.dx * (direction === 'right' ? 1 : -1);
this.dx = 0;
} else if (this.dy !== 0) {
this.dx = this.dy * (direction === 'right' ? -1 : 1);
this.dy = 0;
}
}
}
const mine = (input) => {
const width = Math.max(...input.split('\n').map((line) => line.length));
const tracks = input
.split('\n')
.filter((line) => line.trim())
.map((line) => line.padEnd(width, ' ').split(''));
const carts = [];
for (let y = 0; y < tracks.length; y++) {
for (let x = 0; x < tracks[y].length; x++) {
if (/[\^v<>]/.test(tracks[y][x])) {
const marker = tracks[y][x];
const cart = new MineCart(x, y, marker);
tracks[y][x] = /[<>]/.test(marker) ? '-' : '|';
carts.push(cart);
}
}
}
// eslint-disable-next-line no-constant-condition
while (true) {
const positions = [];
for (let i = 0; i < carts.length; i++) {
const cart = carts[i];
const nextSection = tracks[cart.y + cart.dy][cart.x + cart.dx];
cart.move(nextSection);
const cartPosition = `${cart.x},${cart.y}`;
if (positions.includes(cartPosition)) {
return cartPosition;
}
positions.push(cartPosition);
}
}
};
module.exports = mine;