-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm.ts
129 lines (105 loc) · 3.47 KB
/
algorithm.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
import { readInputLines, writeOutput, readInputAsGrid, readInputAsNumbers } from "~/utils/io";
// DAY: 4
// PUZZLE: 1
// DATE OF COMPLETION: 2024-12-05
const INPUT_PATH = `./tasks/day04/puzzle1/INPUT`;
const OUTPUT_PATH = `./tasks/day04/puzzle1/OUTPUT`;
//This word search allows words to be:
// - horizontal,
// - vertical,
// - diagonal,
// - written backwards,
// - or even overlapping other words.
(await async function main() {
const lines = await readInputLines(INPUT_PATH)
const grid = lines.map((line) => line.split(""))
const addPaddingToGrid = (grid: string[][], padding: number) => {
const originalGridHeight = grid.length
const originalGridWidth = grid[0].length
const blankRow = new Array(originalGridWidth + 2 * padding).fill(".")
// add padding to sides of the original grid
for (let i = 0; i < grid.length; i++) {
for (let p = 0; p < padding; p++) {
grid[i].unshift(".")
grid[i].push(".")
}
}
// add blank rows to the top and bottom
for (let i = 0; i < padding; i++) {
grid.unshift(blankRow)
grid.push(blankRow)
}
return grid
}
const paddedGrid = addPaddingToGrid(grid, 4)
const word = "XMAS"
const vectors = [
[
[ // horizontal
[0, 0],
[0, 1],
[0, 2],
[0, 3]
],
[ // vertical
[0, 0],
[1, 0],
[2, 0],
[3, 0]
],
[ // diagonal
[0, 0],
[1, 1],
[2, 2],
[3, 3]
]
]
]
const countWord = (word: string): number => {
let count: number = 0;
for (let i = 0; i < paddedGrid.length; i++) {
for (let j = 0; j < paddedGrid.length; j++) {
// horizontal
if (
paddedGrid[i][j + 0] === word[0] &&
paddedGrid[i][j + 1] === word[1] &&
paddedGrid[i][j + 2] === word[2] &&
paddedGrid[i][j + 3] === word[3]
) {
count++
}
// vertical
if (
paddedGrid[i + 0][j] === word[0] &&
paddedGrid[i + 1][j] === word[1] &&
paddedGrid[i + 2][j] === word[2] &&
paddedGrid[i + 3][j] === word[3]
) {
count++
}
// DIAGONAL 1
if (
paddedGrid[i + 0][j + 0] === word[0] &&
paddedGrid[i + 1][j + 1] === word[1] &&
paddedGrid[i + 2][j + 2] === word[2] &&
paddedGrid[i + 3][j + 3] === word[3]
) {
count++
}
// DIAGONAL 2
if (
paddedGrid[i + 0][j + 0] === word[0] &&
paddedGrid[i + 1][j - 1] === word[1] &&
paddedGrid[i + 2][j - 2] === word[2] &&
paddedGrid[i + 3][j - 3] === word[3]
) {
count++
}
}
}
return count;
}
const foundCount = countWord("XMAS") + countWord("SAMX")
// SOLUTION HERE
await writeOutput(OUTPUT_PATH, foundCount.toString())
})();