Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

benchmarks_game/fannkuch.fz


# https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/fannkuchredux.html#fannkuchredux
# based on ocaml version by Ethan Burns
fannkuch
post result = 228
=>

  flip (n i32, a Mutable_Array i32) =>
    for i := 0, i+1
    while i <= n / 2
      t := a[i]
      k := n - i
      a[i] := a[k]
      a[k] := t

  count(c i32, a Mutable_Array i32) i32 is
    z := a[0]
    if z != 0
      flip z a
      count (c + 1) a
    else
      c

  rotate (n i32, a Mutable_Array i32) =>
    t := a[0]
    m := n - 1
    for i in 1..m do
      a[i - 1] := a[i]
    a[m] := t


  print(x Mutable_Array i32) =>
    yak "arr: "
    for e in x do
      yak "$e "
    say


  mi : mutate is
  mi.go ()->

    to_marray(x array i32) =>
      res := (mutate.array i32).type.new mi x.length 0
      for i in x.indices do
        res[i] := x[i]
      res

    iter_perms (n i32, f (i32, Mutable_Array i32) -> unit) =>
      mut_n := mut 0
      do_iter (mperm Mutable_Array i32, f (i32, Mutable_Array i32) -> unit, ht i32) =>
        copy := to_marray mperm.as_array
        if ht = 1 then
          f mut_n.get copy
          mut_n <- mut_n.get + 1
        else
          for i in 1..ht do
            do_iter mperm f (ht - 1)
            rotate ht mperm
        unit
      perm := array i32 n i->i
      do_iter to_marray(perm) f n

    n := 7
    csum := mut 0
    m := mut 0
    f(n i32, a Mutable_Array i32) =>
      c := count 0 a
      csum <- csum.get + c * (1 - ((n & 1) << 1))
      if c > m.get then
        m <- c

    iter_perms n ((x,y)->f x y)

    say "Pfannkuchen($n) = ($m)"
    say "$csum"
    csum.get