-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4-giant-squid.ts
168 lines (151 loc) · 4.38 KB
/
4-giant-squid.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import axios from "axios";
const getData = async () =>
await axios.get("https://adventofcode.com/2021/day/4/input", {
headers: {
Cookie:
"session=53616c7465645f5fcd01739818d6d32714847f740139d63198a1338a6890b2efde467b312dc5da5164680836385cb744",
},
});
// list of draws
// list of boards
// board is an interface
interface BingoNumber {
number: number;
marked: boolean;
}
const height = 5;
const width = 5;
class Board {
numbers: BingoNumber[][];
won: boolean;
id: number;
constructor(boardNumbers: number[], id: number) {
this.numbers = [];
let index = 0;
for (let i = 0; i < height; i++) {
this.numbers[i] = [];
for (let j = 0; j < width; j++) {
this.numbers[i][j] = { number: boardNumbers[index], marked: false };
index++;
}
}
this.id = id;
this.won = false;
}
public markNumber(toMark: number) {
for (let numberLine of this.numbers) {
for (let number of numberLine) {
if (number.number === toMark) {
number.marked = true;
}
}
}
}
public printPretty() {
let string = "";
for (let line of this.numbers) {
line.forEach((number) => (string = string + number.number + " "));
string = string + "\n";
}
return string;
}
public checkWon(): [Array<BingoNumber>, Array<BingoNumber>] {
const numbers = this.numbers;
// diagonal is no winner
// check rows
let wonLines = [];
let wonColumns = [];
for (let lineIndex = 0; lineIndex < numbers[0].length; lineIndex++) {
if (numbers[lineIndex].every((number) => number.marked === true)) {
wonLines.push([...numbers[lineIndex]]);
this.won = true;
}
}
// check columns
// assuming all columns are equally long
for (let columnIndex = 0; columnIndex < numbers.length; columnIndex++) {
let column = [];
for (let rowIndex = 0; rowIndex < 5; rowIndex++) {
column.push(numbers[rowIndex][columnIndex]);
}
if (column.every((number) => number.marked === true)) {
wonColumns.push([...column]);
this.won = true;
}
}
return [wonLines, wonColumns];
}
public getUnmarkedNumbers() {
return this.numbers
.flatMap((numberLines) => numberLines)
.filter((number: BingoNumber) => !number.marked);
}
}
const markDrawnNumberOnAllBoards = (draw: number, boards: Board[]) => {
for (let board of boards) {
board.markNumber(draw);
}
};
// return won boards if one has won, otherwise false
const checkBoardsWon = (boards: Board[]) => {
let wonBoards: Board[] = [];
for (let board of boards) {
let result = board.checkWon();
if (result[0].length > 0 || result[1].length > 0) {
wonBoards.push(board);
}
}
return wonBoards;
};
(async () => {
const res = await getData();
const input = res.data;
const cleaned = input.split("\n\n").map((x: string) => {
return x
.replace(/[\n ,]+/g, " ")
.trim()
.split(" ");
});
const draws = cleaned[0].map((y) => parseInt(y));
let boards = cleaned
.slice(1)
.map((board) => board.map((numberString) => parseInt(numberString)));
const boardList = boards.map((board, i) => new Board(board, i));
let notWonBoards = [];
let lastBoardId;
let counter = 0;
for (let draw of draws) {
markDrawnNumberOnAllBoards(draw, boardList);
let result = checkBoardsWon(boardList);
if (result.length === 1) {
console.log("result: ");
let wonBoard = result[0] as Board;
let sum = wonBoard
.getUnmarkedNumbers()
.map((number) => number.number)
.reduce((acc, curr) => acc + curr);
console.log("Final score: " + sum * draw);
}
notWonBoards = boardList.filter((board) => board.won === false);
if (notWonBoards.length === 1) {
console.log("last board");
console.log(notWonBoards[0]);
lastBoardId = notWonBoards[0].id;
console.log(
notWonBoards[0].numbers.map((numberLine) =>
numberLine.map((number) => number.marked)
)
);
}
if (notWonBoards.length === 0 && lastBoardId) {
let wonLastBoard = boardList.find((board) => board.id === lastBoardId);
let sum = wonLastBoard
.getUnmarkedNumbers()
.map((number) => number.number)
.reduce((acc, curr) => acc + curr);
console.log("Final score last board: " + sum * draw);
break;
}
counter++;
}
})();