generated from eric/adventofcode2023
day three both parts
This commit is contained in:
165
03/code.js
165
03/code.js
@@ -1,26 +1,169 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
// const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
// const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
console.time("part1");
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
console.time("part1");
|
||||
let sum = 0;
|
||||
|
||||
for (const element of inputArray) {
|
||||
const length = element.length;
|
||||
let firstDigitLocation = 0;
|
||||
let firstDigit = 1;
|
||||
let lastDigit = 0;
|
||||
|
||||
for (let i = 0; i < length - 1; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > firstDigit) {
|
||||
firstDigit = digit;
|
||||
firstDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = firstDigitLocation + 1; i < length; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > lastDigit) {
|
||||
lastDigit = digit;
|
||||
}
|
||||
}
|
||||
const joltage = (firstDigit * 10) + lastDigit;
|
||||
sum += joltage;
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log();
|
||||
|
||||
console.log(sum);
|
||||
|
||||
// Part Two
|
||||
console.time("part2");
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
console.time("part2");
|
||||
let sum2 = 0;
|
||||
|
||||
for (const element of inputArray) {
|
||||
const length = element.length;
|
||||
let firstDigitLocation = 0;
|
||||
let firstDigit = 1;
|
||||
let secondDigitLocation = 1;
|
||||
let secondDigit = 0;
|
||||
let thirdDigitLocation = 2;
|
||||
let thirdDigit = 0;
|
||||
let fourthDigitLocation = 3;
|
||||
let fourthDigit = 0;
|
||||
let fifthDigitLocation = 4;
|
||||
let fifthDigit = 0;
|
||||
let sixthDigitLocation = 5;
|
||||
let sixthDigit = 0;
|
||||
let seventhDigitLocation = 6;
|
||||
let seventhDigit = 0;
|
||||
let eighthDigitLocation = 7;
|
||||
let eighthDigit = 0;
|
||||
let ninthDigitLocation = 8;
|
||||
let ninthDigit = 0;
|
||||
let tenthDigitLocation = 9;
|
||||
let tenthDigit = 0;
|
||||
let eleventhDigitLocation = 10;
|
||||
let eleventhDigit = 0;
|
||||
let lastDigit = 0;
|
||||
|
||||
for (let i = 0; i < length - 11; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > firstDigit) {
|
||||
firstDigit = digit;
|
||||
firstDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = firstDigitLocation + 1; i < length - 10; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > secondDigit) {
|
||||
secondDigit = digit;
|
||||
secondDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = secondDigitLocation + 1; i < length - 9 ; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > thirdDigit) {
|
||||
thirdDigit = digit;
|
||||
thirdDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = thirdDigitLocation + 1; i < length - 8; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > fourthDigit) {
|
||||
fourthDigit = digit;
|
||||
fourthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = fourthDigitLocation + 1; i < length - 7; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > fifthDigit) {
|
||||
fifthDigit = digit;
|
||||
fifthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = fifthDigitLocation + 1; i < length - 6; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > sixthDigit) {
|
||||
sixthDigit = digit;
|
||||
sixthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = sixthDigitLocation + 1; i < length - 5; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > seventhDigit) {
|
||||
seventhDigit = digit;
|
||||
seventhDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = seventhDigitLocation + 1; i < length - 4; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > eighthDigit) {
|
||||
eighthDigit = digit;
|
||||
eighthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = eighthDigitLocation + 1; i < length - 3; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > ninthDigit) {
|
||||
ninthDigit = digit;
|
||||
ninthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = ninthDigitLocation + 1; i < length - 2; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > tenthDigit) {
|
||||
tenthDigit = digit;
|
||||
tenthDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = tenthDigitLocation + 1; i < length - 1; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > eleventhDigit) {
|
||||
eleventhDigit = digit;
|
||||
eleventhDigitLocation = i;
|
||||
}
|
||||
}
|
||||
for (let i = eleventhDigitLocation + 1; i < length; i++) {
|
||||
const digit = Number.parseInt(element[i]);
|
||||
if (digit > lastDigit) {
|
||||
lastDigit = digit;
|
||||
}
|
||||
}
|
||||
|
||||
const joltage = (firstDigit * 100000000000) +
|
||||
(secondDigit * 10000000000) +
|
||||
(thirdDigit * 1000000000) +
|
||||
(fourthDigit * 100000000) +
|
||||
(fifthDigit * 10000000) +
|
||||
(sixthDigit * 1000000) +
|
||||
(seventhDigit * 100000) +
|
||||
(eighthDigit * 10000) +
|
||||
(ninthDigit * 1000) +
|
||||
(tenthDigit * 100) +
|
||||
(eleventhDigit * 10) +
|
||||
lastDigit;
|
||||
sum2 += joltage;
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.timeEnd("part2");
|
||||
console.log(sum2);
|
||||
// functions
|
||||
|
||||
Reference in New Issue
Block a user