importFoundation// 0 1 2 3 4 5 6 7 8 9 10 11 12 13letmountain=[["^","^","^","^","^","^","^","","","","","","",""],// 0["","^","^","^","^","^","^","^","^","^","","","",""],// 1["","","^","^","^","^","^","^","^","^","","","",""],// 2["","","^","^","^","^","^","^","","","","","",""],// 3["","","^","^","^","^","^","^","^","^","^","^","^","^"],// 4["","","^","^","^","^","^","^","^","","","","",""],// 5["","","^","^","^","^","^","","","","","","",""]]// 6// simple check to see if its the selected tile for mountains.funcisMountainTile(tile:String)->Bool{returntile=="^"}// simple helper to check if any mountain tiles remain in the tilesetfunchasMountains(mount:[[String]])->Bool{forrowsinmount{forcolinrows{ifisMountainTile(tile:col){returntrue}}}returnfalse}// simple helper to print out tileset neatlyfuncprintMountain(mount:[[String]]){forrowinmount{print(row)}}/*
isValid
@param mount 2d array
@param row the row of the slot we are looking at
@param col the column of the slot we are looing at
@return returns true if the row & col are within the valid range of the given 2d array.
*/funcisValid(mount:[[String]],row:Int,col:Int)->Bool{return((row>=0||row<mount.count)||(col>=0||col<mount[row].count))}/*
edgeCase
@param mount 2d array of single character strings of either mountain tiles '^' or something else
@param row the row of the slot we are looking at
@param col the column of the slot we are looing at
@return returns true if the row/col is accessible from an edge(left/right/up/down)
*/funcisEdgeCase(mount:[[String]],row:Int,col:Int)->Bool{if!isValid(mount:mount,row:row,col:col){returnfalse}else{if(row==0||row==(mount.count-1))||(col==0||col==(mount[row].count-1)){returntrue// we are definitely on an edge!}elseif(!isMountainTile(tile:mount[row][col+1])||!isMountainTile(tile:mount[row][col-1])){returntrue// non mountain is either in front or behind us}elseif(!isMountainTile(tile:mount[row+1][col])||!isMountainTile(tile:mount[row-1][col])){returntrue// non mountain is either above or below us}}returnfalse}/*
Challenge function BirdMountain
@param 2d array of single character strings, either white space ' ' or mountain '^'
@return returns an Int of the calculated 'Height' of the mountain passed in.
*/funcbirdMountain(mount:[[String]])->Int{varmountTiles=mountvarcachedTiles=mountTilesvarpassNum=0repeat{cachedTiles=mountTilespassNum+=1forrowin0..<mount.count{forcolin0..<mount[row].count{// check every tile to see if its a mountain tile, and if so compare to the cached version to see if its an edge case.if(isMountainTile(tile:mountTiles[row][col])&&isEdgeCase(mount:cachedTiles,row:row,col:col)){mountTiles[row][col]=String(passNum)}}}print("Pass #",passNum)printMountain(mount:mountTiles)}whilehasMountains(mount:mountTiles)returnpassNum}print("Initial Mountain: ")printMountain(mount:mountain)print("\n")print("Height of the mountain is: ",birdMountain(mount:mountain),"\n\n")
Definitely feel like I'm going a little heavy on the looping, but I was having a hard time visualizing how I could map the increasing elevations without keeping a reference copy of the array to make sure I wasn't just blowing through everything on each pass.... Any suggestions for improvements would be appreciated!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Swift Solution:
Output:
Definitely feel like I'm going a little heavy on the looping, but I was having a hard time visualizing how I could map the increasing elevations without keeping a reference copy of the array to make sure I wasn't just blowing through everything on each pass.... Any suggestions for improvements would be appreciated!