Created
December 7, 2018 05:22
-
-
Save MatrixFrog/51bff0692726d4e61a59d2e38e197bff to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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