From a7e43c28a30efa91f975079633a8424588d3a905 Mon Sep 17 00:00:00 2001 From: Aelita4 Date: Tue, 13 Dec 2022 00:46:22 +0100 Subject: [PATCH] 12_2 --- day12/index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/day12/index.js b/day12/index.js index 3ada0d8..af4694f 100644 --- a/day12/index.js +++ b/day12/index.js @@ -64,12 +64,9 @@ function BFS(map, visited = [], toCheck = [], endingPos, dimensions) { while(1) { currentPoint = whereFrom[currentPoint] - if(currentPoint === null) break; + if(currentPoint === null) return count; count++; } - - console.log(count) - break; } } } @@ -77,16 +74,16 @@ function BFS(map, visited = [], toCheck = [], endingPos, dimensions) { let index = 0; const heightMap = []; -const start = { x: 0, y: 0 }; +const startingPoints = []; const end = { x: 0, y: 0 }; input.split('\n').forEach(line => { heightMap[index] = [...line.split("")]; + if(line.includes("a")) startingPoints.push({ x: heightMap[index].indexOf("a"), y: index }); if(line.includes("S")) { const startingIndex = heightMap[index].indexOf("S") heightMap[index][startingIndex] = "a"; - start.x = startingIndex; - start.y = index; + startingPoints.push({ x: startingIndex, y: index }); } if(line.includes("E")) { const endingIndex = heightMap[index].indexOf("E") @@ -97,4 +94,10 @@ input.split('\n').forEach(line => { index++; }); -BFS(heightMap, [`${start.x}:${start.y}`], [`${start.x}:${start.y}`], end, { height: index, width: heightMap[0].length }); \ No newline at end of file +const results = [] + +startingPoints.forEach(start => { + results.push(BFS(heightMap, [`${start.x}:${start.y}`], [`${start.x}:${start.y}`], end, { height: index, width: heightMap[0].length })) +}); + +console.log(results.sort((a, b) => a - b)[0]) \ No newline at end of file