Skip to content

Commit

Permalink
opened codeiq 608
Browse files Browse the repository at this point in the history
  • Loading branch information
cielavenir committed Dec 25, 2013
1 parent d63b38c commit 9eab68d
Show file tree
Hide file tree
Showing 26 changed files with 1,475 additions and 0 deletions.
46 changes: 46 additions & 0 deletions tyama_codeiq608_iter.cs
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
class CodeIQRoute{
static IEnumerable<List<T>> Permutation<T>(List<T> x,int? _n=null) where T : IComparable<T>{
int n=_n ?? x.Count;
if(n<0||x.Count<n)yield break;
List<T> a=new List<T>(x);
a.Sort();
yield return a.GetRange(0,n);
for(;;){
int i;
a.Reverse(n,a.Count-n);
for(i=a.Count-2;i>=0;i--)if(a[i].CompareTo(a[i+1])<0)break;
if(i<0){
//a.Reverse(0,a.Count);
/*yield*/ break;
}
int k=i;
for(i=a.Count-1;i>=k+1;i--)if(a[k].CompareTo(a[i])<0)break;
int l=i;
T z=a[k];a[k]=a[l];a[l]=z;
a.Reverse(k+1,a.Count-(k+1));
yield return a.GetRange(0,n);
}
}
const int N=6;
public static void Main(){
int r=0,i;
List<int>e0=new List<int>(),f0=new List<int>();
for(i=0;i<N;i++){e0.Add(0);f0.Add(0);}
for(i=0;i<N;i++){e0.Add(1);f0.Add(1);}
int[] e=new int[N*2+1];
int[] f=new int[N*2+1];
foreach(List<int> _e in Permutation(e0)){
for(i=0;i<N*2;i++)e[i+1]=e[i]+_e[i];
foreach(List<int> _f in Permutation(f0)){
for(i=0;i<N*2;i++){
f[i+1]=f[i]+_f[i];
if(e[i]==f[i]&&e[i+1]==f[i+1])break;
}
if(i==N*2)r++;
}
}
Console.WriteLine(r);
}
}
118 changes: 118 additions & 0 deletions tyama_codeiq608_iter.go
@@ -0,0 +1,118 @@
package main
import (
"fmt"
"sort"
"reflect"
)

/*
func compare(a interface{},b interface{}) int{ // a and b must have the same type. If different, runtime error will be triggered. will be fixed after generics is introduced.
switch reflect.ValueOf(a).Kind() {
case reflect.Int:
n1:=reflect.ValueOf(a).Int()
n2:=reflect.ValueOf(b).Int()
if n1<n2 {return -1} else if n1>n2 {return 1} else {return 0}
case reflect.Float32: case reflect.Float64:
n1:=reflect.ValueOf(a).Float()
n2:=reflect.ValueOf(b).Float()
if n1<n2 {return -1} else if n1>n2 {return 1} else {return 0}
case reflect.String:
n1:=reflect.ValueOf(a).String()
n2:=reflect.ValueOf(b).String()
if n1<n2 {return -1} else if n1>n2 {return 1} else {return 0}
}
return 0 //lol
}
func reflect_reverse(a reflect.Value,start int,size int){
for end:=start+size-1;start<end;start++ {
z:=a.Index(start).Interface()
a.Index(start).Set(a.Index(end))
a.Index(end).Set(reflect.ValueOf(z))
end--
}
}
func permutation(x interface{}, n int) <- chan reflect.Value{
ch := make(chan reflect.Value)
go func(){
if 0<=n&&n<=reflect.ValueOf(x).Len() {
a:=reflect.MakeSlice(reflect.TypeOf(x),reflect.ValueOf(x).Len(),reflect.ValueOf(x).Len())
reflect.Copy(a,reflect.ValueOf(x))
//sort.Sort(sort.IntSlice(a)); //interface{} cannot be sorted, so you must sort the array prior.
ch <- a
for {
reflect_reverse(a,n,a.Len()-n)
i:=0
for i=a.Len()-2;i>=0;i-- {if compare(a.Index(i).Interface(),a.Index(i+1).Interface())<0 {break}}
if i<0 {
//reflect_reverse(a,0,a.Len())
break
}
k:=i
for i=a.Len()-1;i>=k+1;i-- {if compare(a.Index(k).Interface(),a.Index(i).Interface())<0 {break}}
l:=i
z:=a.Index(k).Interface()
a.Index(k).Set(a.Index(l))
a.Index(l).Set(reflect.ValueOf(z))
reflect_reverse(a,k+1,a.Len()-(k+1))
ch <- a
}
}
close(ch)
}()
return ch
}
*/
func reverse(a sort.Interface,start int,size int){
for end:=start+size-1;start<end;start++ {
a.Swap(start,end)
end--
}
}
func permutation(a sort.Interface, n int) <- chan reflect.Value{
ch := make(chan reflect.Value)
go func(){
if 0<=n&&n<=a.Len() {
sort.Sort(a); // a is modified directly, so never write to it within the block
ch <- reflect.ValueOf(a)
for {
reverse(a,n,a.Len()-n)
i:=0
for i=a.Len()-2;i>=0;i-- {if a.Less(i,i+1) {break}}
if i<0 {
reverse(a,0,a.Len())
break
}
k:=i
for i=a.Len()-1;i>=k+1;i-- {if a.Less(k,i) {break}}
l:=i
a.Swap(k,l)
reverse(a,k+1,a.Len()-(k+1))
ch <- reflect.ValueOf(a)
}
}
close(ch)
}()
return ch
}

func main(){
N:=6
e0:=make([]int,N*2)
f0:=make([]int,N*2)
i:=0
r:=0
for i=0;i<N;i++ {e0[N+i]=1;f0[N+i]=1}
e:=make([]int,N*2+1);
f:=make([]int,N*2+1);
for _e:=range permutation(sort.IntSlice(e0),len(e0)) {
for i=0;i<N*2;i++ {e[i+1]=e[i]+_e.Index(i).Interface().(int)}
for _f:=range permutation(sort.IntSlice(f0),len(f0)) {
for i=0;i<N*2;i++ {
f[i+1]=f[i]+_f.Index(i).Interface().(int)
if e[i]==f[i]&&e[i+1]==f[i+1] {break}
}
if i==N*2 {r++}
}
}
fmt.Println(r)
}
57 changes: 57 additions & 0 deletions tyama_codeiq608_iter.n
@@ -0,0 +1,57 @@
//seems ncc dislikes this code...
//error: internal compiler error: got ArgumentException (type is not TypeBuilder but System.MonoType

using System;
using System.Array;
using System.Collections.Generic;
using Nemerle.Imperative;

public class CodeIQRoute{
static permutation[T](x:array[T],n:int):IEnumerable[array[T]] where T : IComparable[T]{
when(0<=n&&n<=x.Length){
def a=array(x.Length);
mutable i:int=0;
for(i=0;i<x.Length;i++)a[i]=x[i];
for(;;){
yield a;
Reverse(a,n,a.Length-n);
for(i=a.Length-2;i>=0;i--)when(a[i].CompareTo(a[i+1])<0)break;
when(i<0){
//Reverse(a,0,a.Length);
break;
}
def k:int=i;
for(i=a.Length-1;i>=k+1;i--)when(a[k].CompareTo(a[i])<0)break;
def l:int=i;
def z=a[k];a[k]=a[l];a[l]=z;
Reverse(a,k+1,a.Length-(k+1));
}
}
}

public static Main(): void{
def N=6;
mutable r:int=0;
mutable i:int;
def e0=array(N*2);
def f0=array(N*2);
for(i=0;i<N;i++){
e0[i]=0;f0[i]=0;
e0[N+i]=1;f0[N+i]=1;
}
def e=array(N*2+1);
def f=array(N*2+1);
for(i=0;i<N*2+1;i++){e[i]=0;f[i]=0;}
foreach(_e in permutation(e0,e0.Length)){
for(i=0;i<N*2;i++)e[i+1]=e[i]+_e[i];
foreach(_f in permutation(f0,f0.Length)){
for(i=0;i<N*2;i++){
f[i+1]=f[i]+_f[i];
when(e[i]==f[i]&&e[i+1]==f[i+1])break;
}
when(i==N*2)r++;
}
}
Console.WriteLine(r);
}
}
40 changes: 40 additions & 0 deletions tyama_codeiq608_iter.pl
@@ -0,0 +1,40 @@
#!/usr/bin/perl
use strict;
sub permute(&@){
my $code = shift;
my @a = @_;
while($code->(@a)){
my $i;
#push(@a,reverse splice @a, $n);
for($i=$#a-1;$i>=0;$i--){if($a[$i]<$a[$i+1]){last;}}
if($i<0){return;}
my $k=$i;
for($i=$#a;$i>=$k+1;$i--){if($a[$k]<$a[$i]){last;}}
my $l=$i;
@a[$k,$l]=@a[$l,$k];
push(@a, reverse splice @a, $k+1);
}
}

my $N=6;
my @e0;
my @f0;
my $i;
my $r=0;
for($i=0;$i<$N;$i++){$e0[$i]=$f0[$i]=0;}
for($i=0;$i<$N;$i++){$e0[$N+$i]=$f0[$N+$i]=1;}
my @e=(0);
my @f=(0);
permute {
for($i=0;$i<$N*2;$i++){$e[$i+1]=$e[$i]+$_[$i];}
permute {
for($i=0;$i<$N*2;$i++){
$f[$i+1]=$f[$i]+$_[$i];
if($e[$i]==$f[$i]&&$e[$i+1]==$f[$i+1]){last;}
}
if($i==$N*2){$r++;}
1;
} @f0;
1;
} @e0;
print $r."\n";
41 changes: 41 additions & 0 deletions tyama_codeiq608_iter.py
@@ -0,0 +1,41 @@
#!/usr/bin/python
#coding:utf-8
import sys
if sys.version_info[0]>=3: from functools import reduce

def find(cond,a):
for e in a:
if cond(e): return e
return None
def permutation(x,n=None):
if n==None: n=len(x)
if n<0 or len(x)<n: return
a=sorted(x)
yield tuple(a[0:n])
while True:
a=a[0:n]+a[len(a)-1:n-1:-1]
k=find(lambda i: a[i]<a[i+1], range(len(a)-2,-1,-1))
if k==None: break
l=find(lambda i: a[k]<a[i], range(len(a)-1,k,-1))
a[k],a[l]=a[l],a[k]
a=a[0:k+1]+a[len(a)-1:k:-1]
yield tuple(a[0:n])

N=6
e0=[0]*N+[1]*N
f0=[0]*N+[1]*N
#各Pは経路を表す。1が縦、0が横を表す。
r=0
i=0
e=[0]*(N*2+1)
f=[0]*(N*2+1)
for _e in permutation(e0):
for i in range(N*2): e[i+1]=e[i]+_e[i]
#e=reduce(lambda s,_: (s.append(s[-1:][0]+_),s)[1],e0,[0])
#数学の座標系のように左下をA、右上をBとすると、eの各インデックスiにおいて、x座標がi-e[i]、y座標がe[i]となる。
for _f in permutation(f0):
for i in range(N*2): f[i+1]=f[i]+_f[i]
#f=reduce(lambda s,_: (s.append(s[-1:][0]+_),s)[1],f0,[0])
if all(e[i]!=f[i] or e[i+1]!=f[i+1] for i in range(N*2)): r+=1
#i番目の座標とi+1番目の座標が等しければ、「道に重複がある」とみなせる。
print(r) # 100360
46 changes: 46 additions & 0 deletions tyama_codeiq608_iter.rb
@@ -0,0 +1,46 @@
#!/usr/bin/ruby
class Array
def permutation2(n=self.size)
return to_enum(:permutation2,n) unless block_given?
return if n<0||self.size<n
a=self.sort
yield a[0,n]
while true
a=a[0,n]+a[n..-1].reverse
k=(a.size-2).downto(0).find{|i|a[i]<a[i+1]}
break if !k
l=(a.size-1).downto(k+1).find{|i|a[k]<a[i]}
a[k],a[l]=a[l],a[k]
a=a[0,k+1]+a[k+1..-1].reverse
yield a[0,n]
end
end
def partial_sum
=begin
s=[0]
0.step(self.size-1){|i|s<<s[i]+self[i]}
s
=end
self.reduce([0]){|s,e|s<<s.last+e}
end
end
N=6
P=([0]*N+[1]*N).permutation2.to_a # N=5のとき、permutation.to_a.uniqの70倍速
#各Pは経路を表す。1が縦、0が横を表す。
r=0
i=0
e=[0]*(N*2+1)
f=[0]*(N*2+1)
P.each{|e0|
(N*2).times{|i|e[i+1]=e[i]+e0[i]}
#数学の座標系のように左下をA、右上をBとすると、eの各インデックスiにおいて、x座標がi-e[i]、y座標がe[i]となる。
P.each{|f0|
r+=1 if (N*2).times.none?{|i|
f[i+1]=f[i]+f0[i]
#i番目の座標とi+1番目の座標が等しければ、「道に重複がある」とみなせる。
e[i]==f[i] && e[i+1]==f[i+1]
}
}
}
p r # 100360

0 comments on commit 9eab68d

Please sign in to comment.