Last active
June 9, 2026 12:59
-
-
Save mg901/cbe02ed9ad51a5ea5f048837f6c8d91f to your computer and use it in GitHub Desktop.
"Grid Lights" Solution
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
| import { useState, useEffect, useMemo } from "react"; | |
| export default function App() { | |
| return <Table data={table} />; | |
| } | |
| const DELAY = 300; | |
| const table = [ | |
| [[1], [1], [1]], | |
| [[1], [0], [1]], | |
| [[1], [1], [1]], | |
| ]; | |
| function Table({ data = [] }) { | |
| const [order, setOrder] = useState([]); | |
| const [isDeactivating, setIsDeactivating] = useState(false); | |
| const activeSet = useMemo(() => new Set(order), [order]); | |
| const flatData = useMemo(() => data.flat(2), [data]); | |
| const activeCells = useMemo(() => flatData.filter((val) => val !== 0).length, [flatData]); | |
| useEffect(() => { | |
| if (!isDeactivating) return; | |
| if (order.length === 0) { | |
| setIsDeactivating(false); | |
| return; | |
| } | |
| const timer = setTimeout(() => { | |
| setOrder((prev) => prev.slice(0, -1)); | |
| }, DELAY); | |
| return () => { | |
| clearTimeout(timer); | |
| }; | |
| }, [isDeactivating, order.length]); | |
| const handleClick = (index) => { | |
| if (isDeactivating || activeSet.has(index)) return; | |
| const nextOrder = [...order, index]; | |
| setOrder(nextOrder); | |
| if (nextOrder.length === activeCells) { | |
| setIsDeactivating(true); | |
| } | |
| }; | |
| return ( | |
| <div className="table"> | |
| {flatData.map((val, i) => ( | |
| <Cell | |
| key={i} | |
| index={i} | |
| isActive={activeSet.has(i)} | |
| isDisabled={val === 0} | |
| onClick={handleClick} | |
| /> | |
| ))} | |
| <div>{order.join(", ")}</div> | |
| </div> | |
| ); | |
| } | |
| function Cell({ index, isActive, isDisabled, onClick }) { | |
| return ( | |
| <button | |
| className="cell" | |
| data-active={isActive} | |
| disabled={isDisabled} | |
| onClick={() => onClick(index)} | |
| /> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment