-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday4question1.ts
More file actions
121 lines (89 loc) · 2.75 KB
/
day4question1.ts
File metadata and controls
121 lines (89 loc) · 2.75 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
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[] = [];
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) {
this.columns.forEach((c) => c.markInput(input));
this.rows.forEach((r) => r.markInput(input));
}
public won() {
return (
this.rows.some((r) => r.test()) || this.columns.some((r) => r.test())
);
}
public addUnmarkedNumbers() {
let latestSum = 0;
for (let row of this.rows) {
latestSum += row.addUnmarkedValues();
}
return latestSum;
}
}
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 winningInput = 0;
for (let input of bingoInputs.split(",")) {
let numericalInput = Number(input);
bingoBoards.forEach((b) => b.markInput(numericalInput));
if (bingoBoards.some((b) => b.won())) {
winningInput = numericalInput;
break;
}
}
const winningBoard = bingoBoards.find((b) => b.won());
const sumOfUnmarkedNumbers = winningBoard.addUnmarkedNumbers();
console.log(sumOfUnmarkedNumbers * winningInput);