-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.35.0 Improve energy efficiency using React Compiler
Improve energy efficiency
- Loading branch information
Showing
35 changed files
with
823 additions
and
304 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module.exports = { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
autoprefixer: {} | ||
} | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { | ||
defineConfig, | ||
minimal2023Preset as preset, | ||
minimal2023Preset as preset | ||
} from "@vite-pwa/assets-generator/config"; | ||
|
||
export default defineConfig({ | ||
headLinkOptions: { | ||
preset: "2023", | ||
preset: "2023" | ||
}, | ||
preset, | ||
images: ["public/app-logo.png"], | ||
images: ["public/app-logo.png"] | ||
}); |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
import { moveBlocks } from "../actions"; | ||
import { hasWon, isStuck } from "../state"; | ||
import { LevelState, Move } from "../types"; | ||
|
||
import { Tactic, WeightedMove } from "./solver-tactics/types"; | ||
import { Solver } from "./types"; | ||
|
||
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
const MAX_LEVEL_MOVES = 2_000; // The dumb moves get filtered out, so this is a safe upper limit | ||
|
||
export const FAIL_STATE: [beaten: boolean, moves: Move[], cost: number] = [ | ||
false, | ||
[], | ||
0 | ||
]; | ||
|
||
export const configureSolver = | ||
( | ||
tactics: Tactic[], | ||
scoreState: (level: LevelState) => number, | ||
scoreStateWithMove: (state: LevelState, move: Move) => number, | ||
lookAheadCount: number | ||
): Solver => | ||
async (level, random = Math.random, displayState) => { | ||
let playLevel = level; | ||
const moves: Move[] = []; | ||
|
||
while (!isStuck(playLevel)) { | ||
const nextMove = evaluateBestMove( | ||
playLevel, | ||
tactics, | ||
scoreStateWithMove, | ||
scoreState, | ||
lookAheadCount, | ||
random | ||
); | ||
|
||
if (!nextMove) { | ||
break; | ||
} else { | ||
if (moves.length > MAX_LEVEL_MOVES) { | ||
break; | ||
} | ||
|
||
moves.push({ | ||
from: nextMove.move.from, | ||
to: nextMove.move.to, | ||
tactic: nextMove.name | ||
}); | ||
|
||
playLevel = moveBlocks(playLevel, nextMove.move); | ||
if (displayState) { | ||
const keepSolving = await displayState( | ||
playLevel, | ||
nextMove.move, | ||
nextMove.name ?? "unknown", | ||
moves.length | ||
); | ||
if (!keepSolving) { | ||
return FAIL_STATE; | ||
} | ||
} | ||
if (moves.length % 30 === 0) { | ||
await delay(2); | ||
} | ||
} | ||
if (hasWon(playLevel)) { | ||
return [true, moves, moves.length + MAX_LEVEL_MOVES]; | ||
} | ||
} | ||
|
||
return FAIL_STATE; | ||
}; | ||
|
||
const lookahead = ( | ||
state: LevelState, | ||
move: Move, | ||
depth: number, | ||
tactics: Tactic[], | ||
scoreStateWithMove: (state: LevelState, move: Move) => number, | ||
scoreState: (state: LevelState) => number, | ||
random = Math.random | ||
): number => { | ||
if (depth === 0) { | ||
return scoreStateWithMove(state, move); // Base case: return the score of the current state | ||
} | ||
const nextState = moveBlocks(state, move); | ||
|
||
const moves = generatePossibleMoves(state, tactics, random); | ||
if (moves.length === 0) { | ||
return scoreState(state); // No more moves available, return score | ||
} | ||
|
||
let bestScore = -Infinity; | ||
|
||
for (const move of moves) { | ||
const score = lookahead( | ||
nextState, | ||
move.move, | ||
depth - 1, | ||
tactics, | ||
scoreStateWithMove, | ||
scoreState, | ||
random | ||
); // Recursive lookahead | ||
bestScore = Math.max(bestScore, score); // Track the best score | ||
} | ||
|
||
return bestScore; | ||
}; | ||
|
||
const removeDoubleMoves = ( | ||
move: WeightedMove, | ||
index: number, | ||
list: WeightedMove[] | ||
) => | ||
list.findIndex( | ||
(m2) => m2.move.from === move.move.from && m2.move.to === move.move.to | ||
) === index; | ||
|
||
const generatePossibleMoves = ( | ||
state: LevelState, | ||
tactics: Tactic[], | ||
random = Math.random | ||
): WeightedMove[] => | ||
tactics | ||
.reduce<WeightedMove[]>( | ||
(r, tactic) => | ||
r.concat( | ||
tactic(state, random) | ||
.sort((a, b) => a.weight - b.weight) | ||
.slice(0, 3) | ||
), | ||
[] | ||
) | ||
.filter(removeDoubleMoves); | ||
|
||
const evaluateBestMove = ( | ||
initialState: LevelState, | ||
tactics: Tactic[], | ||
scoreStateWithMove: (state: LevelState, move: Move) => number, | ||
scoreState: (state: LevelState) => number, | ||
lookAheadCount: number, | ||
random = Math.random | ||
): WeightedMove | null => { | ||
const possibleMoves = generatePossibleMoves(initialState, tactics, random); | ||
|
||
let bestMove: WeightedMove | null = null; | ||
let bestScore = -Infinity; | ||
|
||
for (const move of possibleMoves) { | ||
const moveScore = lookahead( | ||
initialState, | ||
move.move, | ||
lookAheadCount, | ||
tactics, | ||
scoreStateWithMove, | ||
scoreState, | ||
random | ||
); | ||
|
||
if (moveScore > bestScore) { | ||
bestScore = moveScore; | ||
bestMove = move; | ||
} | ||
} | ||
|
||
return bestMove; | ||
}; |
This file contains 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
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { randomMove } from "./solver-tactics/randomMove"; | ||
import { stackColumn } from "./solver-tactics/stackColumn"; | ||
import { startColumn } from "./solver-tactics/startColumn"; | ||
import { configureSolver } from "./configurable-solver"; | ||
import { scoreState, scoreStateWithMove } from "./scoreState"; | ||
import { Solver } from "./types"; | ||
|
||
export const defaultSolver: Solver = configureSolver( | ||
[randomMove, startColumn, stackColumn], | ||
scoreState, | ||
scoreStateWithMove, | ||
2 | ||
); | ||
|
||
export const solvers = { | ||
default: defaultSolver | ||
} as const satisfies Record<string, Solver>; |
Oops, something went wrong.