-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04.js
52 lines (46 loc) · 1.54 KB
/
04.js
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
const fs = require('fs')
const args = process.argv.slice(2)
const data = fs.readFileSync(args[0], 'utf8')
const puzzle = data.split('\n')
let xmas = 0
let x_mas = 0
for (let y = 0; y < puzzle.length; y++) {
for (let x = 0; x < puzzle[0].length; x++) {
xmas += find(x, y)
x_mas += findx_mas(x, y)
}
}
console.log('Del 1:', xmas)
console.log('Del 2:', x_mas)
function find(sx, sy) {
const xmas = 'XMAS'.split('')
let numfound = 0
if (puzzle[sy][sx] != xmas[0]) return 0
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
let found = true
for (let i = 0; i < xmas.length && found; i++) {
let x = sx + dx * i
let y = sy + dy * i
found = puzzle[y] && puzzle[y][x] == xmas[i];
}
if (found) numfound++
}
}
return numfound
}
function findx_mas(sx,sy) {
if(puzzle[sy] && puzzle[sy][sx] != 'A') return 0
let found = 0
// Framåt
if(puzzle[sy - 1] && puzzle[sy - 1][sx - 1] == 'M' &&
puzzle[sy + 1] && puzzle[sy + 1][sx + 1] == 'S') found += 1
if(puzzle[sy + 1] && puzzle[sy + 1][sx - 1] == 'M' &&
puzzle[sy - 1] && puzzle[sy - 1][sx + 1] == 'S') found += 1
// Bakåt
if(puzzle[sy - 1] && puzzle[sy - 1][sx - 1] == 'S' &&
puzzle[sy + 1] && puzzle[sy + 1][sx + 1] == 'M') found += 1
if(puzzle[sy + 1] && puzzle[sy + 1][sx - 1] == 'S' &&
puzzle[sy - 1] && puzzle[sy - 1][sx + 1] == 'M') found += 1
return found > 1 ? 1 : 0
}