Last active
January 24, 2025 15:17
-
-
Save chalkpe/e813fdb8e802095d35800859f715a641 to your computer and use it in GitHub Desktop.
is-odd
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
// example: deno run -A main.ts 52 10 | |
import { decode } from 'https://deno.land/x/[email protected]/mod.ts' | |
import puppeteer from 'https://deno.land/x/[email protected]/mod.ts' | |
const isOdd = async (num: number) => { | |
const browser = await puppeteer.launch({ | |
defaultViewport: { width: 1024, height: 1024 }, | |
}) | |
try { | |
const page = await browser.newPage() | |
await page.evaluate(` | |
document.body.innerHTML = '<div></div>'.repeat(${num}) | |
document.head.innerHTML = '<style>' + | |
'div { width: 1px; height: 1px; background: red } div:nth-child(odd) { background: blue }' + | |
'body { margin: 0; display: grid; grid-template-columns: repeat(1024, 1px); grid-template-rows: repeat(1024, 1px) }'`) | |
const [r, g, b, a] = decode(await page.screenshot()).image.slice(4 * (num - 1)) | |
return r === 0 && g === 0 && b === 255 && a === 255 // blue | |
} finally { | |
await browser.close() | |
} | |
} | |
if (import.meta.main) { | |
const [x, y] = [parseInt(Deno.args[0]), parseInt(Deno.args[1])] | |
console.log(await isOdd(x % (1024 * 1024)) ? x + y : x - y) // odd: add, even: subtract | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment