Created
February 4, 2014 00:00
-
-
Save anonymous/8794989 to your computer and use it in GitHub Desktop.
Half-arsed answer to this question https://codegolf.stackexchange.com/questions/19955/
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
package TerribleSecret | |
type Unf struct { | |
Pak, Chooie *Unf | |
} | |
func FromNative(n uint) (u *Unf) { | |
u = new(Unf) | |
if n == 0 { | |
return | |
} | |
if n&1 != 0 { | |
u.Pak = u | |
} | |
u.Chooie = FromNative(n >> 1) | |
return | |
} | |
func (u *Unf) ToNative() (n uint) { | |
if u == nil { | |
return | |
} | |
n = u.Chooie.ToNative() | |
n <<= 1 | |
if u.Pak != nil { | |
n |= 1 | |
} | |
return | |
} | |
func (u *Unf) Add(n *Unf) { | |
var f *Unf | |
for { | |
if u == nil { | |
u = new(Unf) | |
} | |
if (n == nil || n.Pak == nil) != (f == nil) { | |
if u.Pak != nil { | |
u.Pak = nil | |
} else { | |
u.Pak = u | |
} | |
} | |
if u.Pak != nil && (n == nil || n.Pak == nil) && f != nil { | |
f = nil | |
} else if u.Pak == nil && n != nil && n.Pak != nil && f == nil { | |
f = n.Pak | |
} | |
if n != nil { | |
n = n.Chooie | |
} | |
if n == nil && f == nil { | |
break | |
} | |
if u.Chooie == nil { | |
u.Chooie = new(Unf) | |
} | |
u = u.Chooie | |
} | |
} |
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
package TerribleSecret | |
import "testing" | |
func TestConversion(t *testing.T) { | |
for n := uint(0); n < 10000; n++ { | |
if r := FromNative(n).ToNative(); r != n { | |
t.Fatalf("%d != %d", n, r) | |
} | |
} | |
} | |
func TestAdd(t *testing.T) { | |
for x := uint(0); x < 1000; x++ { | |
px := FromNative(x) | |
for y := uint(0); y < 1000; y++ { | |
py := FromNative(y) | |
py.Add(px) | |
if r := py.ToNative(); r != x+y { | |
t.Fatalf("%d + %d = %d != %d", x, y, x+y, r) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment