generated from eric/adventofcode2023
61 lines
1.2 KiB
JavaScript
61 lines
1.2 KiB
JavaScript
import { readFileSync } from 'node:fs';
|
|
|
|
// const inputArray = readFileSync('sample.txt').toString().split("\n");
|
|
const inputArray = readFileSync('input.txt').toString().split("\n");
|
|
|
|
// Part One
|
|
|
|
let zeroCount = 0;
|
|
let currentPosition = 50;
|
|
|
|
for (const element of inputArray) {
|
|
const instruction = element;
|
|
const direction = instruction[0];
|
|
const steps = Number.parseInt(instruction.slice(1));
|
|
|
|
if (direction === 'L') {
|
|
currentPosition -= steps;
|
|
} else {
|
|
currentPosition += steps;
|
|
}
|
|
|
|
if (currentPosition === 0 || (currentPosition % 100) === 0) {
|
|
zeroCount++;
|
|
}
|
|
}
|
|
|
|
console.log(zeroCount);
|
|
|
|
|
|
// Part Two
|
|
|
|
let zeroCount2 = 0;
|
|
let currentPosition2 = 50;
|
|
|
|
for (const element of inputArray) {
|
|
const instruction = element;
|
|
const direction = instruction[0];
|
|
const steps = Number.parseInt(instruction.slice(1));
|
|
|
|
if (direction === 'L') {
|
|
for (let i = 1; i < steps + 1; i++) {
|
|
currentPosition2 -= 1;
|
|
if (currentPosition2 === 0 || (currentPosition2 % 100) === 0) {
|
|
zeroCount2++;
|
|
}
|
|
}
|
|
} else {
|
|
for (let i = 1; i < steps + 1; i++) {
|
|
currentPosition2 += 1;
|
|
if (currentPosition2 === 0 || (currentPosition2 % 100) === 0) {
|
|
zeroCount2++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
console.log(zeroCount2);
|
|
|
|
// functions
|
|
|