initial upload
This commit is contained in:
10
2024/04/example.txt
Normal file
10
2024/04/example.txt
Normal file
@ -0,0 +1,10 @@
|
||||
MMMSXXMASM
|
||||
MSAMXMSMSA
|
||||
AMXSXMAAMM
|
||||
MSAMASMSMX
|
||||
XMASAMXAMM
|
||||
XXAMMXXAMA
|
||||
SMSMSASXSS
|
||||
SAXAMASAAA
|
||||
MAMMMXMMMM
|
||||
MXMXAXMASX
|
51
2024/04/index.js
Normal file
51
2024/04/index.js
Normal file
@ -0,0 +1,51 @@
|
||||
import fs from 'node:fs'
|
||||
fs.readFile('example.txt', ({ encoding: "utf-8" }), (err, data) => {
|
||||
const letters = ["X", "M", "A", "S"]
|
||||
|
||||
const rows = [...data.split('\n')]
|
||||
|
||||
// directions, straight first: east,south,west,north,southeast,southwest,northwest,northeast
|
||||
const dirs = [[1, 0], [0, 1], [-1, 0], [0, -1], [1, 1], [-1, 1], [-1, -1], [-1, 1]]
|
||||
|
||||
let found_words = 0
|
||||
let found_letters = []
|
||||
const width = rows[0].length
|
||||
const height = rows.length
|
||||
// letters.forEach(letter => {
|
||||
// for (i = 0; i < rows.length; i++) {
|
||||
|
||||
|
||||
// }
|
||||
// })
|
||||
for (let letter = 0; letter < letters.length; letter++) {
|
||||
let letter_positions = []
|
||||
for (let x = 1; x <= width; x++) {
|
||||
for (let y = 1; y <= height; y++) {
|
||||
if (rows[y - 1][x - 1] === letters[letter]) {
|
||||
letter_positions.push([x, y])
|
||||
|
||||
// check if there is a next letter
|
||||
dirs.forEach(direction => {
|
||||
let [new_x, new_y] = direction
|
||||
if (x + new_x in [...rows[y - 1]] && y + new_y in [...rows.keys()] && letter + 1 in [...letters.keys()]) {
|
||||
//console.log(`I'm at ${x},${y} with ${letters[letter]}. Looking for letter at ${x + new_x},${y + new_y}`)
|
||||
if (letters[letter + 1] === rows[y - 1 + new_y][x - 1 + new_x]) {
|
||||
console.log(`I am at ${x},${y} with $ ${letters[letter]}. Found ${letters[letter + 1]}.`)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
found_letters[letter] = letter_positions
|
||||
}
|
||||
|
||||
console.log(found_letters)
|
||||
console.log(`width: ${width}`)
|
||||
console.log(`height: ${height}`)
|
||||
})
|
||||
|
||||
function findFirstLetter(letter) {
|
||||
const [row, col] = Array
|
||||
return [row, col]
|
||||
}
|
0
2024/04/input.txt
Normal file
0
2024/04/input.txt
Normal file
Reference in New Issue
Block a user