day 15, trivial
This commit is contained in:
		
							
								
								
									
										12
									
								
								d15/part1.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								d15/part1.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					S=0
 | 
				
			||||||
 | 
					for l in sys.stdin.read().split(','):
 | 
				
			||||||
 | 
					    c = 0
 | 
				
			||||||
 | 
					    for w in l:
 | 
				
			||||||
 | 
					        c += ord(w)
 | 
				
			||||||
 | 
					        c *= 17
 | 
				
			||||||
 | 
					        c = c % 256
 | 
				
			||||||
 | 
					    print(l, c)
 | 
				
			||||||
 | 
					    S += c
 | 
				
			||||||
 | 
					print(S)
 | 
				
			||||||
							
								
								
									
										26
									
								
								d15/part2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								d15/part2.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def hash(l):
 | 
				
			||||||
 | 
					    c = 0
 | 
				
			||||||
 | 
					    for w in l:
 | 
				
			||||||
 | 
					        c += ord(w)
 | 
				
			||||||
 | 
					        c *= 17
 | 
				
			||||||
 | 
					        c = c % 256
 | 
				
			||||||
 | 
					    return c
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					B = [ dict() for _ in range(256) ]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for l in sys.stdin.read().split(','):
 | 
				
			||||||
 | 
					    e = l.find('=')
 | 
				
			||||||
 | 
					    d = l.find('-')
 | 
				
			||||||
 | 
					    i = max(e,d)
 | 
				
			||||||
 | 
					    lbl = l[:i]
 | 
				
			||||||
 | 
					    box = hash(lbl)
 | 
				
			||||||
 | 
					    if e > 0:  # set
 | 
				
			||||||
 | 
					        v = int(l[i+1:])
 | 
				
			||||||
 | 
					        B[box][lbl] = v    
 | 
				
			||||||
 | 
					    elif d > 0:  # remove
 | 
				
			||||||
 | 
					        if lbl in B[box]:
 | 
				
			||||||
 | 
					            del B[box][lbl]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(sum((i+1)*(j+1)*v for i, b in enumerate(B) for j,(k,v) in enumerate(b.items())))
 | 
				
			||||||
		Reference in New Issue
	
	Block a user