Created
June 2, 2019 07:37
-
-
Save bciccarelli/b070e47814148b885f1f7e13a0c4fd7d to your computer and use it in GitHub Desktop.
Foobar level 3 solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def solution(map): | |
searchMap = [] | |
searchMap.append((0,0,0)) | |
moves = 0 | |
finished = False | |
while not finished: | |
[searchMap, finished] = search(searchMap, map) | |
moves += 1 | |
return moves | |
def search(searchMap, map): | |
temp = [] | |
finished = False | |
for p in searchMap: | |
for xy in [[p[0]-1,p[1]],[p[0]+1,p[1]],[p[0],p[1]-1],[p[0],p[1]+1]]: | |
newP = check(xy[0],xy[1],p,map) | |
temp.append(newP) | |
if(p[0] == len(map[0])-1 and p[1] == len(map)-1): | |
finished = True | |
break | |
return [list(dict.fromkeys(searchMap + temp)), finished] | |
def check(x,y,p,map): | |
if(0<=x<len(map[0]) and 0<=y<len(map)): | |
if(map[y][x] and not p[2] or not map[y][x]): | |
newHasPassed = p[2] or bool(map[y][x]) | |
return (x, y, newHasPassed) | |
return (0,0,0) | |
print("Starting:") | |
print(solution([[0, 1, 1, 0], [0, 0, 0, 1], [1, 1, 0, 0], [1, 1, 1, 0]])) | |
print(solution([[0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0]])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment