Skip to content

Commit bf60582

Browse files
author
Seralius
committed
Solved Day 12 (Sadly didnt Save P1, so Only Commented out the P2 Adding) (Works though)
1 parent 1b5967e commit bf60582

File tree

2 files changed

+138
-2
lines changed

2 files changed

+138
-2
lines changed

Diff for: 12/Part1.js

+69-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,76 @@ const fs = require('fs')
33
var Rawinput = fs.readFileSync("input.txt").toString('utf-8')
44
input = Rawinput.split("\r\n");
55
function Run() {
6-
6+
var Caves = {};
7+
input.forEach(function (line) {
8+
var Points = line.split("-");
9+
var from = Points[0];
10+
var to = Points[1];
11+
Caves[from] = Caves[from] || [];
12+
Caves[from].push(to);
13+
Caves[to] = Caves[to] || [];
14+
if (!Caves[to].includes(from)) {
15+
Caves[to].push(from);
16+
}
17+
})
18+
var DonePaths = [];
19+
var AllDone = false;
20+
var Paths = GetNext(Caves, "start");
21+
Paths.forEach(function (path) {
22+
DonePaths = SolvePath(path, DonePaths, Caves);
23+
})
24+
console.log(DonePaths.length);
725
}
26+
27+
var DonePaths = [];
28+
function SolvePath(path, alreadyEnded, Caves) {
29+
if (path.endsWith("end") && !DonePaths.includes(path)) {
30+
DonePaths.push(path);
31+
}
32+
else {
33+
var next = GetNext(Caves, path);
34+
next.forEach(function (nextPath) {
35+
var Paths = SolvePath(nextPath, alreadyEnded, Caves);
36+
})
37+
}
38+
return DonePaths;
39+
}
40+
41+
function GetNext(Caves, CurrentPath) {
42+
var NextPaths = [];
43+
var smallCave = /[a-z](?=,|$)/g;
44+
var CurrentNode = CurrentPath.split(",").pop();
45+
Caves[CurrentNode].forEach(function (node) {
46+
var maxAmount = 1;
47+
if (CurrentPath != "start") {
48+
var FilteredPath = CurrentPath.replace("start", "")
49+
var smallAmount = FilteredPath.match(smallCave)
50+
if (smallAmount != null) {
51+
smallAmount.forEach(function (smalls) {
52+
var amount = FilteredPath.match(new RegExp(smalls, "g")).length
53+
maxAmount = amount < maxAmount ? maxAmount : amount;
54+
})
55+
}
56+
}
57+
if (node != "start") {
58+
if (smallCave.test(node)) {
59+
if (!CurrentPath.includes(node)) {
60+
NextPaths.push(CurrentPath + "," + node);
61+
}
62+
// else if (maxAmount < 2) {
63+
// NextPaths.push(CurrentPath + "," + node);
64+
// }
65+
}
66+
else {
67+
NextPaths.push(CurrentPath + "," + node);
68+
}
69+
}
70+
})
71+
return NextPaths;
72+
}
73+
74+
75+
876
module.exports = Run;
977
//Start timer
1078
var startTime = new Date().getTime();

Diff for: 12/Part2.js

+69-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,76 @@ const fs = require('fs')
33
var Rawinput = fs.readFileSync("input.txt").toString('utf-8')
44
input = Rawinput.split("\r\n");
55
function Run() {
6-
6+
var Caves = {};
7+
input.forEach(function (line) {
8+
var Points = line.split("-");
9+
var from = Points[0];
10+
var to = Points[1];
11+
Caves[from] = Caves[from] || [];
12+
Caves[from].push(to);
13+
Caves[to] = Caves[to] || [];
14+
if (!Caves[to].includes(from)) {
15+
Caves[to].push(from);
16+
}
17+
})
18+
var DonePaths = [];
19+
var AllDone = false;
20+
var Paths = GetNext(Caves, "start");
21+
Paths.forEach(function (path) {
22+
DonePaths = SolvePath(path, DonePaths, Caves);
23+
})
24+
console.log(DonePaths.length);
725
}
26+
27+
var DonePaths = [];
28+
function SolvePath(path, alreadyEnded, Caves) {
29+
if (path.endsWith("end") && !DonePaths.includes(path)) {
30+
DonePaths.push(path);
31+
}
32+
else {
33+
var next = GetNext(Caves, path);
34+
next.forEach(function (nextPath) {
35+
var Paths = SolvePath(nextPath, alreadyEnded, Caves);
36+
})
37+
}
38+
return DonePaths;
39+
}
40+
41+
function GetNext(Caves, CurrentPath) {
42+
var NextPaths = [];
43+
var smallCave = /[a-z](?=,|$)/g;
44+
var CurrentNode = CurrentPath.split(",").pop();
45+
Caves[CurrentNode].forEach(function (node) {
46+
var maxAmount = 1;
47+
if (CurrentPath != "start") {
48+
var FilteredPath = CurrentPath.replace("start", "")
49+
var smallAmount = FilteredPath.match(smallCave)
50+
if (smallAmount != null) {
51+
smallAmount.forEach(function (smalls) {
52+
var amount = FilteredPath.match(new RegExp(smalls, "g")).length
53+
maxAmount = amount < maxAmount ? maxAmount : amount;
54+
})
55+
}
56+
}
57+
if (node != "start") {
58+
if (smallCave.test(node)) {
59+
if (!CurrentPath.includes(node)) {
60+
NextPaths.push(CurrentPath + "," + node);
61+
}
62+
else if (maxAmount < 2) {
63+
NextPaths.push(CurrentPath + "," + node);
64+
}
65+
}
66+
else {
67+
NextPaths.push(CurrentPath + "," + node);
68+
}
69+
}
70+
})
71+
return NextPaths;
72+
}
73+
74+
75+
876
module.exports = Run;
977
//Start timer
1078
var startTime = new Date().getTime();

0 commit comments

Comments
 (0)