-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday03.js
57 lines (42 loc) · 1.31 KB
/
day03.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
53
54
55
56
57
const fs = require('node:fs');
const entry = fs.readFileSync('./entries/day03.txt', { encoding: 'utf-8' });
const TREE = '#';
const START_POSITION = { x: 1, y: 1 };
const formattedEntry = entry.split('\n');
const MAX_COL_LENGTH = formattedEntry[0].length;
const computeSlope = (startPosition, pattern) => {
const startParams = {
position: startPosition,
path: [],
};
return formattedEntry.reduce((acc, curr, i) => {
const { path, position } = acc;
if (position.y - 1 !== i) return acc;
return {
path: [...path, curr[(position.x - 1) % MAX_COL_LENGTH]],
position: {
x: position.x + pattern.right,
y: position.y + pattern.down,
},
};
}, startParams).path;
};
const prodValues = arr => arr.reduce((acc, curr) => acc * curr);
const countNumberOfTrees = path => path.filter(c => c === TREE).length;
// STEP 1
const patternStep1 = {
right: 3,
down: 1,
};
const pathStep1 = computeSlope(START_POSITION, patternStep1);
console.log(countNumberOfTrees(pathStep1));
// STEP 2
const patternsStep2 = [
{ right: 1, down: 1 },
patternStep1,
{ right: 5, down: 1 },
{ right: 7, down: 1 },
{ right: 1, down: 2 },
];
const pathsStep2 = patternsStep2.map(pattern => computeSlope(START_POSITION, pattern));
console.log(prodValues(pathsStep2.map(countNumberOfTrees)));