-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.ts
139 lines (121 loc) · 3.46 KB
/
day06.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
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
import { readFileSync } from "fs";
class LightGrid {
h: number;
w: number;
state: number[][];
constructor(height: number, width: number) {
this.h = height;
this.w = width;
this.state = [...Array(height)].map(e => Array(width).fill(0));
}
apply1(instruction: Instruction): void {
for(let i = instruction.region.x0; i <= instruction.region.x1; i++) {
for(let j = instruction.region.y0; j <= instruction.region.y1; j++) {
switch (instruction.action) {
case Action.On:
this.state[i][j] = 1;
break;
case Action.Off:
this.state[i][j] = 0;
break;
case Action.Toggle:
const current = this.state[i][j]
if (current === 1) {
this.state[i][j] = 0
} else {
this.state[i][j] = 1
}
}
}
}
// console.log(`Applied ${instruction.action} to region (${instruction.region.x0},${instruction.region.y0}) (${instruction.region.x1},${instruction.region.y1}). ${this.lightsLit()} lights lit.`)
}
apply2(instruction: Instruction): void {
for(let i = instruction.region.x0; i <= instruction.region.x1; i++) {
for(let j = instruction.region.y0; j <= instruction.region.y1; j++) {
switch (instruction.action) {
case Action.On:
this.state[i][j] += 1;
break;
case Action.Off:
if (this.state[i][j] > 0) {
this.state[i][j] -= 1;
}
break;
case Action.Toggle:
this.state[i][j] += 2;
}
}
}
}
lightsLit(): number {
return sumArray(this.state.map(sumArray));
}
}
function sumArray(array: number[]): number {
return array.reduce((a, b) => a + b, 0);
}
enum Action {
Toggle = "toggle",
On = "on",
Off = "off",
};
class Region {
readonly x0: number = 0;
readonly y0: number = 0;
readonly x1: number = 0;
readonly y1: number = 0;
constructor(input: string) {
const matches = input.match(/[0-9]+,[0-9]+/g);
if (matches?.length !== 2) {
console.error("Failed to parse region.");
return;
}
const topLeft = matches[0].split(',').map(Number);
const bottomRight = matches[1].split(',').map(Number);
this.x0 = Math.min(topLeft[0], bottomRight[0]);
this.x1 = Math.max(topLeft[0], bottomRight[0]);
this.y0 = Math.min(topLeft[1], bottomRight[1]);
this.y1 = Math.max(topLeft[1], bottomRight[1]);
}
}
class Instruction {
readonly action: Action;
readonly region: Region;
constructor(input: string) {
this.action = parseAction(input);
this.region = new Region(input);
}
}
function parseAction(input: string): Action {
// ? is called Optional chaining
switch (input.match(/on|off|toggle/g)?.pop()) {
case "on":
return Action.On;
case "off":
return Action.Off;
case "toggle":
default:
return Action.Toggle;
}
}
function readInput(input: string): string[] {
try {
const data = readFileSync(input, "utf8");
return data.split("\n");
} catch (err) {
console.log(err);
return [""];
}
}
export function day06(input: string): void {
const insturctions = readInput(input).map(i => new Instruction(i));
let grid1 = new LightGrid(1000, 1000);
let grid2 = new LightGrid(1000, 1000);
insturctions.forEach(i => {
grid1.apply1(i);
grid2.apply2(i);
});
console.log("Part 1: " + grid1.lightsLit());
console.log("Part 2: " + grid2.lightsLit());
}