first commit
This commit is contained in:
47
03/part1.py
Normal file
47
03/part1.py
Normal file
@ -0,0 +1,47 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
|
||||
def read_file(filename, chunk=1024):
|
||||
file = open(filename, 'r')
|
||||
while True:
|
||||
line = file.read(chunk)
|
||||
if not line:
|
||||
break
|
||||
yield line
|
||||
|
||||
|
||||
def getFloorPlanKey(posX, posY):
|
||||
return "%dx%d" % (posX, posY)
|
||||
|
||||
|
||||
def moveSanta(posX, posY, direction):
|
||||
if direction == '>':
|
||||
posX = posX + 1
|
||||
if direction == '<':
|
||||
posX = posX - 1
|
||||
if direction == '^':
|
||||
posY = posY + 1
|
||||
if direction == 'v':
|
||||
posY = posY - 1
|
||||
return (posX, posY)
|
||||
|
||||
|
||||
def main():
|
||||
posX = 0
|
||||
posY = 0
|
||||
houses = 1
|
||||
floorPlan = dict()
|
||||
floorPlan[getFloorPlanKey(0, 0)] = 1
|
||||
|
||||
for line in read_file('input', 1):
|
||||
(posX, posY) = moveSanta(posX, posY, line[0])
|
||||
key = getFloorPlanKey(posX, posY)
|
||||
|
||||
if key not in floorPlan:
|
||||
houses = houses + 1
|
||||
floorPlan[key] = 0
|
||||
floorPlan[key] = floorPlan[key] + 1
|
||||
print houses
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user