Published
Edited
Jul 5, 2021
Insert cell
Insert cell
Insert cell
import {R,__,curry,equals,flatten,gt,ifElse,includes,map,modulo,multiply,pipe,until} from '@christer/ramda'
Insert cell
Object.assign(window,R)
Insert cell
Insert cell
import {ass, ready, log, cs} from '@christer/utils'
Insert cell
window.even = pipe(modulo(__,2), equals(0))
//window.even = (x) => x % 2 == 0
Insert cell
window.expand1 = ifElse(window.even, x=>[x+2,x*2,x/2], x=>[x+2,x*2])
//window.expand1 = (x) => window.even(x) ? [x+2,x*2,x/2] : [x+2,x*2]
Insert cell
window.expand = (arr) => window.dbg(flatten(map((x) => window.expand1(x),arr)))
//window.expand = (arr) => window.dbg(_.flatten(arr.map((x) => window.expand1(x))))
Insert cell
window.dbg = (x) => log(x.length) || x
Insert cell
cs`
ass false, even 3
ass true, even 4

ass false, includes 4,[5,6]
ass false, includes 4,[7,8,10,12]
ass true, includes 4,[4,9,14,16,20,24]

arr = 1
arr *= 2 until arr > 100
log arr

ass [5,6], expand1 3
ass [7,10,8,12,3], expand [5,6]
ass [9,14,12,20,5,10,16,4,14,24,6], expand [7,10,8,12]

ready 'All asserts ok'
`
Insert cell
Insert cell
cs`
start = new Date()
arr = [3]
arr = expand arr until 8 in arr
log arr.length
(new Date()) - start
`
Insert cell
Insert cell
cs`
log = console.log

[START,TARGET] = [3,10738]

origin = { [START] : 0 }
front = [START]
front1 = []

operation = (a,b) => if b not of origin
origin[b] = a
front1.push b

#operation = (a,b) =>
# if b not of origin then origin[b] = a
# front1.push b

start = new Date()

while TARGET not in front
front.map (x) =>
operation x, x + 2
operation x, x * 2
operation x, x / 2 if x % 2 == 0
[front,front1] = [front1,[]]
log 'front',front.length

last = TARGET
actual = [last]
while last
last = origin[last]
actual.unshift last if last
log (new Date()) - start,'ms'
actual
`
Insert cell
md`## Rekursiv variant
3 -> 10738 tar 1100 ms. Bredden först 25 ms!`
Insert cell
cs`
TARGET = 0
level = 0
f = (path) =>
if path.length >= level
return if TARGET == _.last path then path else false
value = _.last path
if value + 2 not in path
res = f path.concat [value + 2]
if res then return res
if value * 2 not in path
res = f path.concat [value * 2]
if res then return res
if value / 2 not in path and value % 2 == 0
res = f path.concat [value / 2]
if res then return res
false

solve = (a,b) =>
TARGET = b
level = 0
while level < 25
log level
res = f [a]
if res then return res
level += 1

start = new Date()
res = solve 3, 10738
log (new Date()) - start,'ms'
res
`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more