day 14 cleanup
This commit is contained in:
		
							parent
							
								
									35312d30d8
								
							
						
					
					
						commit
						0247ce727a
					
				@ -1,7 +0,0 @@
 | 
				
			|||||||
1.12.......
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
......11.11
 | 
					 | 
				
			||||||
1.1........
 | 
					 | 
				
			||||||
.........1.
 | 
					 | 
				
			||||||
.......1...
 | 
					 | 
				
			||||||
@ -1,53 +0,0 @@
 | 
				
			|||||||
Initial state:
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
..1........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
After 1 second:
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
....1......
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
After 2 seconds:
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
......1....
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
After 3 seconds:
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
........1..
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
After 4 seconds:
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
..........1
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
After 5 seconds:
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
.1.........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
@ -1,7 +0,0 @@
 | 
				
			|||||||
......2..1.
 | 
					 | 
				
			||||||
...........
 | 
					 | 
				
			||||||
1..........
 | 
					 | 
				
			||||||
.11........
 | 
					 | 
				
			||||||
.....1.....
 | 
					 | 
				
			||||||
...12......
 | 
					 | 
				
			||||||
.1....1....
 | 
					 | 
				
			||||||
@ -1,7 +0,0 @@
 | 
				
			|||||||
..... 2..1.
 | 
					 | 
				
			||||||
..... .....
 | 
					 | 
				
			||||||
1.... .....
 | 
					 | 
				
			||||||
           
 | 
					 | 
				
			||||||
..... .....
 | 
					 | 
				
			||||||
...12 .....
 | 
					 | 
				
			||||||
.1... 1....
 | 
					 | 
				
			||||||
							
								
								
									
										67
									
								
								day14/sol.py
									
									
									
									
									
								
							
							
						
						
									
										67
									
								
								day14/sol.py
									
									
									
									
									
								
							@ -9,68 +9,42 @@ def parse(file):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
def solve(file, W, H):
 | 
					def solve(file, W, H):
 | 
				
			||||||
    robots = parse(file)
 | 
					    robots = parse(file)
 | 
				
			||||||
    print(robots)
 | 
					    #print(robots)
 | 
				
			||||||
    robots = [iterate(r, 100, W, H) for r in robots]
 | 
					 | 
				
			||||||
    print(robots)
 | 
					 | 
				
			||||||
    mx = W//2
 | 
					 | 
				
			||||||
    my = H//2
 | 
					 | 
				
			||||||
    quads = {1:[], 2:[], 3:[], 4:[]}
 | 
					 | 
				
			||||||
    print(mx,my)
 | 
					 | 
				
			||||||
    for (x,y),_ in robots:
 | 
					 | 
				
			||||||
        q = 0
 | 
					 | 
				
			||||||
        if x < mx and y < my: q = 1
 | 
					 | 
				
			||||||
        if x > mx and y < my: q = 2
 | 
					 | 
				
			||||||
        if x < mx and y > my: q = 3
 | 
					 | 
				
			||||||
        if x > mx and y > my: q = 4
 | 
					 | 
				
			||||||
        if q > 0:
 | 
					 | 
				
			||||||
            quads[q].append((x,y))
 | 
					 | 
				
			||||||
    print(quads)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    total = 1
 | 
					    robots100 = [iterate(r, 100, W, H) for r in robots]
 | 
				
			||||||
    for v in quads.values():
 | 
					    #print(robots100)
 | 
				
			||||||
        total *= len(v)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    print(total)
 | 
					    q = quad_score(robots100, W, H)
 | 
				
			||||||
 | 
					    print(q)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def solve2(file, W, H):
 | 
					    minq = float('inf')
 | 
				
			||||||
    robots = parse(file)
 | 
					    mint = 0
 | 
				
			||||||
    mq = float('inf')
 | 
					 | 
				
			||||||
    for t in range(0, W*H):
 | 
					    for t in range(0, W*H):
 | 
				
			||||||
        rs = lambda: (iterate(r, t, W, H) for r in robots)
 | 
					        rs = (iterate(r, t, W, H) for r in robots)
 | 
				
			||||||
        q = quads(rs(), W, H)
 | 
					        q = quad_score(rs, W, H)
 | 
				
			||||||
        qs = q[0]*q[1]*q[2]*q[3]
 | 
					        if minq > q:
 | 
				
			||||||
        if mq > qs:
 | 
					            minq = q
 | 
				
			||||||
            mq = qs
 | 
					            mint = t
 | 
				
			||||||
        if qs == mq:
 | 
					
 | 
				
			||||||
    picture = [["."]*W for _ in range(H)]
 | 
					    picture = [["."]*W for _ in range(H)]
 | 
				
			||||||
            for (x,y),_ in rs():
 | 
					    for r in robots:
 | 
				
			||||||
                if y < H:
 | 
					        (x,y),_ = iterate(r, mint, W, H)
 | 
				
			||||||
        picture[y][x] = "#"
 | 
					        picture[y][x] = "#"
 | 
				
			||||||
    for line in picture:
 | 
					    for line in picture:
 | 
				
			||||||
        print("".join(line))
 | 
					        print("".join(line))
 | 
				
			||||||
            print("t = ", t, "q = ", qs)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        #input(">")
 | 
					    print("t = ", mint, "q = ", minq)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        # 442?
 | 
					def quad_score(robots, W, H):
 | 
				
			||||||
        # 493
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def quads(robots, W, H):
 | 
					 | 
				
			||||||
    mx = W//2
 | 
					    mx = W//2
 | 
				
			||||||
    my = H//2
 | 
					    my = H//2
 | 
				
			||||||
    q1, q2, q3, q4 = 0, 0, 0, 0
 | 
					    q1, q2, q3, q4 = 0, 0, 0, 0
 | 
				
			||||||
    #seen = set()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (x,y),_ in robots:
 | 
					    for (x,y),_ in robots:
 | 
				
			||||||
        #if (x,y) in seen:
 | 
					 | 
				
			||||||
        #    continue
 | 
					 | 
				
			||||||
        #seen.add((x,y))
 | 
					 | 
				
			||||||
        if x < mx and y < my: q1 += 1
 | 
					        if x < mx and y < my: q1 += 1
 | 
				
			||||||
        if x > mx and y < my: q2 += 1
 | 
					        if x > mx and y < my: q2 += 1
 | 
				
			||||||
        if x < mx and y > my: q3 += 1
 | 
					        if x < mx and y > my: q3 += 1
 | 
				
			||||||
        if x > mx and y > my: q4 += 1
 | 
					        if x > mx and y > my: q4 += 1
 | 
				
			||||||
    return [q1, q2, q3, q4]
 | 
					    return q1*q2*q3*q4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def iterate(robot, t, W, H):
 | 
					def iterate(robot, t, W, H):
 | 
				
			||||||
    (px,py), (vx,vy) = robot
 | 
					    (px,py), (vx,vy) = robot
 | 
				
			||||||
@ -78,6 +52,5 @@ def iterate(robot, t, W, H):
 | 
				
			|||||||
    py = (py+vy*t)%H
 | 
					    py = (py+vy*t)%H
 | 
				
			||||||
    return (px,py), (vx,vy)
 | 
					    return (px,py), (vx,vy)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#solve(open("sample1.in"), 11, 7)
 | 
					solve(open("sample1.in"), 11, 7)
 | 
				
			||||||
#solve(open("input"), 101, 103)
 | 
					solve(open("input"), 101, 103)
 | 
				
			||||||
solve2(open("input"), 101, 103)
 | 
					 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user