Skip to content

Commit 0198780

Browse files
author
Seralius
committed
Solved Day9
1 parent 870b29d commit 0198780

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

Diff for: 09/Part1.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,31 @@ const fs = require('fs')
22

33
var Rawinput = fs.readFileSync("input.txt").toString('utf-8')
44
input = Rawinput.split("\r\n");
5+
for (let i = 0; i < input.length; i++) {
6+
const element = input[i];
7+
input[i] = element.split("").map(Number);
8+
}
59
function Run() {
6-
10+
var Sum = 0;
11+
input.forEach((row,x) => {
12+
row.forEach((col,y) => {
13+
Sum += IsLowest(x,y)
14+
});
15+
});
16+
console.log(Sum);
17+
}
18+
function IsLowest(x,y) {
19+
var Input = input[x][y];
20+
var Adjacents = [
21+
x == 0 ? 99 : input[x-1][y],
22+
x == input.length - 1 ? 99 : input[x+1][y],
23+
y == 0 ? 99 : input[x][y-1],
24+
y == input[x].length -1 ? 99 : input[x][y+1]
25+
]
26+
if (Input < Adjacents.sort()[0]) {
27+
return 1 + Input;
28+
}
29+
return 0;
730
}
831
module.exports = Run;
932
//Start timer

Diff for: 09/Part2.js

+90-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,97 @@ const fs = require('fs')
22

33
var Rawinput = fs.readFileSync("input.txt").toString('utf-8')
44
input = Rawinput.split("\r\n");
5+
for (let i = 0; i < input.length; i++) {
6+
const element = input[i];
7+
input[i] = element.split("").map(Number);
8+
}
59
function Run() {
6-
10+
var Basins = [];
11+
input.forEach((row, x) => {
12+
row.forEach((col, y) => {
13+
if (IsLowest(x, y) != 0) {
14+
Basins.push(GetBasin(x, y));
15+
}
16+
});
17+
});
18+
Basins = Basins.sort(function(a, b){return b-a})
19+
console.log(Basins[0]* Basins[1] * Basins[2]);
20+
}
21+
function IsLowest(x, y) {
22+
var Input = input[x][y];
23+
var Adjacents = [
24+
x == 0 ? 99 : input[x - 1][y],
25+
x == input.length - 1 ? 99 : input[x + 1][y],
26+
y == 0 ? 99 : input[x][y - 1],
27+
y == input[x].length - 1 ? 99 : input[x][y + 1]
28+
]
29+
if (Input < Adjacents.sort()[0]) {
30+
return 1 + Input;
31+
}
32+
return 0;
33+
}
34+
function GetBasin(x, y) {
35+
var Points = [{ x: x, y: y }];
36+
var StartingPoint = input[x][y];
37+
var OnlyNines = false;
38+
while (!OnlyNines) {
39+
var OnlyNines = true;
40+
Points.forEach(point => {
41+
//Check Up
42+
if (point.y != 0 && input[point.x][point.y - 1] != 9) {
43+
var AlreadyIn = false
44+
Points.forEach(point2 => {
45+
if (point2.x == point.x && point2.y == point.y - 1) {
46+
AlreadyIn = true;
47+
}
48+
});
49+
if (!AlreadyIn) {
50+
OnlyNines = false;
51+
Points.push({ x: point.x, y: point.y - 1 });
52+
}
53+
}
54+
//Check Down
55+
if (point.y + 1 != input[0].length && input[point.x][point.y + 1] != 9 && !Points.includes({ x: point.x, y: point.y + 1 })) {
56+
var AlreadyIn = false
57+
Points.forEach(point2 => {
58+
if (point2.x == point.x && point2.y == point.y + 1) {
59+
AlreadyIn = true;
60+
}
61+
});
62+
if (!AlreadyIn) {
63+
OnlyNines = false;
64+
Points.push({ x: point.x, y: point.y + 1 });
65+
}
66+
}
67+
//Check Left
68+
if (point.x != 0 && input[point.x - 1][point.y] != 9 && !Points.includes({ x: point.x + 1, y: point.y })) {
69+
var AlreadyIn = false
70+
Points.forEach(point2 => {
71+
if (point2.x == point.x - 1 && point2.y == point.y) {
72+
AlreadyIn = true;
73+
}
74+
});
75+
if (!AlreadyIn) {
76+
OnlyNines = false;
77+
Points.push({ x: point.x - 1, y: point.y});
78+
}
79+
}
80+
//Check Right
81+
if (point.x + 1 != input.length && input[point.x + 1][point.y] != 9 && !Points.includes({ x: point.x + 1, y: point.y })) {
82+
var AlreadyIn = false
83+
Points.forEach(point2 => {
84+
if (point2.x == point.x + 1&& point2.y == point.y) {
85+
AlreadyIn = true;
86+
}
87+
});
88+
if (!AlreadyIn) {
89+
OnlyNines = false;
90+
Points.push({ x: point.x + 1, y: point.y});
91+
}
92+
}
93+
})
94+
}
95+
return Points.length;
796
}
897
module.exports = Run;
998
//Start timer

0 commit comments

Comments
 (0)