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>
|
</script>
|
||||||
|
|
||||||
<template>
|
<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 />
|
<AppHeader />
|
||||||
<!-- keep relative for transition -->
|
<!-- keep relative for transition -->
|
||||||
<div class="relative">
|
<div class="relative">
|
||||||
|
|
28
src/algo.ts
28
src/algo.ts
|
@ -1,5 +1,6 @@
|
||||||
import { operators } from './globals'
|
import { operators } from './globals'
|
||||||
import { Operation, OperatorType } from './types'
|
import { Operation, OperatorType } from './types'
|
||||||
|
import { shuffle } from './utils'
|
||||||
|
|
||||||
type HistoryType = { a: number; b: number; op: OperatorType }[]
|
type HistoryType = { a: number; b: number; op: OperatorType }[]
|
||||||
|
|
||||||
|
@ -48,6 +49,12 @@ export function operate(
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isSolvable(result: number, plaquettes: number[]): boolean {
|
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 {
|
function printHistory(history: HistoryType): void {
|
||||||
for (const item of history) {
|
for (const item of history) {
|
||||||
if (item.a < item.b) [item.a, item.b] = [item.b, item.a]
|
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 a = plaquettes[i]
|
||||||
let b = plaquettes[j]
|
let b = plaquettes[j]
|
||||||
if (a < b) [a, b] = [b, a]
|
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
|
if (op === '/' && a % b !== 0) continue
|
||||||
recursOperation(op, a, b, plaquettes, history)
|
recursOperation(op, a, b, plaquettes, history)
|
||||||
}
|
}
|
||||||
|
@ -79,7 +87,7 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
||||||
oldHistory: HistoryType,
|
oldHistory: HistoryType,
|
||||||
): void {
|
): void {
|
||||||
if (found) return
|
if (found) return
|
||||||
if (new Date().getTime() - startTime.getTime() > 250) return
|
if (++numberOfIterations > maxIterations) return
|
||||||
|
|
||||||
const plaquettes = [...oldPlaquettes]
|
const plaquettes = [...oldPlaquettes]
|
||||||
const history = [...oldHistory]
|
const history = [...oldHistory]
|
||||||
|
@ -102,7 +110,7 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
||||||
histories.push(history)
|
histories.push(history)
|
||||||
if (!found) {
|
if (!found) {
|
||||||
found = true
|
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
|
return
|
||||||
}
|
}
|
||||||
|
@ -119,11 +127,21 @@ export function isSolvable(result: number, plaquettes: number[]): boolean {
|
||||||
// Start calculations
|
// Start calculations
|
||||||
const start = Date.now()
|
const start = Date.now()
|
||||||
let found = false
|
let found = false
|
||||||
const startTime = new Date()
|
let numberOfIterations = 0
|
||||||
loopOperations(plaquettes, [])
|
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) {
|
if (histories.length) {
|
||||||
printHistory(histories[0])
|
// printHistory(histories[0])
|
||||||
}
|
}
|
||||||
return found
|
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 {
|
export function randItem<T>(items: T[], rnd = Math.random): T {
|
||||||
return items[Math.floor(rnd() * items.length)]
|
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,
|
isSolvable,
|
||||||
operate,
|
operate,
|
||||||
} from '@/algo'
|
} from '@/algo'
|
||||||
import AppHeader from '@/components/AppHeader.vue'
|
|
||||||
import PlaquetteBox from '@/components/common/PlaquetteBox.vue'
|
|
||||||
import OperationsList from '@/components/OperationsList.vue'
|
import OperationsList from '@/components/OperationsList.vue'
|
||||||
import OperatorsList from '@/components/OperatorsList.vue'
|
import OperatorsList from '@/components/OperatorsList.vue'
|
||||||
import PlaquettesList from '@/components/PlaquettesList.vue'
|
import PlaquettesList from '@/components/PlaquettesList.vue'
|
||||||
|
|
|
@ -35,7 +35,5 @@
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useI18n } from 'vue-i18n'
|
import { useI18n } from 'vue-i18n'
|
||||||
|
|
||||||
import AppHeader from '@/components/AppHeader.vue'
|
|
||||||
|
|
||||||
const { t } = useI18n()
|
const { t } = useI18n()
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user