Skip to content

Instantly share code, notes, and snippets.

@piyushkandpal
Created October 19, 2014 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save piyushkandpal/8eb633aad02b928a4d93 to your computer and use it in GitHub Desktop.
Save piyushkandpal/8eb633aad02b928a4d93 to your computer and use it in GitHub Desktop.
//https://www.hackerrank.com/challenges/the-quickest-way-up Snake and ladder game python solution
# Enter your code here. Read input from STDIN. Print output to STDOUT
T = raw_input()
#print T
jumps = {}
for i in range(int(T)):
L,S = raw_input().split(',')
ladder_dict = { k:v for k,v in (d.split(',') for d in iter(raw_input().split())) }
snake_dict = { k:v for k,v in (d.split(',') for d in iter(raw_input().split())) }
ladder_dict.update(snake_dict)
jumps = ladder_dict
print jumps
final_pos = 100
positions = {0} #initial position off the board
nsteps = 0
while final_pos not in positions:
nsteps += 1
old_positions = positions
positions = set()
for pos in old_positions:
for dice in range(1, 7):
new_pos = pos + dice
positions.add(jumps.get(new_pos, new_pos))
print nsteps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment