Skip to content

Instantly share code, notes, and snippets.

@MatrixFrog
Created December 7, 2018 05:22
Show Gist options
  • Save MatrixFrog/51bff0692726d4e61a59d2e38e197bff to your computer and use it in GitHub Desktop.
Save MatrixFrog/51bff0692726d4e61a59d2e38e197bff to your computer and use it in GitHub Desktop.
async function day3() {
const canvas = document.querySelector('canvas');
const ctx = canvas.getContext('2d');
ctx.fillStyle = 'rgb(128, 0, 0, 0.5)';
const response = await fetch('./day3.txt', { encoding: 'UTF-8'});
const text = await response.text();
const lines = text.split('\n').filter(Boolean);
for (const line of lines) {
const [id, left, top, width, height] = line.split(/[@,:x ]+/);
ctx.fillRect(left, top, width, height);
}
const imageData = ctx.getImageData(0, 0, 2000, 2000);
let result = 0;
for (const px of imageData.data) {
if (px > 128) { result++; }
}
console.log(result);
}
document.addEventListener('DOMContentLoaded', day3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment