-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday4pt1.ts
88 lines (64 loc) · 1.68 KB
/
day4pt1.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
export const input = await Deno.readTextFile('./day4input.txt')
// const input = `MMMSXXMASM
// MSAMXMSMSA
// AMXSXMAAMM
// MSAMASMSMX
// XMASAMXAMM
// XXAMMXXAMA
// SMSMSASXSS
// SAXAMASAAA
// MAMMMXMMMM
// MXMXAXMASX
// `
// console.log(input)
const pattern= /\n/g
// const rowsCount = input.match(pattern).length
const rowItems = input.split(pattern)
const rowsCount = rowItems.length
console.log(rowsCount)
const colsCount = rowItems[0].length
console.log(colsCount)
let xmasMatrix = rowItems
.map(rowItem => rowItem.split(''))
let totalSum = 0
xmasMatrix
.forEach((rowItem, rowIdx) => {
console.log("Row Item List:", rowItem)
rowItem.forEach((cellItem, colIdx) => {
if (cellItem === 'X') {
const directions = getDirs()
let surroundWords = []
for (let dir in directions) {
// console.log(dir, directions[dir])
let wordChars = []
let xC = rowIdx
let yC = colIdx
for (let i = 1; i<=3; i++){
xC += directions[dir][0]
yC += directions[dir][1]
if (xC < 0 || xC > colsCount-1 || yC < 0 || yC > colsCount-1 ) continue
wordChars.push(xmasMatrix[xC][yC])
}
if (!wordChars.length) continue
surroundWords.push(wordChars)
}
console.log(cellItem, xmasMatrix[rowIdx][colIdx])
let matchedWords = surroundWords.map(_ => _.join('')).filter(_ => _ === 'MAS')
console.log(surroundWords, matchedWords, matchedWords.length)
totalSum += matchedWords.length
}
})
})
console.log(totalSum)
function getDirs () {
return {
up: [ -1, 0 ],
down: [ 1, 0 ],
left: [ 0, -1 ],
right: [ 0, 1 ],
upleft: [ -1, -1 ],
upright: [ -1, 1 ],
downleft: [ 1, -1 ],
downright: [ 1, 1 ]
}
}