Advertisement
Guest User

2015 Day 6 P1

a guest
Nov 21st, 2022
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.16 KB | Source Code | 0 0
  1. with open("input.txt","r") as file:
  2.     cmnds = [ask[:-1] for ask in file.readlines()]
  3.  
  4. lights = [[1 for i in range(1000)] for i in range(1000)]
  5.  
  6. def on(cmnd):
  7.     startx, starty = cmnd[0].split(',')
  8.     endx, endy = cmnd[1].split(',')
  9.  
  10.     print(startx,starty)
  11.    
  12.     startx, starty = int(startx), int(starty)
  13.     endx, endy = int(endx), int(endy)
  14.  
  15.     for i in range(startx,endx+1):
  16.         if i == startx:
  17.             for j in range(1000):
  18.                 if j >= starty:
  19.                     lights[i][j] = 0
  20.         elif i == endx:
  21.             for j in range(1000):
  22.                 if j <= endy:
  23.                     lights[i][j] = 0
  24.         else:
  25.             for j in range(1000):
  26.                 lights[i][j] = 0
  27. def off(cmnd):
  28.     startx, starty = cmnd[0].split(',')
  29.     endx, endy = cmnd[1].split(',')
  30.  
  31.     startx, starty = int(startx), int(starty)
  32.     endx, endy = int(endx), int(endy)
  33.  
  34.     for i in range(startx,endx+1):
  35.         if i == startx:
  36.             for j in range(1000):
  37.                 if j >= starty:
  38.                     lights[i][j] = 1
  39.         elif i == endx:
  40.             for j in range(1000):
  41.                 if j <= endy:
  42.                     lights[i][j] = 1
  43.         else:
  44.             for j in range(1000):
  45.                 lights[i][j] = 1
  46.  
  47. def toggle(cmnd):
  48.     startx, starty = cmnd[0].split(',')
  49.     endx, endy = cmnd[1].split(',')
  50.  
  51.     startx, starty = int(startx), int(starty)
  52.     endx, endy = int(endx), int(endy)
  53.  
  54.     for i in range(startx,endx+1):
  55.         if i == startx:
  56.             for j in range(1000):
  57.                 if j >= starty:
  58.                     if lights[i][j] == 0:
  59.                         lights[i][j] = 1
  60.                     if lights[i][j] == 1:
  61.                         lights[i][j] = 0
  62.         elif i == endx:
  63.             for j in range(1000):
  64.                 if j <= endy:
  65.                     if lights[i][j] == 0:
  66.                         lights[i][j] = 1
  67.                     if lights[i][j] == 1:
  68.                         lights[i][j] = 0
  69.         else:
  70.             for j in range(1000):
  71.                 if lights[i][j] == 0:
  72.                     lights[i][j] = 1
  73.                 if lights[i][j] == 1:
  74.                     lights[i][j] = 0
  75.  
  76. def search_on():
  77.     lights_on = 0
  78.  
  79.     #Searches for lights that are equal to zero and counts them
  80.     for i,row in enumerate(lights):
  81.         for j, light in enumerate(row):
  82.             if lights[i][j] == 0:
  83.                 lights_on += 1
  84.  
  85.     return lights_on
  86.            
  87. for line in cmnds:
  88.     if "turn on" in line:
  89.         on(line.split()[2:3] + line.split()[4:])
  90.     elif "turn off" in line:
  91.         off(line.split()[2:3] + line.split()[4:])
  92.     else:
  93.         toggle(line.split()[1:2] + line.split()[3:])
  94.  
  95. print(search_on())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement