generated from eric/adventofcode2023
day two part two
This commit is contained in:
35
02/code.js
35
02/code.js
@@ -1,10 +1,10 @@
|
|||||||
import { readFileSync } from 'node:fs';
|
import { readFileSync } from 'node:fs';
|
||||||
|
|
||||||
//const inputArray = readFileSync('sample.txt').toString().split(",");
|
// const inputArray = readFileSync('sample.txt').toString().split(",");
|
||||||
const inputArray = readFileSync('input.txt').toString().split(",");
|
const inputArray = readFileSync('input.txt').toString().split(",");
|
||||||
|
|
||||||
// Part One
|
// Part One
|
||||||
|
console.time("part1");
|
||||||
let sum = 0;
|
let sum = 0;
|
||||||
|
|
||||||
for (const element of inputArray) {
|
for (const element of inputArray) {
|
||||||
@@ -22,17 +22,44 @@ for (const element of inputArray) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
console.timeEnd("part1");
|
||||||
console.log(sum);
|
console.log(sum);
|
||||||
|
|
||||||
|
|
||||||
// Part Two
|
// Part Two
|
||||||
|
|
||||||
|
console.time("part2");
|
||||||
|
let sum2 = 0;
|
||||||
|
const repeats = [];
|
||||||
|
|
||||||
for (const element of inputArray) {
|
for (const element of inputArray) {
|
||||||
const contents = element.split("-");
|
const contents = element.split("-");
|
||||||
|
const start = Number.parseInt(contents[0]);
|
||||||
|
const end = Number.parseInt(contents[1]);
|
||||||
|
|
||||||
|
for (let sku = start; sku <= end; sku++) {
|
||||||
|
const skuString = sku.toString();
|
||||||
|
const digits = skuString.length
|
||||||
|
const maxRepeatLength = Math.floor(digits / 2);
|
||||||
|
for (let repeatLength = 1; repeatLength <= maxRepeatLength; repeatLength++) {
|
||||||
|
if (digits % repeatLength === 0) {
|
||||||
|
const maxRepeats = digits / repeatLength;
|
||||||
|
const repeatPattern = skuString.slice(0, repeatLength);
|
||||||
|
const repeatedPattern = repeatPattern.repeat(maxRepeats);
|
||||||
|
if (repeatedPattern === skuString) {
|
||||||
|
repeats.push(sku);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log();
|
const uniqueRepeats = [...new Set(repeats)];
|
||||||
|
|
||||||
|
for (const repeat of uniqueRepeats) {
|
||||||
|
sum2 += Number.parseInt(repeat);
|
||||||
|
}
|
||||||
|
console.timeEnd("part2");
|
||||||
|
console.log(sum2);
|
||||||
|
|
||||||
// functions
|
// functions
|
||||||
|
|||||||
@@ -4,22 +4,23 @@ const inputArray = readFileSync('sample.txt').toString().split("\n");
|
|||||||
// const inputArray = readFileSync('input.txt').toString().split("\n");
|
// const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||||
|
|
||||||
// Part One
|
// Part One
|
||||||
|
console.time("part1");
|
||||||
for (const element of inputArray) {
|
for (const element of inputArray) {
|
||||||
const contents = element.split(" ");
|
const contents = element.split(" ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.timeEnd("part1");
|
||||||
console.log();
|
console.log();
|
||||||
|
|
||||||
|
|
||||||
// Part Two
|
// Part Two
|
||||||
|
console.time("part2");
|
||||||
for (const element of inputArray) {
|
for (const element of inputArray) {
|
||||||
const contents = element.split(" ");
|
const contents = element.split(" ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log();
|
console.log();
|
||||||
|
console.timeEnd("part2");
|
||||||
// functions
|
// functions
|
||||||
|
|||||||
@@ -5,21 +5,24 @@ const inputArray = readFileSync('sample.txt').toString().split("\n");
|
|||||||
|
|
||||||
// Part One
|
// Part One
|
||||||
|
|
||||||
|
console.time("part1");
|
||||||
for (const element of inputArray) {
|
for (const element of inputArray) {
|
||||||
const contents = element.split(" ");
|
const contents = element.split(" ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.timeEnd("part1");
|
||||||
console.log();
|
console.log();
|
||||||
|
|
||||||
|
|
||||||
// Part Two
|
// Part Two
|
||||||
|
|
||||||
|
console.time("part2");
|
||||||
for (const element of inputArray) {
|
for (const element of inputArray) {
|
||||||
const contents = element.split(" ");
|
const contents = element.split(" ");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log();
|
console.log();
|
||||||
|
console.timeEnd("part2");
|
||||||
// functions
|
// functions
|
||||||
|
|||||||
Reference in New Issue
Block a user