-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis.ts
executable file
·253 lines (204 loc) · 7.09 KB
/
vis.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#!/usr/bin/env deno run --allow-write --allow-read --allow-net
// Inspired by Jari Komppa:
// https://twitter.com/Sol_HSA/status/1467033308177514496
import {
COLOR_BLUE_1,
COLOR_BLUE_2,
COLOR_BLUE_3,
COLOR_BLUE_4,
COLOR_GREEN_3,
COLOR_GREEN_4,
fontHeight,
Frame,
GIF,
mixColors,
writeText,
} from "../visualisation_utils/mod.ts";
import { BoardMarks, parseInput, sumUnmarkedFields } from "./main.ts";
console.log("Start...");
const gif = new GIF([]);
const { boards, draws } = parseInput(await Deno.readTextFile("input.txt"));
const OVERDRAW_FRAMES = 40;
const COLOR_BACKGROUND = COLOR_BLUE_1;
const COLOR_BOARD = COLOR_BLUE_2;
const COLOR_CELL = COLOR_BLUE_3;
const COLOR_CELL_DRAWN = COLOR_BLUE_4;
const COLOR_CELL_JUST_FOUND = COLOR_GREEN_4;
const COLOR_WIN_FOUND = COLOR_GREEN_3;
const WIN_SCORE_FRAME_GAP = 20;
const DECAY_PER_FRAME = 0.075;
const DECAY_PER_FRAME_OVERDRAW = 0.075;
const BOARDS_PER_ROW = 10;
const cellPad = 2;
const cellSize = 14;
const boardPad = 20;
const boardSize = (cellSize + cellPad) * boards[0].length + cellPad;
const boardOuterSize = boardSize + boardPad;
const width =
(BOARDS_PER_ROW < boards.length ? BOARDS_PER_ROW : boards.length) *
boardOuterSize + boardPad;
const height = Math.ceil(boards.length / BOARDS_PER_ROW) * boardOuterSize +
boardPad;
const allBoardMarks: BoardMarks[] = boards.map((board) =>
board.map((b) => b.map(() => false))
);
const boardWins: (number | undefined)[] = boards.map((_) => undefined);
let firstBoardToWin: number | undefined = undefined;
let lastBoardToWin: number | undefined = undefined;
const formatNumber = (number: number): string =>
number.toString().padStart(2, "0");
console.log("Drawing...");
for (
let drawIndex = 0;
drawIndex < draws.length + OVERDRAW_FRAMES;
drawIndex++
) {
const draw = draws[drawIndex];
const overdraw = Math.max(drawIndex - draws.length, 0);
const darkenOverdraw = Math.min(overdraw * DECAY_PER_FRAME_OVERDRAW, 1);
const applyCellColorOverdraw = (color: number) =>
mixColors(color, COLOR_CELL_DRAWN, darkenOverdraw);
const frame = new Frame(width, height);
frame.drawBox(1, 1, width, height, COLOR_BACKGROUND);
const drawOnTop: (() => void)[] = [];
for (let boardIndex = 0; boardIndex < boards.length; boardIndex++) {
const boardWin = boardWins[boardIndex];
const boardWonBefore = boardWin === undefined ? 0 : (drawIndex - boardWin);
const darken = Math.min(boardWonBefore * DECAY_PER_FRAME, 1);
const applyDarken = (color: number) =>
mixColors(color, COLOR_BACKGROUND, darken);
const board = boards[boardIndex];
const boardMarks = allBoardMarks[boardIndex];
const boardX = boardIndex % BOARDS_PER_ROW;
const boardY = Math.floor(boardIndex / BOARDS_PER_ROW);
const boardXOffset = boardX * boardOuterSize + boardPad;
const boardYOffset = boardY * boardOuterSize + boardPad;
frame.drawBox(
boardXOffset,
boardYOffset,
boardSize,
boardSize,
applyDarken(COLOR_BOARD),
);
for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board[row].length; col++) {
const cellXOffset = boardXOffset + col * (cellSize + cellPad) +
cellPad;
const cellYOffset = boardYOffset + row * (cellSize + cellPad) +
cellPad;
let cellColor = COLOR_CELL;
if (boardMarks[row][col]) {
cellColor = COLOR_CELL_DRAWN;
}
if (board[row][col] === draw && boardWin === undefined) {
boardMarks[row][col] = true;
cellColor = COLOR_CELL_JUST_FOUND;
}
frame.drawBox(
cellXOffset,
cellYOffset,
cellSize,
cellSize,
applyDarken(cellColor),
);
}
}
for (let row = 0; row < board.length; row++) {
const rowIsFilled = boardMarks[row].every((b) => b);
if (rowIsFilled) {
if (boardWin === undefined) {
boardWins[boardIndex] = drawIndex;
if (firstBoardToWin === undefined) firstBoardToWin = boardIndex;
lastBoardToWin = boardIndex;
}
const cellYOffset = boardYOffset + row * (cellSize + cellPad) +
cellPad;
frame.drawBox(
boardXOffset + cellPad,
cellYOffset,
boardSize - 2 * cellPad,
cellSize,
applyDarken(applyCellColorOverdraw(COLOR_WIN_FOUND)),
);
}
}
for (let col = 0; col < board[0].length; col++) {
const columnIsFilled = boardMarks.every((_, row) => boardMarks[row][col]);
if (columnIsFilled) {
if (boardWin === undefined) {
boardWins[boardIndex] = drawIndex;
if (firstBoardToWin === undefined) firstBoardToWin = boardIndex;
lastBoardToWin = boardIndex;
}
const cellXOffset = boardXOffset + col * (cellSize + cellPad) +
cellPad;
frame.drawBox(
cellXOffset,
boardYOffset + cellPad,
cellSize,
boardSize - 2 * cellPad,
applyDarken(applyCellColorOverdraw(COLOR_WIN_FOUND)),
);
}
}
const winDrawIndex = boardWins[boardIndex];
if (winDrawIndex !== undefined) {
const afterWin = drawIndex - winDrawIndex;
if (afterWin > WIN_SCORE_FRAME_GAP) {
const scoreFrames = afterWin - WIN_SCORE_FRAME_GAP;
const isTarget = firstBoardToWin === boardIndex ||
lastBoardToWin === boardIndex;
drawOnTop.push(() => {
const decayOffsetPerLine = 3;
const part = firstBoardToWin === boardIndex
? "P1"
: lastBoardToWin === boardIndex
? "P2"
: "";
const winDraw = draws[winDrawIndex];
const score = sumUnmarkedFields(board, boardMarks);
const lines = [
[part, COLOR_CELL_JUST_FOUND],
[`${winDrawIndex}`, COLOR_CELL],
[`${winDraw} * ${score}`, isTarget ? COLOR_CELL_DRAWN : COLOR_CELL],
[
`${winDraw * score}`,
isTarget ? COLOR_CELL_JUST_FOUND : COLOR_CELL_DRAWN,
],
] as const;
const yOffsetBase = boardYOffset +
Math.round((2 * cellSize + cellPad - fontHeight) / 2);
lines.forEach(([text, color], lineNumber) => {
const darken = (scoreFrames - lineNumber * decayOffsetPerLine) *
DECAY_PER_FRAME_OVERDRAW;
const darkenClamped = 1 - Math.max(Math.min(darken, 1), 0);
writeText(
frame,
text,
boardXOffset,
yOffsetBase + (cellSize + cellPad) * lineNumber,
mixColors(color, COLOR_BACKGROUND, darkenClamped),
);
});
});
}
}
}
drawOnTop.forEach((callback) => callback());
if (drawIndex < draws.length) {
const text = `${formatNumber(drawIndex)} ${formatNumber(draw)}`;
writeText(
frame,
text,
boardPad,
Math.round((boardPad - fontHeight) / 2),
COLOR_CELL_JUST_FOUND,
);
}
gif.push(frame);
}
console.log("Encoding...");
const bytes = await gif.encode();
console.log("Writing...");
Deno.writeFile("output.gif", bytes);
console.log("Done!");