advent of code 2023 day 14 part II

This commit is contained in:
Wouter Groeneveld 2023-12-14 12:58:20 +01:00
parent e82119fc50
commit d3e1f29135
2 changed files with 21 additions and 4 deletions

View File

@ -1,6 +1,23 @@
const tilt = (map) => {
const tilt = (originalMap) => {
// All 'O's should fall from south all the way to north until they hit (a) another one or (b) the bounds.
const theMap = originalMap.map(line => line.split(''))
const executeTilt = () => {
let tilted = false
theMap.forEach((row, rownr) => {
row.forEach((val, colnr) => {
if(val === 'O' && rownr - 1 >= 0 && theMap[rownr - 1][colnr] === '.') {
theMap[rownr][colnr] = '.'
theMap[rownr - 1][colnr] = 'O'
tilted = true
}
})
})
if(tilted) executeTilt() // as long as rocks are rollin', keep on rollin'
}
executeTilt()
return theMap.map(line => line.join(''))
}
const calculateLoad = (map) => {

View File

@ -27,7 +27,8 @@ O..#.OO...
#....###..
#....#....`
assert.equal(expected, tilt(input.split('\n')))
const result = tilt(input.split('\n')).join('\n')
assert.equal(expected, result)
})
test('The total load of the platform calculated is based on the rock positions', (t) => {
@ -45,7 +46,6 @@ O..#.OO...
assert.equal(136, calculateLoad(rolledNorth.split('\n')))
})
/*
test('The result of the input solved from the example is 136', (t) => {
const input = `O....#....
O.OO#....#
@ -60,4 +60,4 @@ O.#..O.#.#
assert.equal(136, solve(input))
})
*/