-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAOC_Day11_part2.py
92 lines (71 loc) · 2.46 KB
/
AOC_Day11_part2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import copy
File = open("Day11_Input.txt", "r")
Lines = File.readlines()
print(Lines)
NewLines = []
#We first add a ring of floor around the seating areas
for i in range(0,len(Lines)):
NewLines.append(list(Lines[i].strip()))
NewLines[i].insert(0,'.')
NewLines[i].append('.')
NewLines.insert(0,['.']*len(NewLines[1]))
NewLines.append(['.']*len(NewLines[1]))
Lines = copy.deepcopy(NewLines)
condition = True
def checkSeats(SeatX,SeatY):
#Direction = N, NE, E, SE, S, SW, W, NW
DirectionX = [0, 1, 1, 1, 0, -1, -1, -1]
DirectionY = [1, 1, 0, -1, -1, -1, 0, 1]
OccupiedInView = 0
for i in range(0,len(DirectionX)):
# print('i',i)
whilecounter = 1
condition = True
while condition == True:
SeatXcheck = SeatX + whilecounter * DirectionX[i]
SeatYcheck = SeatY + whilecounter * DirectionY[i]
# print(SeatX,SeatY,SeatXcheck,SeatYcheck)
if SeatXcheck >= (len(Lines[0])-1) \
or SeatXcheck <= 0 \
or SeatYcheck <= 0 \
or SeatYcheck >= (len(Lines)-1):
# print('limit met')
condition = False
elif Lines[SeatYcheck][SeatXcheck] == "L":
# print('L')
condition = False
elif Lines[SeatYcheck][SeatXcheck] == "#":
OccupiedInView += 1
# print('seat in view')
condition = False
# print('no cond')
whilecounter += 1
# print(OccupiedInView)
return OccupiedInView
while condition == True:
OldChanges = 0
Changes = 0
print("While loop")
NewLines = copy.deepcopy(Lines)
for i in range(1,len(Lines)-1):
print(Lines[i])
for j in range(1,len(Lines[i])-1):
# print(j)
if Lines[i][j] == "L":
# print(checkSeats(i,j))
if checkSeats(j,i) == 0:
NewLines[i][j] = "#"
Changes += 1
if Lines[i][j] == "#":
if checkSeats(j, i) >= 5:
NewLines[i][j] = "L"
Changes += 1
print(OldChanges,Changes)
if OldChanges == Changes:
condition = False
Lines = copy.deepcopy(NewLines)
OldChanges = Changes
Seats = 0
for Line in Lines:
Seats += Line.count("#")
print(Seats)