Advent of Code Day 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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