Skip to content
Snippets Groups Projects
Commit 2fd7fc33 authored by Mark Jeeninga's avatar Mark Jeeninga
Browse files

Day 10 Mark

parent cc8db7ab
No related branches found
No related tags found
No related merge requests found
heights = input.split("\n").map(x => x.split('').map(Number))
n = heights.length
m = heights[0].length
range = x => [...Array(x).keys()]
isValid = x => (x[0] >= 0 && x[0] < n && x[1] >= 0 && x[1] < m)
trailheads = []
trailends = []
range(n).map(i => range(m).map(j => heights[i][j] == 0 ? trailheads.push([i,j]) : (heights[i][j] == 9 ? trailends.push([i,j]) : undefined)))
neighbours = x => [[x[0]-1,x[1]],[x[0]+1,x[1]],[x[0],x[1]-1],[x[0],x[1]+1]].filter(isValid)
getEntry = x => isValid(x) ? heights[x[0]][x[1]] : undefined
setEntry = (x,y) => isValid(x) ? heights[x[0]][x[1]] = y : undefined
onlyUnique = (v, i, a) => a.map(x => x.toString()).indexOf(v.toString()) === i
output1 = trailheads.reduce((t,x) => {
level = 1
list = [x]
while(level < 10){
newList = []
list.map(y => newList = newList.concat(neighbours(y).filter(z => getEntry(z) == level)))
list = newList
level++
}
return t + trailends.reduce((y,z) => y + list.map(x => x.toString()).includes(z.toString()), 0)
}, 0)
output2 = trailheads.reduce((t,x) => {
level = 1
list = [x]
paths = {}
paths[x.toString()] = 1
while(level < 10){
newList = []
list.map(y => {
neighbourhood = neighbours(y).filter(z => getEntry(z) == level)
newList = newList.concat(neighbourhood)
neighbourhood.map(z => paths[z.toString()] ? paths[z.toString()] += paths[y.toString()] : paths[z.toString()] = paths[y.toString()])
})
list = newList.filter(onlyUnique)
level++
}
return t + trailends.reduce((y,z) => y + (paths[z.toString()] ? paths[z.toString()] : 0),0)
},0)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment