Advertisement
Guest User

Untitled

a guest
Dec 11th, 2020
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. from copy import *
  2. FILENAME = open("Jour11.txt",'r')
  3.  
  4. val = []
  5. for l in FILENAME:
  6. if l[-1] == '\n':
  7. val.append(l[:-1])
  8. else:
  9. val.append(l)
  10.  
  11.  
  12. def part1(val):
  13. test = True
  14. voisins = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
  15. ligne = [i for i in range(len(val))]
  16. colonne = [j for j in range(len(val[0]))]
  17. while test:
  18. copie = ['' for j in range(len(val))]
  19. for i in range(len(val)):
  20. for j in range(len(val[0])):
  21. if val[i][j] == '.':
  22. copie[i] += '.'
  23.  
  24. elif val[i][j] == '#':
  25. compt = 0
  26. for l,c in voisins:
  27. if (i-l in ligne) and (j-c in colonne) and (val[i-l][j-c] == '#'):
  28. compt += 1
  29. if compt <4:
  30. copie[i] += '#'
  31. else:
  32. copie[i] += 'L'
  33. else:
  34. vide = True
  35. for l,c in voisins:
  36. if (i-l in ligne) and (j-c in colonne) and (val[i-l][j-c] == '#'):
  37. vide = False
  38. break
  39. if vide:
  40. copie[i] += '#'
  41. else:
  42. copie[i] += 'L'
  43. if val == copie:
  44. test = False
  45. val = deepcopy(copie)
  46. compt = 0
  47. for i in range(len(val)):
  48. for j in range(len(val[0])):
  49. if val[i][j] == '#':
  50. compt += 1
  51. print(compt)
  52. return(None)
  53.  
  54. #part1(val)
  55.  
  56. def part2(val):
  57. test = True
  58. voisins = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
  59. ligne = [i for i in range(len(val))]
  60. colonne = [j for j in range(len(val[0]))]
  61. while test:
  62. copie = ['' for j in range(len(val))]
  63. for i in range(len(val)):
  64. for j in range(len(val[0])):
  65. if val[i][j] == '.':
  66. copie[i] += '.'
  67. elif val[i][j] == '#':
  68. compt = 0
  69. for l,c in voisins:
  70. avance = True
  71. v = 1
  72.  
  73. while avance and v<len(val):
  74. if (i-l*v in ligne) and (j-c*v in colonne) and (val[i-l*v][j-c*v] == '#'):
  75. compt += 1
  76. avance = False
  77. elif (i-l*v in ligne) and (j-c*v in colonne) and (val[i-l*v][j-c*v] == 'L'):
  78. avance = False
  79. else:
  80. v += 1
  81. if compt < 5:
  82. copie[i] += '#'
  83. else:
  84. copie[i] += 'L'
  85. else:
  86. compt = 0
  87. for l,c in voisins:
  88. avance = True
  89. v = 1
  90.  
  91. while avance and v<len(val):
  92. if (i-l*v in ligne) and (j-c*v in colonne) and (val[i-l*v][j-c*v] == '#'):
  93. compt += 1
  94. avance = False
  95. elif (i-l*v in ligne) and (j-c*v in colonne) and (val[i-l*v][j-c*v] == 'L'):
  96. avance = False
  97. v += 1
  98. if compt < 1:
  99. copie[i] += '#'
  100. else:
  101. copie[i] += 'L'
  102. if val == copie:
  103. test = False
  104. val = deepcopy(copie)
  105. compt = 0
  106. for i in range(len(val)):
  107. for j in range(len(val[0])):
  108. if val[i][j] == '#':
  109. compt += 1
  110. print(compt)
  111. return(None)
  112.  
  113.  
  114.  
  115. #part2(val)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement