Added solutions for N=100-110

master
Tibor Bizjak 2023-03-26 17:56:21 +02:00
parent f4b1fbcafb
commit b0d1b49fb4
14 changed files with 1427 additions and 0 deletions

View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
lim = 10**12
x, y = 3, 1
blue = 3
total = 4
while total <= lim:
x, y = 3*x + 8*y, 3*y + x
blue = (2*y + x + 1) // 2
total = (4*y + x + 1) // 2
print(blue)

View File

@ -0,0 +1,27 @@
#!/usr/bin/env python3
CFS = [1, -1]*5 + [1]
def main(cfs):
seq = getseq(cfs)
py = pyramid(seq)
return sum(op(n, py) for n in range(1, len(cfs)))
def getseq(cfs):
return [sum(c*n**pw for pw, c in enumerate(cfs))
for n in range(1, len(cfs))]
def pyramid(seq):
r = [seq]
for i in range(len(seq)-1):
lvl = []
for j in range(len(r[i])-1):
lvl.append(r[i][j+1] - r[i][j])
r.append(lvl)
return r
def op(n, py):
return sum(py[i][n-i-1] for i in range(n))
#print(main([0,0,0,1]))
print(main(CFS))

View File

@ -0,0 +1,23 @@
#!/usr/bin/env python3
# Calculate area of triangle and compare to areas
# of triangles with one vertex = origin
# better solution -> check sign of cross products
def main():
tr = parse("data/102_triangles.txt")
return len([0 for p in tr if areaTR(p) == areaOG(0,2,p) + areaOG(0,4,p) + areaOG(2,4,p)])
def areaTR(p): #triangle area
return abs(p[0]*p[3] - p[0]*p[5] +
p[2]*p[5] - p[2]*p[1] +
p[4]*p[1] - p[4]*p[3])
def areaOG(a, b, p): #triangle with with a=0,0
return abs(p[a]*p[b+1] - p[b]*p[a+1])
def parse(path):
with open(path) as f:
return [list(map(int, row.split(","))) for row in f.read()[:-1].split("\n")]
print(main())

View File

@ -0,0 +1,46 @@
#!/usr/bin/env python3
def test(n, setsums):
newsums = [s+n for s in setsums] + setsums + [n]
if len(newsums) != len(set(newsums)):
return False
return newsums
def search(left, right, length, best, setsums, lor, w): #left to right
if len(left + right) >= length:
if sum(left + right) < best:
#print left+right, sum(left+right)
for i, n in enumerate(left+right):
w[i] = n
return sum(left + right)
return best
hole = length - len(left + right) - 1
minsum = sum(left + right) + (hole*(hole+1))//2 + left[-1]*hole
if lor:
n = max(left[-1]+1, sum(right)-sum(left) + 1)
bound = right[0] - hole
else:
n = left[-1] + hole + 1
bound = best - minsum
if len(right) > 0 and right[0] < bound:
bound = right[0]
while n < bound and n < best - minsum - (n-left[-1])*hole*lor:
newsums = test(n, setsums)
if newsums:
best = search(left + [n]*lor,
[n]*(not lor) + right,
length, best, newsums, not lor, w)
n += 1
return best
def base(length, best):
x = length//2 + length % 2
n = (x + 1 - length%2) * (x-1) + 1
n = 20
w = [0]*length
while n < best - (length-1)*length - n*(length-1):
best = search([n], [], length, best, [n], False, w)
n += 1
return "".join(map(str, w)), best
print(base(7, 500)[0])

