Skip to content

Instantly share code, notes, and snippets.

@0916dhkim
Created December 31, 2022 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0916dhkim/37b502bc275a7f6338f905e8556ce6d6 to your computer and use it in GitHub Desktop.
Save 0916dhkim/37b502bc275a7f6338f905e8556ce6d6 to your computer and use it in GitHub Desktop.
Advent of Code Day 6
const { readFile } = require("fs/promises");
async function readRawInput(path) {
const content = await readFile(path, { encoding: "utf-8" });
return content;
}
/**
* @param {string} fourCharacters ex "abcd"
* @returns true if all unique. false if there's a repeating character.
*/
function allUnique(fourCharacters) {
for (let i = 0; i < 4; i++) {
const char = fourCharacters[i];
for (let j = i + 1; j < 4; j++) {
if (char === fourCharacters[j]) {
return false;
}
}
}
return true;
}
async function main() {
const raw = await readRawInput("./input.txt");
console.log("length", raw.length);
for (let i = 0; i < raw.length - 4; i++) {
const fourChars = raw.substring(i, i + 4);
if (allUnique(fourChars)) {
console.log(i + 4);
return;
}
}
}
main();
const { readFile } = require("fs/promises");
async function readRawInput(path) {
const content = await readFile(path, { encoding: "utf-8" });
return content;
}
/**
* @param {string} fourteenCharacters
* @returns true if all unique. false if there's a repeating character.
*/
function allUnique(fourteenCharacters) {
for (let i = 0; i < 14; i++) {
const char = fourteenCharacters[i];
for (let j = i + 1; j < 14; j++) {
if (char === fourteenCharacters[j]) {
return false;
}
}
}
return true;
}
async function main() {
const raw = await readRawInput("./input.txt");
console.log("length", raw.length);
for (let i = 0; i < raw.length - 14; i++) {
const fourChars = raw.substring(i, i + 14);
if (allUnique(fourChars)) {
console.log(i + 14);
return;
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment