day 9, pure compute, good candidate for compier:
* python: 2.525 s ± 0.169 s * pypy: 175.0 ms ± 8.9 ms 14x * codon: 29.3 ms ± 7.9 ms 87x
This commit is contained in:
		
							
								
								
									
										36
									
								
								d09/part1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								d09/part1.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,36 @@
 | 
				
			|||||||
 | 
					import sys, os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					I = list(map(int, sys.stdin.read().strip()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def sum_n(a, b):
 | 
				
			||||||
 | 
					    return (b-a+1)*(a+b)//2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					I = I
 | 
				
			||||||
 | 
					j = 0
 | 
				
			||||||
 | 
					b = 0
 | 
				
			||||||
 | 
					res = 0
 | 
				
			||||||
 | 
					id = len(I)//2  # last ID
 | 
				
			||||||
 | 
					x = I[len(I)-1]
 | 
				
			||||||
 | 
					for i in range(len(I)//2):
 | 
				
			||||||
 | 
					    if len(I)-2*j-1 - 2*i == 0:
 | 
				
			||||||
 | 
					        res += sum_n(b, b+x-1)*i
 | 
				
			||||||
 | 
					        break
 | 
				
			||||||
 | 
					    nb = b + I[2*i]
 | 
				
			||||||
 | 
					    res += sum_n(b, nb-1)*i
 | 
				
			||||||
 | 
					    b = nb
 | 
				
			||||||
 | 
					    space = I[2*i+1]
 | 
				
			||||||
 | 
					    while space > 0:
 | 
				
			||||||
 | 
					        if x > space:
 | 
				
			||||||
 | 
					            nb = b + space
 | 
				
			||||||
 | 
					            res += sum_n(b, nb-1)*id
 | 
				
			||||||
 | 
					            x -= space
 | 
				
			||||||
 | 
					            space = 0
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            nb = b + x
 | 
				
			||||||
 | 
					            res += sum_n(b, nb-1)*id
 | 
				
			||||||
 | 
					            id -= 1
 | 
				
			||||||
 | 
					            space -= x
 | 
				
			||||||
 | 
					            j += 1
 | 
				
			||||||
 | 
					            x = I[len(I)-2*j-1]
 | 
				
			||||||
 | 
					        b = nb
 | 
				
			||||||
 | 
					print(res)
 | 
				
			||||||
							
								
								
									
										30
									
								
								d09/part2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								d09/part2.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
				
			|||||||
 | 
					import sys, os
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					I = list(map(int, sys.stdin.read().strip()))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def sum_n(a, b):
 | 
				
			||||||
 | 
					    return (b-a+1)*(a+b)//2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					idxs = []
 | 
				
			||||||
 | 
					b = 0
 | 
				
			||||||
 | 
					for i in I:
 | 
				
			||||||
 | 
					    idxs.append(b)
 | 
				
			||||||
 | 
					    b += i
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					res = 0
 | 
				
			||||||
 | 
					id = len(I)//2  # last id
 | 
				
			||||||
 | 
					for j in range(len(I)-1,-1,-2):  # backward
 | 
				
			||||||
 | 
					    fsize = I[j]
 | 
				
			||||||
 | 
					    for i in range(1, j, 2):  # find free space forward
 | 
				
			||||||
 | 
					        if I[i] >= fsize: # enough free space
 | 
				
			||||||
 | 
					            b = idxs[i]
 | 
				
			||||||
 | 
					            res += sum_n(b, b+fsize-1)*id
 | 
				
			||||||
 | 
					            idxs[i] += fsize
 | 
				
			||||||
 | 
					            I[i] -= fsize
 | 
				
			||||||
 | 
					            break
 | 
				
			||||||
 | 
					    else:  # block too big stays where it is
 | 
				
			||||||
 | 
					        b = idxs[j]
 | 
				
			||||||
 | 
					        res += sum_n(b, b+fsize-1)*id
 | 
				
			||||||
 | 
					    id -= 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(res)
 | 
				
			||||||
		Reference in New Issue
	
	Block a user