-
Notifications
You must be signed in to change notification settings - Fork 0
/
d8.py
185 lines (161 loc) · 4.55 KB
/
d8.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import sys
import time
# Declare variables
display = ['..................................................',
'..................................................',
'..................................................',
'..................................................',
'..................................................',
'..................................................']
instructions = []
# Read instructions from file
def readInput():
# fileObj = open('d8_example.txt', 'r')
fileObj = open('d8_input.txt', 'r')
for line in fileObj:
if line != '\n':
instructions.append(line.strip())
# Print text on specific position
def print_there(x, y, text):
sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text))
sys.stdout.flush()
# Prints the current display
def getDisplay():
header = "==[ Easter Bunny HQ ]=============================\n"
print_there(4, 5, header)
cr=5
for row in display:
print_there(cr,5,row)
cr=cr+1
print_there(11,5, "==================================================")
# Turn on a single pixel at x:y - b; 1: On, 0: Off
def pix(x, y, b):
row = display[y]
cx=0
updatedRow = ""
# print "updating x: " + str(x) + " y: " + str(y) + " setting pixel on: " + str(b)
while cx-1 < x:
if cx == x:
if b == 1:
updatedRow = updatedRow + "#" + str(row[cx+1:])
elif b == 0:
updatedRow = updatedRow + "." + str(row[cx+1:])
else:
updatedRow = updatedRow + str(row[cx:cx+1]) + str(row[cx+1:])
else:
updatedRow = updatedRow + row[cx:cx+1]
cx=cx+1
display[y] = updatedRow
# Create a small rectangle. Param ex. 3x2
def rect(param):
param = param.split('x')
x = int(param[0])
y = int(param[1])
cx=0
cy=0
while cy < y:
# print " y : " + str(cy)
while cx < x:
# print " x : " + str(cx)
# Turn on a pixel at position cx:cy
pix(cx, cy, 1)
cx=cx+1
cx=0
cy=cy+1
# Returns the state of a pixel; 0 = off, 1 = on
def getPix(x, y):
state = 0
row = display[y]
cx=0
pix = ""
# print "updating x: " + str(x) + " y: " + str(y) + " setting pixel on: " + str(b)
while cx-1 < x:
if cx == x:
pix = str(row[cx:cx+1])
cx=cx+1
if pix == "#":
state = 1
elif pix == ".":
state = 0
else:
print(' - error in getPix()-function -')
return state
# Rotates the array input by (step) number of steps
def rotateArray(arr, step):
arrIn = arr
arrLen = len(arr)
cs = 0
ca = 0
# Iterate number of steps to shift
while cs < step:
result = []
# Iterate number of array positions
while ca < arrLen:
# If the first array position - take the last one
if ca == 0:
result.append(arrIn[arrLen-1])
else:
result.append(arrIn[ca-1])
ca=ca+1
ca=0
arrIn = result
cs=cs+1
return result
# Rotates the column (x) down by (p) pixels
def rotateCol(x, p):
# Get column
col = []
cr=0
for row in display:
col.append(getPix(x,cr))
cr=cr+1
# Shift column-array
col = rotateArray(col,p)
# Update display with new array
colLen = len(col)
cl=0
while cl < colLen:
pix(x,cl,col[cl])
cl=cl+1
# Rotates the row (y) by (p) pixels
def rotateRow(y, p):
# Get row
row = []
rowLen = len(display[y])
cl=0
while cl < rowLen:
row.append(getPix(cl,y))
cl=cl+1
# Shift row-array
row = rotateArray(row,p)
# Update display with new array
cl=0
while cl < rowLen:
pix(cl,y,row[cl])
cl=cl+1
# Main program
readInput()
for instr in instructions:
iSplit = instr.split(' ')
if iSplit[0] == 'rect':
rect(iSplit[1])
elif iSplit[0] == 'rotate' and iSplit[1] == 'column':
x = int(iSplit[2].split('=')[1])
p = int(iSplit[4])
rotateCol(x, p)
elif iSplit[0] == 'rotate' and iSplit[1] == 'row':
y = int(iSplit[2].split('=')[1])
p = int(iSplit[4])
rotateRow(y, p)
getDisplay()
time.sleep(0.05)
# Count lit pixels
litPixels = 0
rc = 20
for row in display:
litPixels = litPixels + int(str(row).count('#'))
# text = str(row) + " --- pixels on: " + str(litPixels)
# print_there(rc,5,text)
rc += 1
text = "Pixels lit: " + str(litPixels)
print_there(12, 5, text)