Skip to content

Instantly share code, notes, and snippets.

@codevbus
Created February 23, 2016 07:04
Show Gist options
  • Save codevbus/820fc1518269200a8e9a to your computer and use it in GitHub Desktop.
Save codevbus/820fc1518269200a8e9a to your computer and use it in GitHub Desktop.
#!/bin/python
switchFile = 'switches.txt'
with open(switchFile, 'r') as f:
lines = f.readlines()
numSwitch = int(lines[0])
switches = range(0, numSwitch)
switchGrid = {switch: 'off' for switch in switches}
def getrange(l):
x, y = [int(x) for x in l.split()]
if y > x:
return range(x, y)
else:
return range(y, x)
r = map(getrange, lines[1:])
for x in r:
for i in list(x):
if switchGrid[i] == 'off':
switchGrid[i] = 'on'
else:
switchGrid[i] = 'off'
def countSwitch():
onswitch = 0
for i in switchGrid:
if switchGrid[i] == 'on':
onswitch += 1
print(onswitch)
countSwitch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment