-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
10926 lines (9712 loc) · 227 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Created by: Zilog8
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
namespace ScratchPad1
{
class Euler
{
public static void run()
{
}
}
class Program
{
public static void Main(string[] args)
{
Euler185.run();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
class Euler282try1
{
public static void run()
{
int split = 1475789056;
calcCache<point,zint> cc = new standardCalcCache<point,zint>(new ackermannCCOBJ());
zint total = new zint(0);
point[] pp = new point[]{new point(1,0), new point(2,2), new point(3,4)};
foreach(point p in pp)
{
Console.Write(p + " = ");
Console.WriteLine(cc.getVal(p));
}
for(int i=0; i<7; i++)
total+=cc.getVal(new point(i,i));
Console.WriteLine(total);
}
public struct point : IComparable<point>
{
public zint m;
public zint n;
public point (int m, int n)
{
this.m = new zint(m);
this.n = new zint(n);
}
public point(zint m, zint n)
{
this.m = m;
this.n = n;
}
public override bool Equals(object obj)
{
point o = (point)obj;
return (this.m==o.m&&this.n==o.n);
}
public override int GetHashCode()
{
return m.GetHashCode()^n.GetHashCode();
}
public override string ToString()
{
return "(M,N): (" + m.ToString() + "," + n.ToString() + ")";
}
#region IComparable<Euler282.point> implementation
public int CompareTo (point other)
{
int retVal = other.m.CompareTo(this.m);
if(retVal==0)
retVal = other.n.CompareTo(this.n);
return retVal;
}
#endregion
}
public class ackermannCCOBJ : ccObj<point,zint>
{
public zint getVal(point x, calcCache<point,zint> c)
{
if(c.cacheSize()>200000000)
c.partialTrim();
if(x.m==0)
return x.n+1;
else if(x.n==0)
return c.getVal(new point(x.m-1,new zint(1)));
else
return c.getVal(new point(x.m-1, c.getVal(new point(x.m,x.n-1))));
}
}
}
class Euler282
{
public static void run()
{
int split = 1475789056;
zint total = new zint(0);
ackermann cc = new ackermann();
point[] pp = new point[]{new point(1,0), new point(2,2), new point(3,4), new point(4,1)};
foreach(point p in pp)
{
Console.Write(p + " = ");
Console.WriteLine(cc.getVal(p));
}
Console.WriteLine("2,7,125,65533");
for(int i=0; i<7; i++)
{
total+=cc.getVal(new point(i,i));
total.remainder(split);
}
Console.WriteLine(total);
}
public class ackerNode
{
public zint m;
public zint n;
public int cost;
public ackerNode parent;
public int status;
//Status code:
//1: solved, answer is in m
//0: not worked on yet
//-1: waiting on an immediate child
//-2: waiting on an intermediate child
public ackerNode(zint m, zint n, ackerNode parent)
{
this.m = m;
this.n = n;
status = 0;
this.parent = parent;
}
public ackerNode(point x, ackerNode parent)
{
m = x.m;
n = x.n;
status = 0;
this.parent = parent;
}
}
public struct point : IComparable<point>
{
public zint m;
public zint n;
public long cost;
public point (int m, int n)
{
this.m = new zint(m);
this.n = new zint(n);
cost = 0;
}
public point(zint m, zint n)
{
this.m = m;
this.n = n;
cost = 0;
}
public point(zint m, zint n, int cost)
{
this.m = m;
this.n = n;
this.cost = cost;
}
public override bool Equals(object obj)
{
point o = (point)obj;
return (this.m==o.m&&this.n==o.n);
}
public override int GetHashCode()
{
return m.GetHashCode()^n.GetHashCode();
}
public override string ToString()
{
return "(M,N): (" + m.ToString() + "," + n.ToString() + ")";
}
#region IComparable<Euler282.point> implementation
public int CompareTo (point other)
{
return other.cost.CompareTo(cost);
}
#endregion
}
public class ackermann
{
public Dictionary<point, zint> zintCache;
int cacheCostCutoff = 9;
int maxCache = 13000000;
int minCache = 10000;
zint bufferValue;
int bufferCost;
public ackermann()
{
zintCache = new Dictionary<point, zint>();
}
public bool TryGetVal(point x, out zint retVal)
{
if(x.m==0)
{
retVal = x.n+1;
return true;
}
if(zintCache.TryGetValue(x,out retVal))
return true;
return false;
}
public zint getVal(point x)
{
ackerNode currNode = new ackerNode(x, null);
while(currNode!=null)
{
currNode = work(currNode);
}
return bufferValue;
}
public void addToCache(zint m, zint n, int cost, zint val)
{
//if(cost>cacheCostCutoff)
{
if(zintCache.Count<maxCache || zintCache.Count<minCache)
{
point p = new point(m, n, cost);
if(!zintCache.ContainsKey(p))
zintCache.Add(p, val);
}
else
standardCalcCache<point,zint>.keepQuarterOfCache(zintCache);
}
}
public ackerNode work(ackerNode currNode)
{
switch (currNode.status) {
case 0: //Not worked on till now, time to clasify problem
zint retVal;
if(TryGetVal(new point(currNode.m,currNode.n),out retVal))
{
bufferValue = retVal;
bufferCost = 0;
return currNode.parent;
}
else
{
if(currNode.n==0)
{
currNode.m -=1;
currNode.n = new zint(1);
return currNode;
}
else
{
maxCache--;
currNode.status = -2;
return new ackerNode(currNode.m, currNode.n-1, currNode);
}
}
break;
case -2: //Solved the intermidiate child, time to put in the real child.
addToCache(currNode.m, currNode.n-1, bufferCost, bufferValue);
currNode.status = 0;
currNode.cost += 1 + bufferCost;
currNode.m-=1;
currNode.n = bufferValue;
return currNode;
break;
default:
throw new Exception("Shouldn't be possible!");
break;
}
}
}
}
class Euler261
{
static int MaxM = 1500;
public static void run()
{
//This is the stuff that has to be loaded from disk on startup for resume
state now = lib.fileCache<state>("Euler261.state", state.initial);
if(now.m==0)
{
now.m++;
Console.WriteLine("Starting from scratch, m = 1");
now.pivots = firstRun();
lib.fileCacheSave<state>("Euler261.state", now);
printPivotSum(now.pivots);
}
while(now.m<MaxM)
{
now.m++;
Console.WriteLine("Starting m = " + now.m.ToString());
subsequentRun(now);
lib.fileCacheSave<state>("Euler261.state", now);
printPivotSum(now.pivots);
}
}
public static void subsequentRun(state now)
{
SqrSum rightSide = new SqrSum(now.m);
SqrSum leftSide = new SqrSum(now.m+1);
while(leftSide.currIndex<10000000000ul)
{
BigInt2 leftVal = leftSide.getNext();
BigInt2 rightVal = rightSide.currVal;
while(leftVal>rightVal)
rightVal = rightSide.getNext();
if(leftVal==rightVal && leftSide.currIndex<rightSide.currIndex-(ulong)(now.m-1))
now.pivots.Add(leftSide.currIndex);
}
}
// k^2 + (k+1)^2 + ... (k+m)^2 == (n+1)^2 + ... (n+m)^2
public static HashSet<ulong> firstRun()
{
HashSet<ulong> pivots = new HashSet<ulong>();
lib.SqrGen2 rightSide = new lib.SqrGen2(1);
SqrSum leftSide = new SqrSum(2);
while(leftSide.currIndex<10000000000ul)
{
BigInt2 leftVal = leftSide.getNext();
BigInt2 rightVal = rightSide.currValue;
while(leftVal>rightVal)
rightVal = rightSide.getNext();
if(leftVal==rightVal)
pivots.Add(leftSide.currIndex);
}
return pivots;
}
public class SqrSum
{
BigInt2[] cache;
int cacheIndex;
lib.SqrGen2 genIndex;
public BigInt2 currVal;
public ulong currIndex
{
get{ return genIndex.currIndex;}
}
public SqrSum(int m)
{
cache = new BigInt2[m];
cacheIndex = 0;
genIndex = new lib.SqrGen2();
currVal = new BigInt2();
for(int i=0;i<m-1;i++)
getNext();
}
public BigInt2 getNext()
{
currVal-=cache[cacheIndex];
cache[cacheIndex] = genIndex.getNext();
currVal+=cache[cacheIndex];
cacheIndex++;
cacheIndex %= cache.Length;
return currVal;
}
}
public static void printPivotSum(HashSet<ulong> pivots)
{
BigInt2 totalSum = new BigInt2(0);
foreach(ulong l in pivots)
{
totalSum+=l;
}
Console.WriteLine("Total = " + totalSum.ToString());
}
public static void printPivots(HashSet<ulong> pivots)
{
BigInt2 totalSum = new BigInt2(0);
foreach(ulong l in pivots)
{
Console.Write(l.ToString()+",");
totalSum+=l;
}
Console.WriteLine();
Console.WriteLine("Total = " + totalSum.ToString());
}
[Serializable]
public class state
{
public int m;
public HashSet<ulong> pivots;
public static state initial()
{
state r = new state();
r.m = 0;
r.pivots = new HashSet<ulong>();
return r;
}
}
}
class Euler261new
{
public static void run()
{
int chunkSize = 10;
ulong maxIndex = 1000;
Unit.all = new Unit[chunkSize];
Unit.results = new HashSet<ulong>();
Unit.offset = 0;
Unit.all[0].FirstSet();
}
private struct Unit
{
public static ulong offset;
public static Unit[] all;
public static HashSet<ulong> results;
lib.SqrGen2 lowEnd;
lib.SqrGen2 highEnd;
public BigInt2 currVal;
public void FirstSet()
{
lowEnd = new ScratchPad1.lib.SqrGen2(0);
}
public void SetNext(int nextIndex)
{
}
}
}
class Euler249
{
//strategy:
//Think of it as a recursive knap-sack problem
//maxPossible = sum of all primes 2..4999
//for each prime P up to maxPossible:
// getNumSolutions(P, 1 + math.min(index of P, index of 4999)) //getNumSolutions(spaceLeftInSack,index of cealing prime value)
// this function calls itself somehow to get its answer:
// val += getNumSolutions(arg[0]-prime[arg[1]-1], arg[1]-1);
// val += getNumSolutions(arg[0], arg[1]-1);
//Possible speedups: short-circuit if spaceLeftInSack>sum of all primes up to prime[celingindex-1]
public static void run()
{
runProb();
}
public static void runProb()
{
int[] pA = lib.allPrimesBelow(5000);
int[] pAsums = new int[pA.Length];
int total = 0;
for(int i=0; i<pA.Length; i++)
{
total+=pA[i];
pAsums[i] = total;
}
recursiveKnapSack.primesArray = pA;
//recursiveKnapSack.primesSet = new HashSet<int>(pA);
recursiveKnapSack.primeSums = pAsums;
//TODO: Implement a weakReference-based cache.
//Idea: a dictionary of the strong, plus a costum hashmap (using the hash of the key) of 10-1000 weakReferenced dictionaries.
var cc = new weakReferenceCache<tuple,limitedzint>(new recursiveKnapSack());
int[] sacks = lib.allPrimesBelow(total+1);
limitedzint result = new limitedzint(0,2);
for(int i=0; i<sacks.Length;i++)
{
if((i<<25)==0)
{
Console.Clear();
Console.WriteLine(i.ToString() + " / " + sacks.Length.ToString());
}
int sackSize = sacks[i];
result.addTo(cc.getVal(new tuple(sackSize, 1 + Math.Min(i,pA.Length-1))));
}
Console.WriteLine(result);
}
public static void runTest()
{
int[] pA = lib.allPrimesBelow(5000);
int[] pAsums = new int[pA.Length];
int total = 0;
for(int i=0; i<pA.Length; i++)
{
total+=pA[i];
pAsums[i] = total;
}
recursiveKnapSack.primesArray = pA;
//recursiveKnapSack.primesSet = new HashSet<int>(pA);
recursiveKnapSack.primeSums = pAsums;
var cc = new standardCalcCache<tuple,limitedzint>(new recursiveKnapSack());
int[] sacks = lib.allPrimesBelow(total+1);
limitedzint result = new limitedzint(0,2);
result.addTo(cc.getVal(new tuple(21, 1 + Math.Min(18,pA.Length-1))));
Console.WriteLine(result);
}
class recursiveKnapSack : ccObj<tuple, limitedzint>
{
// public static HashSet<int> primesSet;
public static int[] primesArray;
public static int[] primeSums;
public limitedzint getVal(Euler249.tuple x, calcCache<Euler249.tuple, limitedzint> c)
{
limitedzint res = new limitedzint(0,2);
int nextIndex = x.indexOfCeiling-1;
if(x.spaceLeft < primeSums[nextIndex] && nextIndex>0)
res.addTo(c.getVal(new tuple(x.spaceLeft, nextIndex)));
int calc = x.spaceLeft-primesArray[nextIndex];
int nextPsum = int.MinValue;
if(nextIndex>0)
nextPsum = primeSums[nextIndex-1];
if(calc==0 || calc == nextPsum)
res.increment();
else if(calc>0 && calc < nextPsum)
res.addTo(c.getVal(new tuple(calc, nextIndex)));
return res;
}
}
private static void calcNumberOfSets()
{
Factorial.pfc = lib.primeFactorCountForAllNumsBelow(5000);
Console.WriteLine(Euler093.nCk(9,5));
Console.WriteLine(nCk(9,5));
int[] pA = lib.allPrimesBelow(5000);
zint b = new zint(0);
for(int i=1; i<=pA.Length; i++)
b+=nCk(pA.Length,i);
Console.WriteLine(b);
}
private struct tuple : IComparable<tuple>
{
public int spaceLeft;
public int indexOfCeiling;
public tuple(int a, int b)
{
this.spaceLeft = a;
this.indexOfCeiling = b;
}
public override bool Equals(object obj)
{
tuple o = (tuple)obj;
return (this.spaceLeft==o.spaceLeft&&this.indexOfCeiling==o.indexOfCeiling);
}
public override int GetHashCode()
{
return (spaceLeft<<10)^indexOfCeiling;;
}
public int CompareTo(tuple other)
{
return other.indexOfCeiling-this.indexOfCeiling;
}
}
public static zint nCk(int n, int k)
{
return (((new Factorial(n))/(new Factorial(k)))/(new Factorial(n-k))).getValue();
}
public class Factorial
{
public static int[][] pfc;
int[] factorCount;
private Factorial()
{}
public Factorial(int n)
{
if(n<2)
factorCount = new int[]{};
else
{
List<int> l = new List<int>();
for(int i=2;i<=n; i++)
{
while(l.Count<pfc[i].Length)
l.Add(0);
for(int ii=2; ii<pfc[i].Length; ii++)
l[ii]+=pfc[i][ii];
}
while(l[l.Count-1]==0)
l.RemoveAt(l.Count-1);
factorCount = l.ToArray();
}
}
public static Factorial operator / (Factorial a, Factorial b)
{
Factorial retVal = new Factorial();
List<int> l = new List<int>(a.factorCount.Length);
l.Add(0); l.Add(0);
for(int i=2; i<a.factorCount.Length;i++)
{
l.Add(a.factorCount[i]);
if(i<b.factorCount.Length)
{
l[i]-=b.factorCount[i];
}
}
while(l.Count>0 && l[l.Count-1]==0)
l.RemoveAt(l.Count-1);
retVal.factorCount = l.ToArray();
return retVal;
}
public zint getValue()
{
zint b = new zint(1);
for(int i=2; i<factorCount.Length; i++)
{
for(int x = factorCount[i];x>0;x--)
b*=i;
}
return b;
}
}
}
class Euler244
{
public static void run()
{
HashSet<int> olderGen = new HashSet<int>();
List<state> prevGen = new List<state>(new state[]{state.start});
long result = 0;
while(result==0)
result = calc(olderGen, ref prevGen);
Console.WriteLine(result);
}
public static long calc(HashSet<int> olderGen, ref List<state> prevGen)
{
List<state> newGen = new List<state>();
HashSet<int> newGenH = new HashSet<int>();
long tot = 0;
foreach(state s in prevGen)
{
if(s.canGoDown)
{
state sd = s.Down;
int sdh = sd.GetHashCode();
if(!olderGen.Contains(sdh))
{
if(sdh==23130)
tot+=sd.checksum;
else
{
newGen.Add(sd);
newGenH.Add(sdh);
}
}
}
if(s.canGoUp)
{
state sd = s.Up;
int sdh = sd.GetHashCode();
if(!olderGen.Contains(sdh))
{
if(sdh==23130)
tot+=sd.checksum;
else
{
newGen.Add(sd);
newGenH.Add(sdh);
}
}
}
if(s.canGoLeft)
{
state sd = s.Left;
int sdh = sd.GetHashCode();
if(!olderGen.Contains(sdh))
{
if(sdh==23130)
tot+=sd.checksum;
else
{
newGen.Add(sd);
newGenH.Add(sdh);
}
}
}
if(s.canGoRight)
{
state sd = s.Right;
int sdh = sd.GetHashCode();
if(!olderGen.Contains(sdh))
{
if(sdh==23130)
tot+=sd.checksum;
else
{
newGen.Add(sd);
newGenH.Add(sdh);
}
}
}
}
olderGen.UnionWith(newGenH);
prevGen = newGen;
return tot;
}
public class state : IEquatable<state>
{ //index = x + y*4
BitArray board; //false if red, true if blue, empty is false
int slider; //coordinates of the empty slot, in x+y*4
public int checksum;
byte last;
public static state start
{
get{
state s = new state();
s.checksum = 0;
s.slider = 0;
s.last = 255;
s.board = new BitArray(16);
for(int x = 2; x<4; x++)
for(int y = 0; y<4; y++)
s.board[x+4*y] = true;
return s;
}
}
public static state end
{
get{
state s = new state();
s.checksum = 0;
s.slider = 0;
s.last = 255;
s.board = new BitArray(16);
for(int x = 0; x<4; x++)
for(int y = 0; y<4; y++)
s.board[x+4*y] = (x%2==1)^(y%2==1);
return s;
}
}
private static int calcChecksum(byte move, int lastChecksum)
{
long l = Math.BigMul(lastChecksum, 243);
l+=move;
return (int)(l%100000007);
}
public bool canGoLeft
{
get{
if(slider%4==3 || last == 82)
return false;
else
return true;
}
}
public state Left
{
get{
BitArray b = new BitArray(board);
int slid = slider+1;
b[slider] = b[slid];
b[slid] = false;
state mool = new state();
mool.board = b;
mool.slider = slid;
mool.last = 76;
mool.checksum = calcChecksum(76, checksum);
return mool;
}
}
public bool canGoRight
{
get{
if(slider%4==0 || last == 76)
return false;
else
return true;
}
}
public state Right
{
get{
BitArray b = new BitArray(board);
int slid = slider-1;
b[slider] = b[slid];
b[slid] = false;
state mool = new state();
mool.board = b;
mool.slider = slid;
mool.last = 82;
mool.checksum = calcChecksum(82, checksum);
return mool;
}
}
public bool canGoUp
{
get{
if(slider/4==3 || last == 68)
return false;
else
return true;
}
}
public state Up
{
get{
BitArray b = new BitArray(board);
int slid = slider+4;
b[slider] = b[slid];
b[slid] = false;
state mool = new state();
mool.board = b;
mool.slider = slid;
mool.last = 85;
mool.checksum = calcChecksum(85, checksum);
return mool;
}
}
public bool canGoDown
{
get{
if(slider/4==0 || last == 85)
return false;
else
return true;
}
}
public state Down
{
get{
BitArray b = new BitArray(board);
int slid = slider-4;
b[slider] = b[slid];
b[slid] = false;
state mool = new state();
mool.board = b;
mool.slider = slid;
mool.last = 68;
mool.checksum = calcChecksum(68, checksum);
return mool;
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
int y = slider/4;
int x = slider-y*4;
sb.AppendLine("Checksum: " + checksum.ToString() + "; Last: " + last.ToString()
+ "; Slider: (" + x.ToString() + "," + y.ToString() + ") ; Hash: " +
GetHashCode().ToString()); //Convert.ToString(GetHashCode(),2));
for(int iy=0; iy<16; iy+=4)
{
for(int ix=0; ix<4; ix++)
{
int val = ix+iy;
if(val==slider)
sb.Append(" ");
else if(board[val])
sb.Append("B");
else
sb.Append("R");
}
sb.AppendLine();
}
return sb.ToString();
}
public override int GetHashCode()
{
int hash = slider;
int i=16;
while(i>0)
{
hash<<=1;
if(board[--i])
hash++;
}
return hash;
}
public static bool operator == (state a, state b)
{
if(a.slider!=b.slider)
return false;
int i = a.board.Length;
while(i>0)
if(a.board[--i]!=b.board[i])
return false;
return true;
}
public static bool operator != (state a, state b)
{
return !(a==b);
}
public override bool Equals(object obj)
{
return this==(state)obj;
}
public bool Equals(state other)
{
return this==other;
}
}
}
class Euler233
{