View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
from math import sqrt, log10
def main():
k = 1
m = 0
last = 1
gr = (1 + sqrt(5)) / 2
first = gr*(1/sqrt(5))
while cond(str(int(first))[:9], str(last)):
last, m = (last+m)%10**9, last
first = gr*first
if first >= 10**9:
first /= 10
k += 1
return k, int(log10(gr)*k - log10(1/sqrt(5)) + 1/2.0)
def cond(first, last):
a = len(set(last)) != 9
b = len(set(first)) != 9
c = '0' in first+last
return a or b or c
print(main()[0])

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
def test(s):
if len(s) != len(set(s)):
return False
s.sort()
if sum(s[:len(s)//2 + len(s)%2]) <= sum(s[len(s)//2+1:]):
return False
setsums = []
for n in s:
setsums += [m+n for m in setsums] + [n]
if len(setsums) != len(set(setsums)):
return False
return True
with open("data/105_sets.txt") as f:
r = f.read()
sets = [list(map(int, line.split(','))) for line in r.split('\n')[:-1]]
print(sum(sum(s) for s in sets if test(s)))

View File

@ -0,0 +1,17 @@
#!/usr/bin/env python3
from math import factorial
def bin(n, k):
if n < 0:
return 0
if n < k:
return 0
return factorial(n) // (factorial(n-k) * factorial(k))
n = 12
ms = 0
for k in range(0, n//2 +1):
ms += bin(n, 2*k) * bin(2*k-1, k+1)
print(ms)

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
def parse(fname):
with open(fname) as f:
r = f.read()
r = [line.split(',') for line in r.split('\n')[:-1]]
weights = {a : [(b, int(w)) for b, w in enumerate(line)
if w!='-'
]
for a, line in enumerate(r)
}
return weights
def search(network, visited={0}):
if len(visited) == len(network):
return 0
edges = [(n2, w) for node in visited
for n2, w in network[node]
if n2 not in visited
]
next, w = min(edges, key = lambda x: x[1])
return w + search(network, visited | {next})
network = parse("data/107_network.txt")
total_weight = sum(w for node in network
for _, w in network[node])
total_weight //= 2
min_weight = search(network)
print(total_weight - min_weight)

View File

@ -0,0 +1,54 @@
#!/usr/bin/env python3
from lib import primegen
from math import log
from operator import mul
from functools import reduce
def calc(pows):
return (reduce(mul, (2*p + 1 for p in pows)) + 1) // 2
def increment(i, pows):
n = 0
if i < len(pows):
n = pows[i]
return [n + 1] * (i+1) + pows[i+1:]
def val(pows):
return sum(p*v for p, v in zip(pows, vals))
def fpwof2(pows, n):
if len(pows) == 1:
return n-1
r = (2*n - 1) // reduce(mul, (2*p + 1 for p in pows[1:]))
r = (r - 1) // 2
while calc([r] + pows[1:]) < n:
r += 1
return r
N = 10 ** 3
primes = list(primegen(100))
vals = [log(p) / log(2) for p in primes]
i = 0
pows = [0]
best = fpwof2(pows, N)
best_pows = None
while not (val(pows) >= best and sum(pows) == len(pows)):
if not i:
pows[0] = fpwof2(pows, N)
if len(pows) > 1 and pows[0] < pows[1]:
pows[0] = pows[1]
if val(pows) < best:
best = val(pows)
best_pows = pows
i = 1
pows = increment(i, pows)
if val(pows) >= best:
i += 1
else:
i = 0
print(reduce(mul, (n**pw for n, pw in zip(primes, best_pows))))

24
109_darts.py 100644
View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
bull = [25]
miss = [0]
fields = list(range(1, 21))
single = fields + bull
double = [2*i for i in single]
treble =[3*i for i in fields]
regs = miss + single + double + treble
regs.sort()
count = 0
score = 100
for fst in double:
for i, sec in enumerate(regs):
if fst + 2*sec >= score:
break
for thd in regs[i:]:
if fst + sec + thd >= score:
break
count += 1
print(count)

File diff suppressed because it is too large Load Diff

100
data/105_sets.txt 100644
View File

@ -0,0 +1,100 @@
81,88,75,42,87,84,86,65
157,150,164,119,79,159,161,139,158
673,465,569,603,629,592,584,300,601,599,600
90,85,83,84,65,87,76,46
165,168,169,190,162,85,176,167,127
224,275,278,249,277,279,289,295,139
354,370,362,384,359,324,360,180,350,270
599,595,557,298,448,596,577,667,597,588,602
175,199,137,88,187,173,168,171,174
93,187,196,144,185,178,186,202,182
157,155,81,158,119,176,152,167,159
184,165,159,166,163,167,174,124,83
1211,1212,1287,605,1208,1189,1060,1216,1243,1200,908,1210
339,299,153,305,282,304,313,306,302,228
94,104,63,112,80,84,93,96
41,88,82,85,61,74,83,81
90,67,84,83,82,97,86,41
299,303,151,301,291,302,307,377,333,280
55,40,48,44,25,42,41
1038,1188,1255,1184,594,890,1173,1151,1186,1203,1187,1195
76,132,133,144,135,99,128,154
77,46,108,81,85,84,93,83
624,596,391,605,529,610,607,568,604,603,453
83,167,166,189,163,174,160,165,133
308,281,389,292,346,303,302,304,300,173
593,1151,1187,1184,890,1040,1173,1186,1195,1255,1188,1203
68,46,64,33,60,58,65
65,43,88,87,86,99,93,90
83,78,107,48,84,87,96,85
1188,1173,1256,1038,1187,1151,890,1186,1184,1203,594,1195
302,324,280,296,294,160,367,298,264,299
521,760,682,687,646,664,342,698,692,686,672
56,95,86,97,96,89,108,120
344,356,262,343,340,382,337,175,361,330
47,44,42,27,41,40,37
139,155,161,158,118,166,154,156,78
118,157,164,158,161,79,139,150,159
299,292,371,150,300,301,281,303,306,262
85,77,86,84,44,88,91,67
88,85,84,44,65,91,76,86
138,141,127,96,136,154,135,76
292,308,302,346,300,324,304,305,238,166
354,342,341,257,348,343,345,321,170,301
84,178,168,167,131,170,193,166,162
686,701,706,673,694,687,652,343,683,606,518
295,293,301,367,296,279,297,263,323,159
1038,1184,593,890,1188,1173,1187,1186,1195,1150,1203,1255
343,364,388,402,191,383,382,385,288,374
1187,1036,1183,591,1184,1175,888,1197,1182,1219,1115,1167
151,291,307,303,345,238,299,323,301,302
140,151,143,138,99,69,131,137
29,44,42,59,41,36,40
348,329,343,344,338,315,169,359,375,271
48,39,34,37,50,40,41
593,445,595,558,662,602,591,297,610,580,594
686,651,681,342,541,687,691,707,604,675,699
180,99,189,166,194,188,144,187,199
321,349,335,343,377,176,265,356,344,332
1151,1255,1195,1173,1184,1186,1188,1187,1203,593,1038,891
90,88,100,83,62,113,80,89
308,303,238,300,151,304,324,293,346,302
59,38,50,41,42,35,40
352,366,174,355,344,265,343,310,338,331
91,89,93,90,117,85,60,106
146,186,166,175,202,92,184,183,189
82,67,96,44,80,79,88,76
54,50,58,66,31,61,64
343,266,344,172,308,336,364,350,359,333
88,49,87,82,90,98,86,115
20,47,49,51,54,48,40
159,79,177,158,157,152,155,167,118
1219,1183,1182,1115,1035,1186,591,1197,1167,887,1184,1175
611,518,693,343,704,667,686,682,677,687,725
607,599,634,305,677,604,603,580,452,605,591
682,686,635,675,692,730,687,342,517,658,695
662,296,573,598,592,584,553,593,595,443,591
180,185,186,199,187,210,93,177,149
197,136,179,185,156,182,180,178,99
271,298,218,279,285,282,280,238,140
1187,1151,890,593,1194,1188,1184,1173,1038,1186,1255,1203
169,161,177,192,130,165,84,167,168
50,42,43,41,66,39,36
590,669,604,579,448,599,560,299,601,597,598
174,191,206,179,184,142,177,180,90
298,299,297,306,164,285,374,269,329,295
181,172,162,138,170,195,86,169,168
1184,1197,591,1182,1186,889,1167,1219,1183,1033,1115,1175
644,695,691,679,667,687,340,681,770,686,517
606,524,592,576,628,593,591,584,296,444,595
94,127,154,138,135,74,136,141
179,168,172,178,177,89,198,186,137
302,299,291,300,298,149,260,305,280,370
678,517,670,686,682,768,687,648,342,692,702
302,290,304,376,333,303,306,298,279,153
95,102,109,54,96,75,85,97
150,154,146,78,152,151,162,173,119
150,143,157,152,184,112,154,151,132
36,41,54,40,25,44,42
37,48,34,59,39,41,40
681,603,638,611,584,303,454,607,606,605,596

View File

@ -0,0 +1,40 @@
-,-,-,427,668,495,377,678,-,177,-,-,870,-,869,624,300,609,131,-,251,-,-,-,856,221,514,-,591,762,182,56,-,884,412,273,636,-,-,774
-,-,262,-,-,508,472,799,-,956,578,363,940,143,-,162,122,910,-,729,802,941,922,573,531,539,667,607,-,920,-,-,315,649,937,-,185,102,636,289
-,262,-,-,926,-,958,158,647,47,621,264,81,-,402,813,649,386,252,391,264,637,349,-,-,-,108,-,727,225,578,699,-,898,294,-,575,168,432,833
427,-,-,-,366,-,-,635,-,32,962,468,893,854,718,427,448,916,258,-,760,909,529,311,404,-,-,588,680,875,-,615,-,409,758,221,-,-,76,257
668,-,926,366,-,-,-,250,268,-,503,944,-,677,-,727,793,457,981,191,-,-,-,351,969,925,987,328,282,589,-,873,477,-,-,19,450,-,-,-
495,508,-,-,-,-,-,765,711,819,305,302,926,-,-,582,-,861,-,683,293,-,-,66,-,27,-,-,290,-,786,-,554,817,33,-,54,506,386,381
377,472,958,-,-,-,-,-,-,120,42,-,134,219,457,639,538,374,-,-,-,966,-,-,-,-,-,449,120,797,358,232,550,-,305,997,662,744,686,239
678,799,158,635,250,765,-,-,-,35,-,106,385,652,160,-,890,812,605,953,-,-,-,79,-,712,613,312,452,-,978,900,-,901,-,-,225,533,770,722
-,-,647,-,268,711,-,-,-,283,-,172,-,663,236,36,403,286,986,-,-,810,761,574,53,793,-,-,777,330,936,883,286,-,174,-,-,-,828,711
177,956,47,32,-,819,120,35,283,-,50,-,565,36,767,684,344,489,565,-,-,103,810,463,733,665,494,644,863,25,385,-,342,470,-,-,-,730,582,468
-,578,621,962,503,305,42,-,-,50,-,155,519,-,-,256,990,801,154,53,474,650,402,-,-,-,966,-,-,406,989,772,932,7,-,823,391,-,-,933
-,363,264,468,944,302,-,106,172,-,155,-,-,-,380,438,-,41,266,-,-,104,867,609,-,270,861,-,-,165,-,675,250,686,995,366,191,-,433,-
870,940,81,893,-,926,134,385,-,565,519,-,-,313,851,-,-,-,248,220,-,826,359,829,-,234,198,145,409,68,359,-,814,218,186,-,-,929,203,-
-,143,-,854,677,-,219,652,663,36,-,-,313,-,132,-,433,598,-,-,168,870,-,-,-,128,437,-,383,364,966,227,-,-,807,993,-,-,526,17
869,-,402,718,-,-,457,160,236,767,-,380,851,132,-,-,596,903,613,730,-,261,-,142,379,885,89,-,848,258,112,-,900,-,-,818,639,268,600,-
624,162,813,427,727,582,639,-,36,684,256,438,-,-,-,-,539,379,664,561,542,-,999,585,-,-,321,398,-,-,950,68,193,-,697,-,390,588,848,-
300,122,649,448,793,-,538,890,403,344,990,-,-,433,596,539,-,-,73,-,318,-,-,500,-,968,-,291,-,-,765,196,504,757,-,542,-,395,227,148
609,910,386,916,457,861,374,812,286,489,801,41,-,598,903,379,-,-,-,946,136,399,-,941,707,156,757,258,251,-,807,-,-,-,461,501,-,-,616,-
131,-,252,258,981,-,-,605,986,565,154,266,248,-,613,664,73,-,-,686,-,-,575,627,817,282,-,698,398,222,-,649,-,-,-,-,-,654,-,-
-,729,391,-,191,683,-,953,-,-,53,-,220,-,730,561,-,946,686,-,-,389,729,553,304,703,455,857,260,-,991,182,351,477,867,-,-,889,217,853
251,802,264,760,-,293,-,-,-,-,474,-,-,168,-,542,318,136,-,-,-,-,392,-,-,-,267,407,27,651,80,927,-,974,977,-,-,457,117,-
-,941,637,909,-,-,966,-,810,103,650,104,826,870,261,-,-,399,-,389,-,-,-,202,-,-,-,-,867,140,403,962,785,-,511,-,1,-,707,-
-,922,349,529,-,-,-,-,761,810,402,867,359,-,-,999,-,-,575,729,392,-,-,388,939,-,959,-,83,463,361,-,-,512,931,-,224,690,369,-
-,573,-,311,351,66,-,79,574,463,-,609,829,-,142,585,500,941,627,553,-,202,388,-,164,829,-,620,523,639,936,-,-,490,-,695,-,505,109,-
856,531,-,404,969,-,-,-,53,733,-,-,-,-,379,-,-,707,817,304,-,-,939,164,-,-,616,716,728,-,889,349,-,963,150,447,-,292,586,264
221,539,-,-,925,27,-,712,793,665,-,270,234,128,885,-,968,156,282,703,-,-,-,829,-,-,-,822,-,-,-,736,576,-,697,946,443,-,205,194
514,667,108,-,987,-,-,613,-,494,966,861,198,437,89,321,-,757,-,455,267,-,959,-,616,-,-,-,349,156,339,-,102,790,359,-,439,938,809,260
-,607,-,588,328,-,449,312,-,644,-,-,145,-,-,398,291,258,698,857,407,-,-,620,716,822,-,-,293,486,943,-,779,-,6,880,116,775,-,947
591,-,727,680,282,290,120,452,777,863,-,-,409,383,848,-,-,251,398,260,27,867,83,523,728,-,349,293,-,212,684,505,341,384,9,992,507,48,-,-
762,920,225,875,589,-,797,-,330,25,406,165,68,364,258,-,-,-,222,-,651,140,463,639,-,-,156,486,212,-,-,349,723,-,-,186,-,36,240,752
182,-,578,-,-,786,358,978,936,385,989,-,359,966,112,950,765,807,-,991,80,403,361,936,889,-,339,943,684,-,-,965,302,676,725,-,327,134,-,147
56,-,699,615,873,-,232,900,883,-,772,675,-,227,-,68,196,-,649,182,927,962,-,-,349,736,-,-,505,349,965,-,474,178,833,-,-,555,853,-
-,315,-,-,477,554,550,-,286,342,932,250,814,-,900,193,504,-,-,351,-,785,-,-,-,576,102,779,341,723,302,474,-,689,-,-,-,451,-,-
884,649,898,409,-,817,-,901,-,470,7,686,218,-,-,-,757,-,-,477,974,-,512,490,963,-,790,-,384,-,676,178,689,-,245,596,445,-,-,343
412,937,294,758,-,33,305,-,174,-,-,995,186,807,-,697,-,461,-,867,977,511,931,-,150,697,359,6,9,-,725,833,-,245,-,949,-,270,-,112
273,-,-,221,19,-,997,-,-,-,823,366,-,993,818,-,542,501,-,-,-,-,-,695,447,946,-,880,992,186,-,-,-,596,949,-,91,-,768,273
636,185,575,-,450,54,662,225,-,-,391,191,-,-,639,390,-,-,-,-,-,1,224,-,-,443,439,116,507,-,327,-,-,445,-,91,-,248,-,344
-,102,168,-,-,506,744,533,-,730,-,-,929,-,268,588,395,-,654,889,457,-,690,505,292,-,938,775,48,36,134,555,451,-,270,-,248,-,371,680
-,636,432,76,-,386,686,770,828,582,-,433,203,526,600,848,227,616,-,217,117,707,369,109,586,205,809,-,-,240,-,853,-,-,-,768,-,371,-,540
774,289,833,257,-,381,239,722,711,468,933,-,-,17,-,-,148,-,-,853,-,-,-,-,264,194,260,947,-,752,147,-,-,343,112,273,344,680,540,-

View File

@ -0,0 +1,7 @@
-,16,12,21,-,-,-
16,-,-,17,20,-,-
12,-,-,28,-,31,-
21,17,28,-,18,19,23
-,20,-,18,-,-,11
-,-,31,19,-,-,27
-,-,-,23,11,27,-