Day one solutions
This commit is contained in:
parent
ae6974a643
commit
1aa40a052b
102
01/code.js
Normal file
102
01/code.js
Normal file
@ -0,0 +1,102 @@
|
||||
const fs = require('fs');
|
||||
|
||||
// const inputArray = fs.readFileSync('sample.txt').toString().split("\n");
|
||||
let inputArray = fs.readFileSync('input.txt').toString().split("\n");
|
||||
let allInts = [];
|
||||
let sum = 0;
|
||||
|
||||
// Part One
|
||||
|
||||
for (i in inputArray) {
|
||||
const forwards = inputArray[i];
|
||||
const backwards = inputArray[i].split("").reverse().join("");
|
||||
let firstInt;
|
||||
let lastInt;
|
||||
for (j in forwards) {
|
||||
if (parseInt(forwards[j])) {
|
||||
firstInt = parseInt(forwards[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (j in backwards) {
|
||||
if (parseInt(backwards[j])) {
|
||||
lastInt = parseInt(backwards[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
allInts.push(parseInt(`${firstInt}${lastInt}`));
|
||||
}
|
||||
|
||||
sum = allInts.reduce((a, b) => a + b, 0);
|
||||
|
||||
console.log(sum);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
// inputArray = fs.readFileSync('sample2.txt').toString().split("\n");
|
||||
allInts = [];
|
||||
|
||||
for (i in inputArray) {
|
||||
let forwards = inputArray[i];
|
||||
let needsParsed = true;
|
||||
while (needsParsed) {
|
||||
for (j in forwards) {
|
||||
if (forwards[j] === "o" && forwards.substring(j,parseInt(parseInt(j)+parseInt(3))) === "one") {
|
||||
forwards = forwards.substring(0, j) + "1" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "t" && forwards.substring(j,parseInt(parseInt(j)+parseInt(3))) === "two") {
|
||||
forwards = forwards.substring(0, j) + "2" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "t" && forwards.substring(j,parseInt(parseInt(j)+parseInt(5))) === "three") {
|
||||
forwards = forwards.substring(0, j) + "3" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "f" && forwards.substring(j,parseInt(parseInt(j)+parseInt(4))) === "four") {
|
||||
forwards = forwards.substring(0, j) + "4" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "f" && forwards.substring(j,parseInt(parseInt(j)+parseInt(4))) === "five") {
|
||||
forwards = forwards.substring(0, j) + "5" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "s" && forwards.substring(j,parseInt(parseInt(j)+parseInt(3))) === "six") {
|
||||
forwards = forwards.substring(0, j) + "6" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "s" && forwards.substring(j,parseInt(parseInt(j)+parseInt(5))) === "seven") {
|
||||
forwards = forwards.substring(0, j) + "7" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "e" && forwards.substring(j,parseInt(parseInt(j)+parseInt(5))) === "eight") {
|
||||
forwards = forwards.substring(0, j) + "8" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (forwards[j] === "n" && forwards.substring(j,parseInt(parseInt(j)+parseInt(4))) === "nine") {
|
||||
forwards = forwards.substring(0, j) + "9" + forwards.substring(parseInt(parseInt(j)+parseInt(1)));
|
||||
break;
|
||||
} else if (parseInt(j) === forwards.length - 1) {
|
||||
needsParsed = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
backwards = forwards.split("").reverse().join("");
|
||||
|
||||
let firstInt;
|
||||
let lastInt;
|
||||
for (j in forwards) {
|
||||
if (parseInt(forwards[j])) {
|
||||
firstInt = parseInt(forwards[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (j in backwards) {
|
||||
if (parseInt(backwards[j])) {
|
||||
lastInt = parseInt(backwards[j]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
allInts.push(parseInt(`${firstInt}${lastInt}`));
|
||||
}
|
||||
|
||||
sum = allInts.reduce((a, b) => a + b, 0);
|
||||
|
||||
console.log(sum);
|
||||
|
||||
// functions
|
||||
|
1000
01/input.txt
Normal file
1000
01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
4
01/sample.txt
Normal file
4
01/sample.txt
Normal file
@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
7
01/sample2.txt
Normal file
7
01/sample2.txt
Normal file
@ -0,0 +1,7 @@
|
||||
two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen
|
Loading…
Reference in New Issue
Block a user