AdventOfCode2022/06/day6.js

78 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2022-12-06 19:38:16 +01:00
let output = document.getElementById("output");
let input;
let fileContent;
function readFile(input) {
let file = input.files[0];
console.log("Reading file", file)
let fileReader = new FileReader();
fileReader.readAsText(file);
fileReader.onload = function() {
fileContent = fileReader.result;
};
fileReader.onerror = function() {
alert(fileReader.error);
};
}
let start = document.getElementById("start");
start.addEventListener("mouseover", () => {
start.innerHTML = "JUST DO IT"
});
start.addEventListener("mouseout", () => {
start.innerHTML = "Read File"
});
start.addEventListener("click", () => {
findPosition(fileContent)
});
function findPosition(line) {
2023-12-09 13:11:52 +01:00
console.time("day06")
2022-12-06 19:38:16 +01:00
let i = 4;
let packetStart = line.substring(0, 4); // should always contain 4 chars
let messageStart = line.substring(0, 14); // should always contain 14 chars
let packetStartI = 0;
while (i < line.length) {
console.log(i, "Char", line[i], "chars:", packetStart);
// if packetStart not found, look for packet start
if (packetStartI == 0) {
if (/(.).*\1/.test(packetStart)) { // if unique
packetStart = packetStart.substring(1, 4) + line[i];
}
else {
packetStartI = i;
messageStart = line.substring(i, i + 14);
}
}
else { // search for message start
if (/(.).*\1/.test(messageStart)) { // if unique
messageStart = messageStart.substring(1, 14) + line[i];
}
else {
2023-12-09 13:11:52 +01:00
output.innerHTML = "Found the first packet start marker at position <b>" + packetStartI + "</b>, the marker is " + packetStart + "<br>" +
"Found the first message start marker at position <b>" + i + "</b>, the marker is " + messageStart + "\n";
console.timeEnd("day06")
2022-12-06 19:38:16 +01:00
return;
}
}
i++;
}
}
2022-12-06 20:20:10 +01:00
function randomizeSnowflakes() {
// Get all elements with the class "snow__flake"
var snowflakes = document.getElementsByClassName("snow__flake");
// Loop through the snowflakes
for (var i = 0; i < snowflakes.length; i++) {
// Generate a random number between 0 and 4
var delay = Math.random() * 4;
// Set the "animation-delay" property for the current snowflake
snowflakes[i].style.setProperty("animation-delay", delay + "s");
}
}
randomizeSnowflakes();