Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ab872f5

Browse files
committed
day 3 part 2
1 parent f4e09f7 commit ab872f5

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

day3/program.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,33 @@ let items;
33

44
fs.readFile('./input.txt', 'utf-8', (e, data) => {
55
items = data.split("\n");
6-
const result = progress(0, 0, 0);
7-
console.log('result!', result);
6+
const resultPart1 = progress(0, 0, 0, [3,1]);
7+
const resultPart2 = checkPart2();
8+
9+
console.log('result!', resultPart1, resultPart2);
810
});
911

10-
function progress(trees, currentRow, currentColumn) {
12+
function checkPart2() {
13+
const oneone = progress(0, 0, 0, [1,1]);
14+
const threeone = progress(0, 0, 0, [3,1]);
15+
const fiveone = progress(0, 0, 0, [5,1]);
16+
const sevenone = progress(0, 0, 0, [7,1]);
17+
const onetwo = progress(0, 0, 0, [1,2]);
18+
return oneone*threeone*fiveone*sevenone*onetwo;
19+
}
20+
21+
function progress(trees, currentRow, currentColumn, nextStep) {
1122
const block = items[currentRow].substr(currentColumn, 1);
1223
if (block === '#') trees++;
1324

14-
currentRow++;
25+
currentRow += nextStep[1];
1526
if (!items[currentRow]) return trees;
1627

17-
currentColumn +=3;
28+
currentColumn += nextStep[0];
1829

1930
if (currentColumn > items[currentRow].length - 1) {
2031
currentColumn -= items[currentRow].length;
2132
}
2233

23-
return progress(trees, currentRow, currentColumn);
34+
return progress(trees, currentRow, currentColumn, nextStep);
2435
}

0 commit comments

Comments
 (0)