generated from eric/adventofcode2023
day seven both parts
This commit is contained in:
103
07/code.js
Normal file
103
07/code.js
Normal file
@@ -0,0 +1,103 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
|
||||
let sampleMode = false;
|
||||
let usedArray = [];
|
||||
|
||||
const sampleArray = readFileSync('sample.txt').toString().split("\n");
|
||||
const inputArray = readFileSync('input.txt').toString().split("\n");
|
||||
|
||||
if (sampleMode) {
|
||||
usedArray = sampleArray;
|
||||
} else {
|
||||
usedArray = inputArray;
|
||||
}
|
||||
|
||||
// Part One
|
||||
|
||||
console.time("part1");
|
||||
|
||||
let newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let i = 0; i < newInput.length; i++) {
|
||||
newInput[i] = newInput[i].split("");
|
||||
}
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if ((newInput[i - 1][j] === '|' || newInput[i - 1][j] === 'S') && newInput[i][j] === '.') {
|
||||
newInput[i][j] = '|';
|
||||
}
|
||||
if (newInput[i - 1][j] === '|' && newInput[i][j] === '^') {
|
||||
if (j > 0 && newInput[i][j - 1] === '.') {
|
||||
newInput[i][j - 1] = '|';
|
||||
}
|
||||
}
|
||||
if (newInput[i - 1][j] === '|' && newInput[i][j] === '^') {
|
||||
if (j < newInput[i].length - 1 && newInput[i][j + 1] === '.') {
|
||||
newInput[i][j + 1] = '|';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let splits = 0;
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if (newInput[i][j] === '^' && newInput[i - 1][j] === '|') {
|
||||
splits++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part1");
|
||||
console.log(splits);
|
||||
|
||||
|
||||
// Part Two
|
||||
|
||||
console.time("part2");
|
||||
|
||||
newInput = JSON.parse(JSON.stringify(usedArray));
|
||||
for (let i = 0; i < newInput.length; i++) {
|
||||
newInput[i] = newInput[i].split("");
|
||||
}
|
||||
|
||||
for (let i = 1; i < newInput.length; i++) {
|
||||
for (let j = 0; j < newInput[i].length; j++) {
|
||||
if (newInput[i - 1][j] === 'S') {
|
||||
newInput[i][j] = 1;
|
||||
}
|
||||
if (Number.parseInt(newInput[i - 1][j]) > 0 && Number.parseInt(newInput[i][j]) > 0) {
|
||||
newInput[i][j] = newInput[i][j] + newInput[i - 1][j];
|
||||
}
|
||||
if ((Number.parseInt(newInput[i - 1][j]) > 0) && newInput[i][j] === '.') {
|
||||
newInput[i][j] = newInput[i - 1][j];
|
||||
}
|
||||
if (Number.parseInt(newInput[i - 1][j]) > 0 && newInput[i][j] === '^') {
|
||||
if (j > 0 && newInput[i][j - 1] === '.') {
|
||||
newInput[i][j - 1] = newInput[i - 1][j];
|
||||
} else if (j > 0 && Number.parseInt(newInput[i][j - 1]) > 0) {
|
||||
newInput[i][j - 1] = Number.parseInt(newInput[i][j - 1]) + newInput[i - 1][j];
|
||||
}
|
||||
if (j < newInput[i].length - 1 && newInput[i][j + 1] === '.') {
|
||||
newInput[i][j + 1] = newInput[i - 1][j];
|
||||
} else if (j < newInput[i].length - 1 && Number.parseInt(newInput[i][j + 1]) > 0) {
|
||||
newInput[i][j + 1] = Number.parseInt(newInput[i][j + 1]) + newInput[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let timelines = 0;
|
||||
|
||||
const bottomRow = newInput[newInput.length - 1];
|
||||
for (let i = 0; i < bottomRow.length; i++) {
|
||||
if (Number.parseInt(bottomRow[i]) > 0) {
|
||||
timelines += Number.parseInt(bottomRow[i]);
|
||||
}
|
||||
}
|
||||
|
||||
console.timeEnd("part2");
|
||||
console.log(timelines);
|
||||
|
||||
// functions
|
||||
Reference in New Issue
Block a user