-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4question2.ts
More file actions
155 lines (114 loc) · 3.58 KB
/
day4question2.ts
File metadata and controls
155 lines (114 loc) · 3.58 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
import { readFileSync } from "fs";
class TestableMember {
checked: boolean = false;
value: number;
constructor(value: number) {
this.value = value;
}
public test(input: number) {
this.checked = this.checked || input === this.value;
}
}
class TestableCollection {
private members: TestableMember[] = [];
public addMember(member: number) {
this.members.push(new TestableMember(member));
}
public markInput(input: number) {
this.members.forEach((m) => m.test(input));
}
public test(): boolean {
return this.members.every((m) => m.checked);
}
public addUnmarkedValues() {
return this.members.reduce<number>((latestReduction, member) => {
if (!member.checked) {
return latestReduction + member.value;
}
return latestReduction;
}, 0);
}
}
class BingoBoard {
private rows: TestableCollection[] = [];
private columns: TestableCollection[] = [];
private _isWinningBoard: boolean = false;
public get isWinningBoard(): boolean {
return this._isWinningBoard;
}
protected set isWinningBoard(value: boolean) {
this._isWinningBoard = value;
}
private _winningRound: number = -1;
public get winningRound(): number {
return this._winningRound;
}
protected set winningRound(value: number) {
this._winningRound = value;
}
private _winningInput: number = -1;
public get winningInput(): number {
return this._winningInput;
}
protected set winningInput(value: number) {
this._winningInput = value;
}
constructor(rows: string[]) {
const gridSize = rows.length;
for (let i = 0; i < gridSize; i++) {
this.rows.push(new TestableCollection());
this.columns.push(new TestableCollection());
}
rows.forEach((row, rowIndex) => {
row
.split(" ")
.filter((i) => i !== " " && i !== "")
.forEach((number, columnIndex) => {
this.rows[rowIndex].addMember(Number(number));
this.columns[columnIndex].addMember(Number(number));
}, this);
});
}
public markInput(input: number, round: number) {
this.columns.forEach((c) => c.markInput(input));
this.rows.forEach((r) => r.markInput(input));
if (
!this.isWinningBoard &&
(this.rows.some((r) => r.test()) || this.columns.some((r) => r.test()))
) {
this.isWinningBoard = true;
this.winningRound = round;
this.winningInput = input;
}
}
public addUnmarkedNumbers() {
let rollingSum = 0;
for (let row of this.rows) {
rollingSum += row.addUnmarkedValues();
}
return rollingSum;
}
}
let inputs: string[];
const rawData = readFileSync("./day4inputs.txt", "utf8");
inputs = rawData.split("\r\n");
const bingoInputs = inputs.shift();
inputs = inputs.filter((i) => i !== "");
const gridSize = 5;
const bingoBoards: BingoBoard[] = [];
for (let i = 0; i < inputs.length; i += gridSize) {
bingoBoards.push(new BingoBoard(inputs.slice(i, i + gridSize)));
}
let roundCounter = 0;
for (let input of bingoInputs.split(",")) {
bingoBoards.forEach((b) => b.markInput(Number(input), roundCounter));
if (bingoBoards.every((b) => b.isWinningBoard)) {
break;
}
roundCounter++;
}
const lastWinningBoard = bingoBoards
.filter((b) => b.isWinningBoard)
.sort((a, b) => a.winningRound - b.winningRound)[bingoBoards.length - 1];
const sumOfUnmarkedNumbers = lastWinningBoard.addUnmarkedNumbers();
console.log(sumOfUnmarkedNumbers * lastWinningBoard.winningInput);