generated from eric/adventofcode2023
Compare commits
2 Commits
a466e30de5
...
06de865c12
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
06de865c12 | ||
|
|
26d828fe1a |
56
02/code.js
56
02/code.js
@@ -1,25 +1,65 @@
|
||||
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(",");
|
||||
const inputArray = readFileSync('input.txt').toString().split(",");
|
||||
|
||||
// Part One
|
||||
console.time("part1");
|
||||
let sum = 0;
|
||||
|
||||
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
|
||||
if (digits % 2 === 0) {
|
||||
if (skuString.slice(0, digits / 2) === skuString.slice(digits / 2)) {
|
||||
sum += sku;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
console.timeEnd("part1");
|
||||
console.log(sum);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split(" ");
|
||||
console.time("part2");
|
||||
let sum2 = 0;
|
||||
const repeats = [];
|
||||
|
||||
for (const element of inputArray) {
|
||||
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
|
||||
|
||||
@@ -4,22 +4,23 @@ 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.timeEnd("part1");
|
||||
console.log();
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
}
|
||||
|
||||
console.log();
|
||||
|
||||
console.timeEnd("part2");
|
||||
// functions
|
||||
|
||||
@@ -5,21 +5,24 @@ const inputArray = readFileSync('sample.txt').toString().split("\n");
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log();
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
for (const element of inputArray) {
|
||||
const contents = element.split(" ");
|
||||
|
||||
}
|
||||
|
||||
console.log();
|
||||
|
||||
console.timeEnd("part2");
|
||||
// functions
|
||||
|
||||
Reference in New Issue
Block a user