From e389edd2b35d01e6a55de13b3eaca4966270b57e Mon Sep 17 00:00:00 2001 From: kirbylife Date: Tue, 10 Dec 2024 00:51:53 -0600 Subject: [PATCH] Day 6 part 1 --- day-6/main.js | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 6 ++-- 2 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 day-6/main.js diff --git a/day-6/main.js b/day-6/main.js new file mode 100644 index 0000000..c1234eb --- /dev/null +++ b/day-6/main.js @@ -0,0 +1,92 @@ +import fs from "node:fs"; + +const DIRECTIONS = { + UP: "up", + DOWN: "down", + LEFT: "left", + RIGHT: "right", +}; + +function rotate(currentDir) { + switch (currentDir) { + case DIRECTIONS.UP: + return DIRECTIONS.RIGHT; + case DIRECTIONS.RIGHT: + return DIRECTIONS.DOWN; + case DIRECTIONS.DOWN: + return DIRECTIONS.LEFT; + case DIRECTIONS.LEFT: + return DIRECTIONS.UP; + } +} + +function getInput() { + const data = fs + .readFileSync("input2.txt", "utf8") + .split("\n") + .map((line) => line.split("")); + return data; +} + +function part1() { + let data = getInput(); + const HEIGHT = data.length; + const WIDTH = data[0].length; + let currentDir = DIRECTIONS.UP; + + let actualPos = { x: 0, y: 0 }; + + // Search initial pos + for (let y = 0; y < HEIGHT; y++) { + const x = data[y].indexOf("^"); + if (x == -1) continue; + data[y][x] = "X"; + actualPos.y = y; + actualPos.x = x; + } + // console.log(actualPos); + + // Start algorithm + let counter = 1; + let nextStep = null; + let newX = null; + let newY = null; + while (true) { + switch (currentDir) { + case DIRECTIONS.UP: + newY = actualPos.y - 1; + newX = actualPos.x; + break; + case DIRECTIONS.DOWN: + newY = actualPos.y + 1; + newX = actualPos.x; + break; + case DIRECTIONS.LEFT: + newY = actualPos.y; + newX = actualPos.x - 1; + break; + case DIRECTIONS.RIGHT: + newY = actualPos.y; + newX = actualPos.x + 1; + break; + } + nextStep = (data[newY] ?? [])[newX]; + if (nextStep == undefined) break; + if (nextStep == ".") { + data[newY][newX] = "X"; + actualPos.y = newY; + actualPos.x = newX; + counter++; + } else if (nextStep == "X") { + actualPos.y = newY; + actualPos.x = newX; + } else { + currentDir = rotate(currentDir); + } + } + console.log(counter); + // print board + // console.log(data.map((line) => line.join("")).join("\n")); +} + +part1(); diff --git a/readme.md b/readme.md index e321ec1..b111d81 100644 --- a/readme.md +++ b/readme.md @@ -7,7 +7,8 @@ Día 1: Python Día 2: Zig Día 3: Bash Día 4: LibreOffice Calc -Día 5: Julia +Día 5: Julia +Día 6: Javascript Lenguajes por usar: @@ -19,12 +20,11 @@ Lenguajes por usar: 1. PHP 1. Java 1. Ruby -1. Javascript +1. ~~Javascript~~ 1. ~~LibreOffice Calc~~ 1. Rust 1. Nim 1. C# -1. Latino 1. D 1. ~~Bash~~ 1. Crystal