-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler.py
More file actions
1594 lines (1436 loc) · 44.6 KB
/
Copy patheuler.py
File metadata and controls
1594 lines (1436 loc) · 44.6 KB
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
import math
import random
import time
import fractions
from decimal import *
import sys
import e_util
INPUT_DIR = "in_files/"
def e_16():
print sum([int(c) for c in str(2**1000)])
def e_13():
f = open("e_13.in", "r")
line = f.readline()
# 2d array holding last 10 digits per line
# must do it this way since Python has no easy way to make 2d arrays
arr = [[], [], [], [], [], [], [], [], [], []]
while line:
arr[0].append(int(line[49]))
arr[1].append(int(line[48]))
arr[2].append(int(line[47]))
arr[3].append(int(line[46]))
arr[4].append(int(line[45]))
arr[5].append(int(line[44]))
arr[6].append(int(line[43]))
arr[7].append(int(line[42]))
arr[8].append(int(line[41]))
arr[9].append(int(line[40]))
line = f.readline()
carry = 0
result = ""
for place in xrange(10):
sum = carry
for digit in arr[place]:
sum += digit
ones = sum % 10
result = str(ones) + result
carry = int((sum - ones) / 10)
print result
def e_14():
occurence = 0
maxlength = 0
memoi = [0] * 1000000
for curr in xrange(1000000):
length = 1
n = curr
if memoi[n] != 0:
length = memoi[n]
else:
while n > 1:
if n < 1000000 and memoi[n] != 0:
length += memoi[n]
break
if n % 2 == 0:
n = int(n / 2)
else:
n = 3 * n + 1
length += 1
memoi[curr] = length
if length > maxlength:
maxlength = length
occurence = curr
print(str(occurence) + " | " + str(maxlength))
def e_17():
print sum([int(x) for x in str(math.e_util.factorial(100))])
def e_18():
arr = [
[75],
[95, 64],
[17, 47, 82],
[18, 35, 87, 10],
[20, 04, 82, 47, 65],
[19, 01, 23, 75, 03, 34],
[88, 02, 77, 73, 07, 63, 67],
[99, 65, 04, 28, 06, 16, 70, 92],
[41, 41, 26, 56, 83, 40, 80, 70, 33],
[41, 48, 72, 33, 47, 32, 37, 16, 94, 29],
[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14],
[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57],
[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48],
[63, 66, 04, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31],
[04, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 04, 23]]
for row in xrange(1, len(arr)):
arr[row][0] += arr[row - 1][0]
for i in xrange(1, len(arr[row]) - 1):
if arr[row - 1][i - 1] > arr[row - 1][i]:
arr[row][i] += arr[row - 1][i - 1]
else:
arr[row][i] += arr[row - 1][i]
arr[row][len(arr[row]) - 1] += arr[row - 1][len(arr[row - 1]) - 1]
print max(arr[len(arr) - 1])
def e_19():
SUN = 1; MON = 2;TUE = 3;WED = 4; THU = 5; FRI = 6; SAT = 7;
year = 1901
month = 1
day = TUE
sum = 0
while year < 2001:
month = 1
while month <= 12:
if month == 9 or month == 4 or month == 6 or month == 11:
day += 1
if day > SAT:
day = SUN
elif month == 2:
day -= 1
if day < SUN:
day = SAT
else:
if day == SAT:
day = MON
elif day == FRI:
day = SUN
else:
day += 2
if day == SUN:
sum += 1
month += 1
year += 1
print sum
def sum_divisors(n):
sum = 0
for i in xrange(1, int(math.sqrt(n)) + 1):
if n % i == 0:
sum += i
sum += n / i
return sum
def e_21():
n = 10000
visited = []
result = 0
for i in xrange(n):
if i not in visited:
d = sum_divisors(i)
if sum_divisors(d) == i and d != i:
result += i
visited.append(i)
if d < n:
result += d
visited.append(d)
print result
def e_25():
first = 0
sec = 1
n = 1
while len(str(sec)) < 1000:
temp = first
first = sec
sec += temp
n += 1
print n
def find_max_recur(s):
result = ""
# do binary split
right_limit = int(len(s) / 2)
while right_limit > 0:
for start in xrange(len(s) - right_limit * 2 + 1):
substring = s[start : right_limit + start]
if substring == s[right_limit + start : right_limit + start + len(substring)]:
# if 's' contains substring sufficient number of times,
# with some leeway at head and tail
if s.count(substring) > int(len(s) / len(substring)) - 5:
subresult = find_max_recur(substring)
if len(subresult) > 0:
return subresult
else:
return substring
right_limit -= 1
return result
def e_26():
getcontext().prec = 2000
max_d = 0
max_str = ""
start = time.time()
for d in xrange(3, 1000):
dec = str(Decimal(1) / Decimal(d)).split(".")[1]
recurrer = find_max_recur(dec)
if len(max_str) < len(recurrer):
max_d = d
max_str = recurrer
print str(max_d)
def e_27():
max_primes = 0
max_a = 0
max_b = 0
for a in xrange(-999, 1000):
for b in xrange(-999, 1000):
n = 0
while e_util.is_prime(n**2 + a * n + b):
n += 1
if n > max_primes:
max_primes = n
max_a = a
max_b = b
print max_a * max_b
def e_28():
spiral_size = 3
curr = 1
result = 1
while spiral_size <= 1001:
for i in xrange(4):
curr += spiral_size - 1
result += curr
spiral_size += 2
print result
def e_31():
denoms = [1, 2, 5, 10, 20, 50, 100, 200]
def combo_recurr(n, max_denom):
if max_denom == 1:
return 1
remainder = n - max_denom
if remainder == 0:
return 1 + combo_recurr(n, denoms[denoms.index(max_denom) - 1])
if remainder < 0:
return combo_recurr(n, denoms[denoms.index(max_denom) - 1])
return combo_recurr(remainder, max_denom) + combo_recurr(n, denoms[denoms.index(max_denom) - 1])
def combos(n):
return combo_recurr(n, 200)
print combos(200)
def e_35():
def is_circular(n):
n = list(str(n))
for i in xrange(len(n) - 1):
last = n[len(n) - 1]
for c in xrange(len(n) - 2, -1, -1):
n[c + 1] = n[c]
n[0] = last
if e_util.is_prime(int("".join(n))) == False:
return False
return True
primes = e_util.find_primes(1000000)
# count number of circular primes
count = 0
for i in xrange(len(primes)):
if is_circular(primes[i]):
count += 1
print count
def e_36():
total = 0
for n in xrange(1000000):
if e_util.is_palindrome(n) and e_util.is_palindrome("{0:b}".format(n)):
total += n
print total
def e_37():
primes = e_util.find_primes(900000)
result = []
for i in xrange(4, len(primes)):
s = str(primes[i])
# remove from right to left
truncable = True
while len(s) > 1:
s = s[0 : len(s) - 1]
if int(s) not in primes:
truncable = False
break
if truncable:
# from left to right
s = str(primes[i])
while len(s) > 1:
s = s[1 : len(s)]
if int(s) not in primes:
truncable = False
break
if truncable:
result.append(primes[i])
print sum(result)
def e_40():
d_end = 0
d = 1
p = 0
result = 1
while d <= 1000000:
d_beg = d_end + 1
r = 10**p
len_r = len(str(r))
i_beg = 10**p
i_end = 10**(p + 1) - 1
d_end = d_beg + len_r * 9 * r - 1
while d >= d_beg and d <= d_end:
d_cell = (d - d_beg) / len_r
num = i_beg + d_cell
digit = str(num)[(d - d_beg) % len_r]
result *= int(digit)
d *= 10
p += 1
print result
def e_41():
def is_pandigital(x):
x = str(x)
n = len(x)
digits = []
for i in xrange(1, n + 1):
digits.append(str(i))
for i in xrange(len(digits)):
if digits[i] not in x:
return False
return True
primes = e_util.find_primes(99999999)
for i in reversed(primes):
if is_pandigital(i):
print i
break
def e_67():
f = open("triangle.txt", "r")
arr = []
for line in f:
row = [int(num) for num in line.split()]
arr.append(row)
for row in xrange(1, len(arr)):
arr[row][0] += arr[row - 1][0]
for i in xrange(1, len(arr[row]) - 1):
if arr[row - 1][i - 1] > arr[row - 1][i]:
arr[row][i] += arr[row - 1][i - 1]
else:
arr[row][i] += arr[row - 1][i]
arr[row][len(arr[row]) - 1] += arr[row - 1][len(arr[row - 1]) - 1]
print max(arr[len(arr) - 1])
def e_49():
primes = e_util.find_primes(10000)
for i in xrange(0, len(primes) - 3):
for j in xrange(i + 1, len(primes) - 2):
if e_util.is_perm(str(primes[i]), str(primes[j])):
diff = primes[j] - primes[i]
try:
partner = primes.index(primes[j] + diff)
except ValueError:
partner = -1
if partner != -1 and e_util.is_perm(str(primes[j]), str(primes[partner])):
print primes[i], primes[j], primes[partner]
def e_24():
perms = e_util.get_perms("0123456789")
print perms[999999]
def e_30():
result = []
for i in xrange(2, 9999999):
word = str(i)
acc = 0
for c in word:
acc += int(c)**5
if acc == i:
result.append(i)
print sum(result)
def e_29():
all = set(i**j for i in xrange(2, 101) for j in xrange(2, 101))
print len(all)
def e_34():
for i in xrange(3, 99999):
total = sum(e_util.factorial(int(c)) for c in str(i))
if total == i:
print i
# Returns list of abundant numbers up to n
def get_abundants(n):
result = []
for i in xrange(2, n):
if sum(e_util.get_factors(i)) > i:
result.append(i)
return result
def abundant_summable(x, abunds=[]):
if not abunds:
abunds = get_abundants(x)
else:
# get subset of abunds we care about
i = 0
for abund_num in abunds:
if abund_num >= x:
break
i += 1
abunds = abunds[0 : i]
abunds_set = set(abunds)
for abund_num in abunds:
if (x - abund_num) in abunds_set:
return True
return False
def e_23():
total = sum(i for i in xrange(1, 24))
abunds = get_abundants(28124)
for i in xrange(25, 28124):
if not abundant_summable(i, abunds):
total += i
print total
def is_penta(x):
sol = e_util.solve_quad(3, -1, -2 * x)[1]
if sol != None and sol > 0 and sol % 1 == 0:
return True
return False
def is_hexa(x):
sol = e_util.solve_quad(2, -1, -x)[1]
if sol != None and sol > 0 and sol % 1 == 0:
return True
return False
def e_45():
n = 286
tri = n * (n + 1) / 2
while not (is_penta(tri) and is_hexa(tri)):
n += 1
tri = n * (n + 1) / 2
print tri
def get_patterns(s):
"""
Given a string, return a list of patterns where each pattern replaces
occurences of a unique character with one or more asterisks.
"""
result = []
char_count = {}
for c in s:
if c in char_count:
char_count[c] += 1
else:
char_count[c] = 1
for key in char_count:
num_wilds = char_count[key]
if len(char_count) == 1: # don't include patterns of only wildcards, e.g. "****"
num_wilds = char_count[key] - 1
for i in xrange(1, num_wilds + 1): # for every possible number of wildcards
for perm in e_util.get_perms(key * (char_count[key] - i) + "*" * i):
pattern = ""
j = 0
for c in s:
if c == key:
pattern += perm[j]
j += 1
else:
pattern += c
result.append(pattern)
return result
def e_51():
mem = {}
num_digits = 0
FAMILY_TARGET = 8
for prime in e_util.find_primes(1000000):
prime_str = str(prime)
if len(prime_str) > num_digits:
mem = {}
num_digits = len(prime_str)
for pattern in get_patterns(prime_str):
if pattern in mem:
mem[pattern] += 1
if mem[pattern] == FAMILY_TARGET:
print pattern + " " + prime_str
return
else:
mem[pattern] = 1
def e_52():
i = 10
while True:
flag = True
m = 2
for m in xrange(2, 7):
if not e_util.is_perm(str(i), str(i * m)):
flag = False
break
if flag:
print i
break
i += 1
def e_39():
max_sols = 0
max_p = 0
for p in xrange(12, 1001):
sols = 0
a = 1
b = p
while a < b:
d = p - a
b = (d**2 - a**2) / float((2 * d))
a += 1
if b % 1 == 0: # sides are integral length
sols += 1
if sols > max_sols:
max_sols = sols
max_p = p
print max_p
def e_33():
for num in xrange(11, 99):
if str(num)[1] == "0":
continue
for den in xrange(num + 1, 99):
num_s = str(num)
den_s = str(den)
if den_s[1] == "0":
continue
for n_i in xrange(0, len(num_s)):
for d_i in xrange(0, len(den_s)):
if num_s[n_i] == den_s[d_i]:
red_num = num_s[0 : n_i] + num_s[n_i + 1 : len(num_s)]
red_den = den_s[0 : d_i] + den_s[d_i + 1 : len(den_s)]
if float(int(red_num)) / int(red_den) == float(num) / den:
print num_s + " / " + den_s
def e_32():
result = set()
for a in xrange(123, 9876):
a_s = str(a)
b = 0
prod = 1
while len(str(prod)) <= 4:
b += 1
# simple string checks
b_s = str(b)
has_rep = False
for c in a_s:
if c in b_s:
has_rep = True
break
if has_rep:
continue
prod = a * b
if e_util.is_perm(a_s + b_s + str(prod), "123456789"):
result.add(prod)
print sum(result)
def e_53():
total = 0
for n in xrange(23, 101):
for r in xrange(2, n):
combos = e_util.factorial(n) / (e_util.factorial(r) * e_util.factorial(n - r))
if combos > 1000000:
total += 1
print total
def e_56():
max = 0
for a in xrange(1, 100):
for b in xrange(1, 100):
prod = a**b
digit_sum = sum([int(c) for c in str(prod)])
if digit_sum > max:
max = digit_sum
print max
def e_38():
max = 0
for i in xrange(2, 49877):
ct_prd = ""
m = 1
while len(ct_prd) < 9:
ct_prd += str(i * m)
m += 1
if e_util.is_perm(ct_prd, "123456789") and int(ct_prd) > max:
max = int(ct_prd)
print max
def e_55():
result = 0
for i in range (1, 10000):
curr = i
is_lychrel = True
for iterations in xrange(0, 50):
sum = curr + int((str(curr)[ : : -1]))
if e_util.is_palindrome(str(sum)):
is_lychrel = False
break
curr = sum
if is_lychrel:
result += 1
print result
def e_50():
max = 0
max_sum = 0
primes = e_util.find_primes(5000)
prime_set = set(e_util.find_primes(1000000))
for i in xrange(0, len(primes)):
sum = primes[i]
seq_len = 1
for j in xrange(i + 1, len(primes)):
sum += primes[j]
if sum > 1000000:
sum -= primes[j]
break
if sum in prime_set:
seq_len = j - i + 1
if seq_len > max:
max = seq_len
max_sum = sum
print max_sum
def is_triangle(x):
sol = e_util.solve_quad(1, 1, -2 * x)[1]
if sol != None and sol > 0 and sol % 1 == 0:
return True
return False
def is_triangle_word(word):
total = 0
for c in word:
total += ord(c) - 64
return is_triangle(total)
def e_42():
result = 0
f = open(INPUT_DIR + "words.txt", "r")
line = f.readline()
words = line.split(",")
for w in words:
w = w.replace('"', '')
if is_triangle_word(w):
result += 1
print result
def is_sumPrimeAndDouble(x, primes=e_util.find_primes(1000000)):
p = primes[0]
i = 0
while p < x:
sum = 0
base = 1
while sum < x:
sum = p + 2 * base**2
base += 1
if sum == x:
return True
i += 1
p = primes[i]
return False
def e_46():
i = 9
primes = e_util.find_primes(10000)
while True:
if not e_util.is_prime(i) and not is_sumPrimeAndDouble(i, primes):
print i
return
i += 2
def e_47():
i = 1000
T = 4 # find first T consecutive integers with T distinct factors
i_processed = False # optimization to avoid getting prime factors twice
while True:
if i_processed or len(e_util.get_prime_factors(i)) >= T:
seq_len = 1
for j in reversed(xrange(1, T)):
if len(e_util.get_prime_factors(i + j)) >= T:
seq_len += 1
else:
break
if seq_len == T:
print [i + k for k in xrange(0, T)]
return
i += T - seq_len + 1 # skip ahead
if seq_len > 1:
i_processed = True
else:
i_processed = False
else:
i += 1
i_processed = False
def e_43():
sum = 0
primes = e_util.find_primes(18)
for p in e_util.get_perms("0123456789"):
flag = True
for i in xrange(1, 8):
if int(p[i : i + 3]) % primes[i - 1] != 0:
flag = False
break
if flag:
sum += int(p)
print sum
def e_44():
RANGE = 10000 # terms to lookahead for matching pairs
pentas = [n * (3 * n - 1) / 2 for n in xrange(1, 10001)]
lookup_tbl = set(pentas)
min_diff = sys.maxint
for i in xrange(0, len(pentas) - 1):
for j in xrange(i + 1, min(i + RANGE + 1, len(pentas))):
sum = pentas[j] + pentas[i]
diff = pentas[j] - pentas[i]
if sum in lookup_tbl and diff in lookup_tbl:
if diff < min_diff:
min_diff = diff
print min_diff
def e_79():
f = open("keylog.txt", "r")
visited = {}
for line in f:
line = line.strip() # remove newline
for i in xrange(0, len(line)):
c = line[i]
if c not in visited:
visited[c] = []
for k in line[i + 1 : ]:
if k not in visited[c]:
visited[c].append(k)
code = ""
while len(visited) > 0:
for c in visited:
if len(visited[c]) == len(visited) - 1:
code += c
del visited[c]
break
print code
def e_63():
digits = 1
count = 1 # we know 1^x will always be 1 digit, so just count it here
while True:
i = 2
pwrd = str(i**digits)
while len(pwrd) <= digits:
if len(str(pwrd)) == digits:
count += 1
i += 1
pwrd = str(i**digits)
if i == 10 and len(str(9**digits)) < digits:
break
digits += 1
print count
def has_english(filename, threshold=9):
file = open(filename, "r")
vocab = set(['the', 'The', 'and', 'you', 'there', 'have', 'that', 'from'])
found = 0
for line in file:
for word in line.split():
if word in vocab:
found += 1
file.close()
if found >= threshold:
return True
return False
def decrypt(cipher, key, outname=None):
f = open(cipher, "r")
outfile = None
if not outname:
outfile = open(f.name + ".decrypted", "w")
else:
outfile = open(outname, "w")
i = 0
for line in f:
codes = line.split(",")
for code in codes:
dcrptd_byte = int(code)^ord(key[i])
outfile.write(chr(dcrptd_byte))
i += 1
if i == len(key):
i = 0
outfile.close()
f.close()
def e_59():
cipher = INPUT_DIR + "cipher1.txt"
outname = "decrypted.out"
all_letters = ""
done = False
key_len = 3
for c in xrange(97, 97 + 26):
for _ in xrange(0, key_len): # include keys with repeating characters, e.g. 'aaa'
all_letters += chr(c)
for combo in e_util.get_combos(all_letters, key_len):
for key in e_util.get_perms(combo):
decrypt(cipher, key, outname)
if has_english(outname):
print key
return
print "FAILED"
class _RepeatValsResult:
"""
Container for find_repeat_vals result
"""
def __init__(self):
self.is_four_kind = self.is_three_kind = self.is_two_pair = self.is_pair = False
# value of a {3,4}-of-a-kind, pair vals
self.val = self.pair1_val = self.pair2_val = None
self.remainder = []
def find_repeat_vals(hand):
""" Determines if a hand contains pairs, or a {3,4}-of-a-kind """
vals = {} # Map: val -> # of cards with value of 'val' in hand
result = _RepeatValsResult()
for card in hand:
if card.val in vals:
vals[card.val] += 1
else:
vals[card.val] = 1
for val in vals:
if vals[val] == 4:
result.is_four_kind = True
result.val = val
elif vals[val] == 3:
result.is_three_kind = True
result.val = val
elif vals[val] == 2:
if result.pair1_val is None:
result.is_pair = True
result.pair1_val = val
else:
result.is_two_pair = True
result.pair2_val = val
else:
result.remainder.append(val)
return result
class _FindStr8Result:
"""
Container for find_str8() result. Also stores high - card since find_str8() sorts the
hand allowing easy access to highcard.
"""
def __init__(self):
self.high_card = None
self.has_str8 = False
def find_str8(hand):
result = _FindStr8Result()
srted = [card.val for card in hand]
srted.sort()
result.high_card = srted[-1]
for i in xrange(0, len(srted) - 1):
if srted[i] + 1 != srted[i + 1]:
return result
result.has_str8 = True
return result
def has_flush(hand):
suit = hand[0].suit
for i in xrange(1, len(hand)):
if hand[i].suit != suit:
return False
return True
class _Card:
def __init__(self, val, suit):
self.val = val
self.suit = suit
def __init__(self, s):
try:
self.val = int(s[0])
except ValueError:
if s[0] == "T":
self.val = 10
elif s[0] == "J":
self.val = 11
elif s[0] == "Q":
self.val = 12
elif s[0] == "K":
self.val = 13
else:
self.val = 14
self.suit = s[1]
def __repr__(self):
return self.suit + " - " + str(self.val)
class _HandRank:
HIGH_CARD, PAIR, TWO_PAIR, THREE_KIND, STR8, FLUSH\
, FULL_HOUSE, FOUR_KIND, STR8_FLUSH = xrange(9)
def print_rank(rank):
ranks = ["HIGH_CARD", "PAIR", "TWO_PAIR", "THREE_KIND", "STR8", "FLUSH"\
, "FULL_HOUSE", "FOUR_KIND", "STR8_FLUSH"]
return ranks[rank]
def rank_hand(hand_info):
rank = _HandRank.HIGH_CARD
rep_result = hand_info.rep_result
str8_result = hand_info.str8_result
is_flush = hand_info.is_flush
if rep_result.is_two_pair:
rank = _HandRank.TWO_PAIR
elif rep_result.is_pair:
rank = _HandRank.PAIR
if rep_result.is_three_kind:
rank = _HandRank.FULL_HOUSE
elif rep_result.is_three_kind:
rank = _HandRank.THREE_KIND
elif rep_result.is_four_kind:
rank = _HandRank.FOUR_KIND
elif str8_result.has_str8:
rank = _HandRank.STR8
if is_flush:
rank = _HandRank.STR8_FLUSH
elif is_flush:
rank = _HandRank.FLUSH
return rank
class _HandInfo:
"""
Wrapper containing all the information you need about a poker hand.
"""
def __init__(self, hand):
self.rep_result = find_repeat_vals(hand)
self.str8_result = find_str8(hand)
self.is_flush = has_flush(hand)
self.rank = rank_hand(self)
def is_winning_hand(h1_info, h2_info):
""" Returns True iff hand 1 is the winning hand """
if h1_info.rank != h2_info.rank:
return h1_info.rank > h2_info.rank
# resolve tie
if h1_info.rank == _HandRank.PAIR or h1_info.rank == _HandRank.TWO_PAIR:
h1_pair_val = max(h1_info.rep_result.pair1_val, h1_info.rep_result.pair2_val)
h2_pair_val = max(h2_info.rep_result.pair1_val, h2_info.rep_result.pair2_val)
if h1_pair_val > h2_pair_val:
return True
elif h1_pair_val < h2_pair_val:
return False
else:
return max(h1_info.rep_result.remainder) > max(h2_info.rep_result.remainder)
elif (h1_info.rank == _HandRank.THREE_KIND or h1_info.rank == _HandRank.FOUR_KIND or\
h1_info.rank == _HandRank.FULL_HOUSE):
if h1_info.rep_result.val > h2_info.rep_result.val:
return True
elif h1_info.rep_result.val < h2_info.rep_result.val:
return False
if h1_info.str8_result.high_card == h2_info.str8_result.high_card:
raise Exception("NO CLEAR WINNER")
return h1_info.str8_result.high_card > h2_info.str8_result.high_card
def e_54():
result = 0
f = open(INPUT_DIR + "poker.txt", "r")
#f = open(INPUT_DIR + "my.test", "r")
for line in f:
cards = line.split()
p1_hand = []
p2_hand = []
# populate hands
for i in xrange(0, 5):
p1_hand.append(_Card(cards[i]))
for i in xrange(5, 10):
p2_hand.append(_Card(cards[i]))
hand_info_1 = _HandInfo(p1_hand)
hand_info_2 = _HandInfo(p2_hand)
# determine winner
if is_winning_hand(hand_info_1, hand_info_2):
result += 1
print result
def e_57():
"""
'acc' represents the fraction we add to 1 to calculate sqrt(2). With each iteration, we can see that
the acc for the next iteration is simply 1 / (2 + previous_acc).