Day 4: Ceres Search

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • Karmmah@lemmy.world
    link
    fedilink
    arrow-up
    1
    ·
    edit-2
    24 days ago

    Julia

    Had some time to clean up the code today since the solution was quite straight forward after making a plan on how to approach it.

    spoiler
    function readWordSearch(inputFile::String)::Matrix{Char}
    	f = open(inputFile,"r")
    	lines::Vector{String} = readlines(f)
    	close(f)
    	wordSearch = Matrix{Char}(undef,length(lines),length(lines))
    	for (i,line) in enumerate(lines)
    		wordSearch[i,:] = collect(line)
    	end
    	return wordSearch
    end
    
    function countXMASAppearances(wS::Matrix{Char})::Int
    	appearanceCount::Int = 0
    	for i=1 : size(wS)[1] #lines
    		for j=1 : size(wS)[2] #columns
    			wS[i,j]!='X' ? continue : nothing #continue if char is not X
    			#if char is X, check surrounding area
    			# check horizontals
    			#left
    			j>=4 ? (wS[i,j-1]*wS[i,j-2]*wS[i,j-3]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			#right
    			j<=size(wS)[2]-3 ? (wS[i,j+1]*wS[i,j+2]*wS[i,j+3]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			# check verticals
    			#up
    			i>=4 ? (wS[i-1,j]*wS[i-2,j]*wS[i-3,j]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			#down
    			i<=size(wS)[1]-3 ? (wS[i+1,j]*wS[i+2,j]*wS[i+3,j]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			# check diagonals
    			#left up
    			i>=4 && j>=4 ? (wS[i-1,j-1]*wS[i-2,j-2]*wS[i-3,j-3]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			#right up
    			i>=4 && j<=size(wS)[2]-3 ? (wS[i-1,j+1]*wS[i-2,j+2]*wS[i-3,j+3]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			#left down
    			i<=size(wS)[1]-3 && j>=4 ? (wS[i+1,j-1]*wS[i+2,j-2]*wS[i+3,j-3]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    			#right down
    			i<=size(wS)[1]-3 && j<=size(wS)[2]-3 ? (wS[i+1,j+1]*wS[i+2,j+2]*wS[i+3,j+3]=="MAS" ? appearanceCount+=1 : nothing) : nothing
    		end
    	end
    	return appearanceCount
    end
    
    function countX_MASAppearances(wordSearch::Matrix{Char})::Int
    	appearances::Int = 0
    	for l=2 : size(wordSearch)[1]-1
    		for c=2 : size(wordSearch)[2]-1
    			wordSearch[l,c]!='A' ? continue : nothing
    			checkArr = [wordSearch[l-1,c-1],wordSearch[l-1,c+1],wordSearch[l+1,c-1],wordSearch[l+1,c+1]]
    			if checkArr in [['M','M','S','S'],['M','S','M','S'],['S','S','M','M'],['S','M','S','M']]
    				appearances += 1
    			end
    		end
    	end
    	return appearances
    end
    
    wordSearch::Matrix{Char} = readWordSearch(inputFile
    prinltn("part 1 appearances: $(countXMASAppearances(wordSearch))")
    prinltn("part 2 appearances: $(countX_MASAppearances(wordSearch))")