Advertisement
Guest User

AoC Day 3

a guest
Dec 10th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. '''
  2. SIDE = 1000
  3. FORMAT: (ID @ LEFT,RIGHT: XxY)
  4. Part 1
  5. How many square inches of fabric are within two or more claims?
  6. Solution:
  7. Create 1000 arrays with 1000 indices
  8. Loop through each entry of the input.txt to record the positions.
  9. For each use, add 1 to the array indice.
  10. e.g. 5,5 5,5 the spaces filled would be 6=left+1 to 11=left +5, 6=top+1 to 11=top+5
  11. in the actual array: the first would be array[6][6] to array[11][11]
  12. @ space1 number comma number colon space2 number x number
  13. '''
  14.  
  15. def values():
  16.     checker = 0
  17.     at = 0
  18.     comma = 0
  19.     colon = 0
  20.     x = 0
  21.     values = [] #array of [left, top, x, y]
  22.     for id, line in enumerate(open('input.txt')):
  23.         #1353 @ 743,209: 14x21
  24.         for indice, char in enumerate(line):
  25.             if char == '@': at = indice
  26.             if char == ',': comma = indice
  27.             if char == ':': colon = indice
  28.             if char == 'x':
  29.                 x = indice
  30.                 values.append([int(line[at+2:comma]), int(line[comma+1:colon]), int(line[colon+2:x]), int(line[x+1:-1])])
  31.                 if id == checker:
  32.                     #print(values)
  33.                     checker += 1 #so array can check for values
  34.     return values
  35.  
  36. def createArray():
  37.     subarray = []
  38.     for i in range(1000): subarray.append(0)
  39.     array = []
  40.     for i in range(1000): array.append(subarray)
  41.     return array
  42.  
  43. def markArrays():
  44.     array = createArray()
  45.     value = values()
  46.     for c, coords in enumerate(value):
  47.         for i in range(coords[1], coords[1] + coords[3] + 1):
  48.         #problem starts from 1,1 so range gets aoc values, then i and j are adjusted below
  49.             for j in range(coords[0], coords[0] + coords[2] + 1):
  50.                 if array[i - 1][j - 1] == 1:
  51.                     array[i - 1][j - 1] = 'X'
  52.                 else:
  53.                     array[i - 1][j - 1] = 1
  54.                 print(c, i, j, array[i - 1][j - 1])
  55.                 #xarr = array[i - 1], xind = xarr[j - 1], to enter arry, go to y array (which has to be 1 less than i bc adjusting coords->array), then x indice
  56.     return array
  57.  
  58. def counter(array):
  59.     counter = 0
  60.     for set in array:
  61.         for value in set:
  62.             if type(value) == str:
  63.                 counter += 1
  64.     return counter
  65. print(markArrays())
  66. print(counter(markArrays()))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement