Skip to content

Commit

Permalink
day-6: part-2 wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bigandy committed Dec 7, 2024
1 parent ee073cd commit f1f98b0
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 24 deletions.
137 changes: 113 additions & 24 deletions day-6/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// import { input } from "./test";
import { input } from "./real";
import { input, inputWithObstacle } from "./test";
// import { input } from "./real";

// get a string of all input characters in a row.
const inputString = input.replaceAll("\n", "");
const inputString = inputWithObstacle.replaceAll("\n", "");
const inputStringLength = inputString.length;
const rowLength = input.split("\n").filter(Boolean)[0].length;

const replaceCharacterAtIndex = (
inputString: string,
index: number,
cursorIndex: number,
newCharacter: string
) => {
let newString = inputString;
newString =
newString.substring(0, index) +
newString.substring(0, cursorIndex) +
newCharacter +
newString.substring(index + 1);
newString.substring(cursorIndex + 1);
return newString;
};

Expand All @@ -34,9 +34,9 @@ const getCursorAndNextCharacterInformation = (inputString: string) => {
const down = inputString.indexOf("v");
const left = inputString.indexOf("<");

let cursor = undefined;
let nextCharIndex = undefined;
let directionCharacter = undefined;
let cursor = 0;
let nextCharIndex = 0;
let directionCharacter = "^";

if (up !== -1) {
cursor = up;
Expand All @@ -59,6 +59,7 @@ const getCursorAndNextCharacterInformation = (inputString: string) => {
return {
cursor,
nextCharIndex,
nextChar: inputString.charAt(nextCharIndex),
directionCharacter,
};
};
Expand All @@ -75,29 +76,29 @@ const firstAnswer = () => {
const { cursor, nextCharIndex, directionCharacter } =
getCursorAndNextCharacterInformation(inputString);

if (nextCharIndex! > inputStringLength || nextCharIndex! < 0) {
if (nextCharIndex > inputStringLength || nextCharIndex < 0) {
// replace the cursor with X and return it.
return replaceCharacterAtIndex(inputString, cursor!, "X");
return replaceCharacterAtIndex(inputString, cursor, "X");
}

if (inputString.charAt(nextCharIndex!) === "#") {
// rotate Current Direction
const newDirectionCharacter = getNextDirection(directionCharacter!);
const newDirectionCharacter = getNextDirection(directionCharacter);

let newString = replaceCharacterAtIndex(
inputString,
cursor!,
cursor,
newDirectionCharacter
);
return processString(newString);
} else {
// replace the current cursor location with a x
let newString = replaceCharacterAtIndex(inputString, cursor!, "X");
let newString = replaceCharacterAtIndex(inputString, cursor, "X");
// move the cursor to the nextChar location
newString = replaceCharacterAtIndex(
newString,
nextCharIndex!,
directionCharacter!
nextCharIndex,
directionCharacter
);
// console.log(newString);

Expand All @@ -113,15 +114,103 @@ const firstAnswer = () => {
console.log("part-1 answer:", count);
};

firstAnswer();
// firstAnswer();

const findNewCharacter = (nextChar, directionalCharacter) => {
console.log({ nextChar, directionalCharacter });

if (nextChar === "X") {
if (["<", ">"].includes(directionalCharacter)) {
return "-";
} else if (["^", "v"].includes(directionalCharacter)) {
return "|";
}
} else if (nextChar === "|") {
if (["<", ">"].includes(directionalCharacter)) {
return "+";
}
return "|";
} else if (nextChar === "-") {
if (["v", "^"].includes(directionalCharacter)) {
return "+";
}
return "-";
} else if (nextChar === "+") {
return false;
} else {
return "X";
}
};

const secondAnswer = () => {
const processString = (inputString: string) => {
const { cursor, nextCharIndex, nextChar, directionCharacter } =
getCursorAndNextCharacterInformation(inputString);

const goingOverBoard =
nextCharIndex > inputStringLength || nextCharIndex < 0;

// const secondAnswer = () => {
// let count = 0;
// rows.forEach((row) => {
// If going off the board, replace the final character with an X.
if (goingOverBoard) {
// replace the cursor with X and return it.
return replaceCharacterAtIndex(inputString, cursor!, "X");
}

// });
if (nextChar === "#" || nextChar === "O") {
// rotate Current Direction
const newDirectionCharacter = getNextDirection(directionCharacter);

// console.log("part-2 answer:", count);
// };
let newString = replaceCharacterAtIndex(
inputString,
cursor,
newDirectionCharacter
);
return processString(newString);
} else {
const getNewCharacter = findNewCharacter(nextChar, directionCharacter);
console.log({ getNewCharacter });
if (!getNewCharacter) {
return inputString;
}

// replace the current cursor location with a x
let newString = replaceCharacterAtIndex(
inputString,
cursor,
getNewCharacter!
);
// move the cursor to the nextChar location
newString = replaceCharacterAtIndex(
newString,
nextCharIndex,
directionCharacter
);
// console.log(newString);

return processString(newString);
}
};

const answer = processString(inputString);
const check = `
....#.....
....+---+#
....|...|.
..#.|...|.
....|..#|.
....|...|.
.#.O^---+.
........#.
#.........
......#...
`;
console.log({
answe: answer,
check: check.split("\n").filter(Boolean).join(""),
});
// const count = (answer.match(new RegExp("X", "g")) || []).length;

// console.log("part-2 answer:", count);
};

// secondAnswer();
secondAnswer();
13 changes: 13 additions & 0 deletions day-6/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ export const input = `
#.........
......#...
`;

export const inputWithObstacle = `
....#.....
.........#
..........
..#.......
.......#..
..........
.#.O^.....
........#.
#.........
......#...
`;

0 comments on commit f1f98b0

Please sign in to comment.