-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.ts
76 lines (69 loc) · 1.71 KB
/
day03.ts
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
import { readFileSync } from "fs";
interface Point {
x: number;
y: number;
}
function part1(directions: string): number {
let location: Point = { x: 0, y: 0 };
let visited: Set<string> = new Set();
// Using stringify to compare objects by value in Set.
visited.add(JSON.stringify({ x: 0, y: 0 }));
for (let i = 0; i < directions.length; i++) {
switch (directions[i]) {
case ">":
location.x += 1;
break;
case "<":
location.x -= 1;
break;
case "^":
location.y += 1;
break;
case "v":
location.y -= 1;
break;
}
visited.add(JSON.stringify({ x: location.x, y: location.y }));
}
return visited.size;
}
function part2(directions: string): number {
let location: Point[] = [
{ x: 0, y: 0 }, // santa
{ x: 0, y: 0 }, // roboSanta
];
let visited: Set<string> = new Set();
visited.add(JSON.stringify({ x: 0, y: 0 }));
for (let i = 0; i < directions.length; i++) {
switch (directions[i]) {
case ">":
location[i % 2].x += 1;
break;
case "<":
location[i % 2].x -= 1;
break;
case "^":
location[i % 2].y += 1;
break;
case "v":
location[i % 2].y -= 1;
break;
}
visited.add(JSON.stringify({ x: location[i % 2].x, y: location[i % 2].y }));
}
return visited.size;
}
function readInput(input: string): string {
try {
const data = readFileSync(input, "utf8");
return data;
} catch (err) {
console.log(err);
return "";
}
}
export function day03(input: string): void {
const directions = readInput(input);
console.log("Part 1: " + part1(directions));
console.log("Part 2: " + part2(directions));
}