-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a820269
commit e45d7cb
Showing
1 changed file
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,74 @@ | ||
let data = []; | ||
//fetch("input.txt") | ||
fetch("input1_test.txt") | ||
let answers = []; | ||
let answer2 = 100000000000000; | ||
fetch("input.txt") | ||
//fetch("input1_test.txt") | ||
.then((r) => r.text()) | ||
.then((d) => { | ||
//console.log(d); | ||
d = d.replaceAll("\r", ""); | ||
d = d.replaceAll("\r", ""); | ||
data = d.split("\n\n").map((e) => { | ||
return e = e.split(":")[1]; | ||
return (e = e.split(":")[1]); | ||
}); | ||
for (let i = 0; i < data.length; i++) { | ||
data[i] = data[i].replace("\n", " ").trim().split("\n").map((e) => { | ||
return e.split(" ").map((e) => { return Number(e) }) | ||
}); | ||
data[i] = data[i] | ||
.replace("\n", " ") | ||
.trim() | ||
.split("\n") | ||
.map((e) => { | ||
return e.split(" ").map((e) => { | ||
return Number(e); | ||
}); | ||
}); | ||
} | ||
console.log(data); | ||
// | ||
// part 1 | ||
// | ||
for (let i = 0; i < data[0].length; i++) { | ||
for (let j = 0; j < data[0][i].length; j++) { | ||
//console.log("seed:"+data[0][i][j]); | ||
answers.push(procesSeed(data[0][i][j])); | ||
// | ||
} | ||
} | ||
answers.sort((a, b) => { | ||
return a - b; | ||
}); | ||
console.log("answer1:" + answers[0]); | ||
// | ||
// part 2 | ||
// | ||
for (let i = 0; i < data[0].length; i++) { | ||
for (let j = 0; j < data[0][i].length; j += 2) { | ||
// | ||
console.log(j); | ||
// // | ||
for (let k = 0; k < data[0][i][j + 1]; k++) { | ||
if (procesSeed(data[0][i][j] + k)<answer2) | ||
{ | ||
answer2 = procesSeed(data[0][i][j] + k); | ||
} | ||
} | ||
} | ||
} | ||
console.log("answer2:" + answer2); | ||
}); | ||
|
||
function procesSeed(seed) { | ||
for (let k = 1; k < 8; k++) { | ||
//all stages | ||
let temp = seed; | ||
for ( | ||
let l = 0; | ||
l < data[k].length; | ||
l++ //all values in a stage | ||
) { | ||
if (temp >= data[k][l][1] && temp < data[k][l][1] + data[k][l][2]) { | ||
seed = data[k][l][0] - (data[k][l][1] - seed); | ||
break; | ||
} | ||
} | ||
} | ||
return seed; | ||
} |