-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.tsx
68 lines (59 loc) · 2.11 KB
/
Solution.tsx
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
import { Coordinate } from './Graph';
export enum Orientation {
NONE,
X_MINUS,
X_PLUS,
Y_MINUS,
Y_PLUS
}
export function orientationToRotation(o: Orientation): number {
switch (o) {
case Orientation.NONE: return 0;
case Orientation.X_MINUS: return Math.PI;
case Orientation.X_PLUS: return 0;
case Orientation.Y_MINUS: return -Math.PI / 2;
case Orientation.Y_PLUS: return Math.PI / 2;
}
};
function orientationFromString(s: string): Orientation {
switch (s) {
case "X_MINUS": return Orientation.X_MINUS;
case "X_PLUS": return Orientation.X_PLUS;
case "Y_MINUS": return Orientation.Y_MINUS;
case "Y_PLUS": return Orientation.Y_PLUS;
default: return Orientation.NONE;
}
}
export class Pose {
public position: Coordinate = new Coordinate(0, 0);
public orientation: Orientation = Orientation.NONE;
constructor(position: Coordinate = new Coordinate(0, 0), orientation: Orientation = Orientation.NONE) {
this.position = position;
this.orientation = orientation;
}
}
export type Config = Pose[];
export type Solution = Config[];
export function parseSolution(text: string): Solution {
const lines = text.trim().split("\n");
const solution: Solution = [];
for (const line of lines) {
const config: Config = [];
const pos_re = /(\((\d+),(\d+),?([XY]{1}_[A-Z]{4,5})?\),)/g;
while (true) {
const m = pos_re.exec(line);
if (m === null) break;
if (m === null || m.length !== 5) throw new Error("Invalid solution");
const x = Number(m[2]);
if (x < 0) throw new Error(`Invalid solution: position ${x} is negative`);
const y = Number(m[3]);
if (y < 0) throw new Error(`Invalid solution: position ${y} is negative`);
const o = orientationFromString(m[4]);
const pose = new Pose(new Coordinate(x, y), o);
config.push(pose);
}
if (config.length === 0) throw new Error("Invalid solution");
solution.push(config);
}
return solution;
}