More randomness in solving algo, and no longer dependant on timer
This commit is contained in:
parent
7eae7202bc
commit
9de2e42c42
|
@ -3,7 +3,7 @@ import AppHeader from './components/AppHeader.vue'
|
|||
</script>
|
||||
|
||||
<template>
|
||||
<div class="container flex overflow-hidden flex-col px-2 mx-auto h-full max-w-md">
|
||||
<div class="container flex overflow-hidden flex-col px-2 mx-auto max-w-md h-full">
|
||||
<AppHeader />
|
||||
<!-- keep relative for transition -->
|
||||
<div class="relative">
|
||||
|
|
28
src/algo.ts
28
src/algo.ts
|
@ -1,5 +1,6 @@
|
|||
import { operators } from './globals'
|
||||
import { Operation, OperatorType } from './types'
|
||||
import { shuffle } from './utils'
|
||||
|
||||
type HistoryType = { a: number; b: number; op: OperatorType }[]
|
||||
|
||||
|
@ -48,6 +49,12 @@ export function operate(
|
|||
}
|
||||
|
||||
export function isSolvable(result: number, plaquettes: number[]): boolean {
|
||||
console.log('---')
|
||||
console.log('Solving ' + result)
|
||||
|
||||
// Bigger numbers have more time to solve themselves
|
||||
const maxIterations = result * 100
|
||||
|
||||
function printHistory(history: HistoryType): void {
|
||||
for (const item of history) {
|
||||
if (item.a < item.b) [item.a, item.b] = [item.b, item.a]
|
||||
|
@ -63,7 +70,8 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
|||
let a = plaquettes[i]
|
||||
let b = plaquettes[j]
|
||||
if (a < b) [a, b] = [b, a]
|
||||
for (const op of operators) {
|
||||
const ops = shuffle([...operators])
|
||||
for (const op of ops) {
|
||||
if (op === '/' && a % b !== 0) continue
|
||||
recursOperation(op, a, b, plaquettes, history)
|
||||
}
|
||||
|
@ -79,7 +87,7 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
|||
oldHistory: HistoryType,
|
||||
): void {
|
||||
if (found) return
|
||||
if (new Date().getTime() - startTime.getTime() > 250) return
|
||||
if (++numberOfIterations > maxIterations) return
|
||||
|
||||
const plaquettes = [...oldPlaquettes]
|
||||
const history = [...oldHistory]
|
||||
|
@ -102,7 +110,7 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
|||
histories.push(history)
|
||||
if (!found) {
|
||||
found = true
|
||||
console.log('1e solution trouvée en ' + (Date.now() - start) + 'ms')
|
||||
console.log(`1e solution trouvée en ${Date.now() - start}ms`)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -119,11 +127,21 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
|||
// Start calculations
|
||||
const start = Date.now()
|
||||
let found = false
|
||||
const startTime = new Date()
|
||||
let numberOfIterations = 0
|
||||
loopOperations(plaquettes, [])
|
||||
if (found) {
|
||||
console.log(
|
||||
`Réussite : ${Date.now() - start}ms et ${numberOfIterations} iterations`,
|
||||
)
|
||||
}
|
||||
else {
|
||||
console.log(
|
||||
`Echec : ${Date.now() - start}ms et ${numberOfIterations} iterations`,
|
||||
)
|
||||
}
|
||||
|
||||
if (histories.length) {
|
||||
printHistory(histories[0])
|
||||
// printHistory(histories[0])
|
||||
}
|
||||
return found
|
||||
|
||||
|
|
25
src/utils.ts
25
src/utils.ts
|
@ -11,3 +11,28 @@ export function randRange(min: number, max: number, rnd = Math.random): number {
|
|||
export function randItem<T>(items: T[], rnd = Math.random): T {
|
||||
return items[Math.floor(rnd() * items.length)]
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuffles an array in place and returns it
|
||||
* @param array
|
||||
* @returns
|
||||
*/
|
||||
export function shuffle<T>(array: T[]): T[] {
|
||||
let currentIndex = array.length
|
||||
let randomIndex
|
||||
|
||||
// While there remain elements to shuffle...
|
||||
while (currentIndex !== 0) {
|
||||
// Pick a remaining element...
|
||||
randomIndex = Math.floor(Math.random() * currentIndex)
|
||||
currentIndex--
|
||||
|
||||
// And swap it with the current element.
|
||||
;[array[currentIndex], array[randomIndex]] = [
|
||||
array[randomIndex],
|
||||
array[currentIndex],
|
||||
]
|
||||
}
|
||||
|
||||
return array
|
||||
}
|
||||
|
|
|
@ -74,8 +74,6 @@ import {
|
|||
isSolvable,
|
||||
operate,
|
||||
} from '@/algo'
|
||||
import AppHeader from '@/components/AppHeader.vue'
|
||||
import PlaquetteBox from '@/components/common/PlaquetteBox.vue'
|
||||
import OperationsList from '@/components/OperationsList.vue'
|
||||
import OperatorsList from '@/components/OperatorsList.vue'
|
||||
import PlaquettesList from '@/components/PlaquettesList.vue'
|
||||
|
|
|
@ -35,7 +35,5 @@
|
|||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
import AppHeader from '@/components/AppHeader.vue'
|
||||
|
||||
const { t } = useI18n()
|
||||
</script>
|
||||
|
|
Loading…
Reference in New Issue
Block a user