-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (133 loc) · 3.46 KB
/
index.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const fs = require("fs");
const input = fs.readFileSync("input.txt", "utf8").split("\n");
// Make this immutable?
const boundaries = input.shift().split(" ");
const boundaryX = boundaries[0];
const boundaryY = boundaries[1];
const scent = [];
const makeBotObject = botString => {
const botArray = botString.split(" ");
return {
x: Number(botArray[0]),
y: Number(botArray[1]),
orientation: botArray[2],
status: ""
};
};
const commandBot = (bot, commands) => {
const commandBotReducer = (bot, command) => {
// lost bots don't move, this is a no-op
if (bot.status === "LOST") {
return bot;
}
// Don't move forward if position is in scent, also a no-op
const sniffed = scent.filter(
sniff =>
sniff.x === bot.x &&
sniff.y === bot.y &&
sniff.orientation === bot.orientation
);
if (sniffed.length && command === "F") {
return bot;
}
// Orientation
switch (command) {
case "L":
switch (bot.orientation) {
case "N":
bot.orientation = "W";
break;
case "E":
bot.orientation = "N";
break;
case "S":
bot.orientation = "E";
break;
case "W":
bot.orientation = "S";
break;
}
break;
case "R":
switch (bot.orientation) {
case "N":
bot.orientation = "E";
break;
case "E":
bot.orientation = "S";
break;
case "S":
bot.orientation = "W";
break;
case "W":
bot.orientation = "N";
break;
}
break;
// Movement
case "F":
switch (bot.orientation) {
case "N":
const north = bot.y + 1;
if (north > boundaryY) {
bot.status = "LOST";
scent.push(bot);
} else {
bot.y = north;
}
break;
case "E":
const east = bot.x + 1;
if (east > boundaryX) {
bot.status = "LOST";
scent.push(bot);
} else {
bot.x = east;
}
break;
case "S":
const south = bot.y - 1;
if (south < 0) {
bot.status = "LOST";
scent.push(bot);
} else {
bot.y = south;
}
break;
case "W":
const west = bot.x - 1;
if (west < 0) {
bot.status = "LOST";
scent.push(bot);
} else {
bot.x = west;
}
break;
}
break;
}
return bot;
};
const finalBot = commands.reduce(commandBotReducer, bot);
return `${finalBot.x} ${finalBot.y} ${finalBot.orientation} ${finalBot.status}\n`;
};
const parse = input => {
let bot = "";
const inputReducer = (accumalator, current) => {
switch (current.split(" ").length) {
case 1:
const botObject = makeBotObject(bot);
const commands = current.split("");
return (accumalator += commandBot(botObject, commands));
case 3:
bot = current;
return accumalator;
}
};
return input.reduce(inputReducer, "");
};
const output = parse(input);
fs.writeFileSync("output.txt", output);
console.log("Complete!");
console.log("See output.txt for results");
module.exports = { makeBotObject, commandBot };