Quick actions

cmd+k|ctrl+k

Navigation

Languages

aoc2017-day13

Snippet info

Language

Python

Visibility

public

Author

christophe.guillon.perso

Created

2017-12-13T17:40:44Z

Updated

2017-12-13T17:40:44Z

import sys                                                                                                                                                                                                          
import re                                                                                                                                                                                                           
                                                                                                                                                                                                                    
def selfprint(x):                                                                                                                                                                                                   
    print(x)                                                                                                                                                                                                        
    return x                                                                                                                                                                                                        
                                                                                                                                                                                                                    
def one_by_one(inputf):                                                                                                                                                                                             
    return ([int(__)                                                                                                                                                                                                
             for __ in _.split()]                                                                                                                                                                                   
            for _ in                                                                                                                                                                                                
            (re.sub("[:]", " ", _)                                                                                                                                                                                  
             for _ in                                                                                                                                                                                               
             (_.strip()                                                                                                                                                                                             
              for _ in iter(inputf))                                                                                                                                                                                
             if _))                                                                                                                                                                                                 
                                                                                                                                                                                                                    
def scanner_pos(tick, depth):                                                                                                                                                                                       
    x = depth - 1                                                                                                                                                                                                   
    p = x - abs(x - tick % (2*x))                                                                                                                                                                                   
    return p                                                                                                                                                                                                        
                                                                                                                                                                                                                    
def positions(tick, firewalls):                                                                                                                                                                                     
    return ((_, scanner_pos(tick + _[0], _[1])) for _ in firewalls)                                                                                                                                                 
                                                                                                                                                                                                                    
def collisions(tick, firewalls, pos):                                                                                                                                                                               
    return ((f, p == pos) for f, p in positions(tick, firewalls) if p == pos)                                                                                                                                       
                                                                                                                                                                                                                    
def has_next(seq):                                                                                                                                                                                                  
    return next((True for _ in seq), False)                                                                                                                                                                         
                                                                                                                                                                                                                    
def compute_weight(seq):                                                                                                                                                                                            
    return sum((f[0] * f[1] for f,_ in seq))                                                                                                                                                                        
                                                                                                                                                                                                                    
with sys.stdin as f:                                                                                                                                                                                               
    firewalls = list(one_by_one(f))                                                                                                                                                                                 
                                                                                                                                                                                                                    
print(compute_weight(collisions(0, firewalls, 0)))                                
INFO