Created
March 30, 2022 19:47
-
-
Save tcarreira/20be6bae420db7e319c12075ce48589c to your computer and use it in GitHub Desktop.
Solving AdventOfCode2015 - day 06 - for Dojo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function part1(matrix, instructions) { | |
let newMatrix = matrix.map(row => row.slice()); | |
instructions.forEach(instructionString => { | |
instruction = parseLine(instructionString); | |
newMatrix = changeLights(newMatrix, instruction.operation, instruction.range); | |
}) | |
return newMatrix.reduce((acc, row) => acc + row.reduce((acc, cell) => acc + cell, 0), 0); | |
} | |
function part2(matrix, instructions) { | |
let newMatrix = matrix.map(row => row.slice()); | |
instructions.forEach(instructionString => { | |
instruction = parseLine(instructionString); | |
newMatrix = changeLights2(newMatrix, instruction.operation, instruction.range); | |
}) | |
return newMatrix.reduce((acc, row) => acc + row.reduce((acc, cell) => acc + cell, 0), 0); | |
} | |
function parseLine(s) { | |
const regex = /(turn (on|off)|toggle) (\d+),(\d+) through (\d+),(\d+)/ ; | |
[, operation, , x_i, y_i, x_f, y_f] = s.match(regex); | |
return { | |
operation, | |
range: { | |
start: [parseInt(x_i), parseInt(y_i)], | |
finish: [parseInt(x_f), parseInt(y_f)], | |
} | |
}; | |
} | |
function changeLights(matrix, operation, range) { | |
// deep copy array of array | |
const newMatrix = matrix.map(row => row.slice()); | |
const { start, finish } = range; | |
for (let x = start[0]; x <= finish[0]; x++) { | |
for (let y = start[1]; y <= finish[1]; y++) { | |
if (operation === "turn on") { | |
newMatrix[y][x] = 1; | |
} else if (operation === "turn off") { | |
newMatrix[y][x] = 0; | |
} else if (operation === "toggle") { | |
newMatrix[y][x] = newMatrix[y][x] === 1 ? 0 : 1; | |
} | |
} | |
} | |
return newMatrix | |
} | |
function changeLights2(matrix, operation, range) { | |
// deep copy array of array | |
const newMatrix = matrix.map(row => row.slice()); | |
const { start, finish } = range; | |
for (let x = start[0]; x <= finish[0]; x++) { | |
for (let y = start[1]; y <= finish[1]; y++) { | |
if (operation === "turn on") { | |
newMatrix[y][x] += 1; | |
} else if (operation === "turn off") { | |
newMatrix[y][x] = newMatrix[y][x] > 0 ? newMatrix[y][x] - 1 : 0; | |
} else if (operation === "toggle") { | |
newMatrix[y][x] += 2; | |
} | |
} | |
} | |
return newMatrix | |
} | |
module.exports = {part1, part2, parseLine, changeLights, changeLights2}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require('assert'); | |
const fs = require('fs'); | |
const { part1, part2, parseLine, changeLights, changeLights2 } = require('../src/dojo'); | |
describe('Test parseLine', function () { | |
it('should parse toggle', function () { | |
res = parseLine("turn on 461,550 through 564,900"); | |
expected = { operation: "turn on", range: { start: [461, 550], finish: [564, 900] } }; | |
assert.deepEqual(res, expected); | |
}); | |
it('should parse turn on', function () { | |
res = parseLine("turn on 123,456 through 567,890"); | |
expected = { operation: "turn on", range: { start: [123, 456], finish: [567, 890] } }; | |
assert.deepEqual(res, expected); | |
}); | |
it('should parse turn off', function () { | |
res = parseLine("turn off 0,0 through 0,0"); | |
expected = { operation: "turn off", range: { start: [0, 0], finish: [0, 0] } }; | |
assert.deepEqual(res, expected); | |
}); | |
}); | |
describe('Test changeLights', function () { | |
it('should not modify input', function () { | |
matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; | |
res = changeLights(matrix, "turn on", { start: [0, 0], finish: [2, 0] }); | |
expected = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]; | |
assert.deepEqual(matrix, [[0, 0, 0], [0, 0, 0], [0, 0, 0]]); | |
}); | |
it('should turn on', function () { | |
matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; | |
res = changeLights(matrix, "turn on", { start: [0, 0], finish: [2, 0] }); | |
expected = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]; | |
assert.deepEqual(res, expected); | |
}); | |
it('should turn off', function () { | |
matrix = [[1, 0, 0], [0, 0, 0], [0, 0, 1]]; | |
res = changeLights(matrix, "turn off", { start: [0, 0], finish: [2, 0] }); | |
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 1]]; | |
assert.deepEqual(res, expected); | |
}); | |
it('should toggle', function () { | |
matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]; | |
res = changeLights(matrix, "toggle", { start: [0, 0], finish: [1, 1] }); | |
expected = [[0, 1, 0], [1, 0, 0], [0, 0, 1]]; | |
assert.deepEqual(res, expected); | |
}); | |
}); | |
describe('Test part1', function () { | |
it('should return 5', function () { | |
const matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; | |
const instructions = [ | |
"turn on 0,0 through 2,2", | |
"toggle 0,0 through 2,0", | |
"turn off 1,1 through 1,1", | |
] | |
const res = part1(matrix, instructions); | |
assert.deepEqual(res, 5); | |
}); | |
it('should return solution', function () { | |
if (process.env.ALL_TESTS !== undefined) { | |
const data = fs.readFileSync('input.txt', 'utf8') | |
const instructions = data.split('\n'); | |
const matrix = Array(1000).fill().map(() => Array(1000).fill(0)); | |
const res = part1(matrix, instructions); | |
assert.deepEqual(res, 543903); | |
} else { | |
this.skip(); | |
} | |
}); | |
}); | |
describe('Test changeLights2', function () { | |
it('should not modify input', function () { | |
matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; | |
res = changeLights2(matrix, "turn on", { start: [0, 0], finish: [2, 0] }); | |
expected = [[1, 1, 1], [0, 0, 0], [0, 0, 0]]; | |
assert.deepEqual(matrix, [[0, 0, 0], [0, 0, 0], [0, 0, 0]]); | |
}); | |
it('should turn on', function () { | |
matrix = [[1, 2, 0], [0, 1, 0], [0, 0, 0]]; | |
res = changeLights2(matrix, "turn on", { start: [0, 0], finish: [2, 0] }); | |
expected = [[2, 3, 1], [0, 1, 0], [0, 0, 0]]; | |
assert.deepEqual(res, expected); | |
}); | |
it('should turn off', function () { | |
matrix = [[1, 2, 0], [0, 1, 0], [0, 0, 0]]; | |
res = changeLights2(matrix, "turn off", { start: [0, 0], finish: [2, 0] }); | |
expected = [[0, 1, 0], [0, 1, 0], [0, 0, 0]]; | |
assert.deepEqual(res, expected); | |
}); | |
it('should toggle', function () { | |
matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]; | |
res = changeLights2(matrix, "toggle", { start: [0, 0], finish: [1, 1] }); | |
expected = [[3, 2, 0], [2, 3, 0], [0, 0, 1]]; | |
assert.deepEqual(res, expected); | |
}); | |
}); | |
describe('Test part2', function () { | |
it('should return 5', function () { | |
const matrix = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]; | |
const instructions = [ | |
"turn on 0,0 through 2,2", | |
"toggle 0,0 through 2,0", | |
"turn off 1,1 through 1,1", | |
] | |
// const expectedMatrix = [[3,3,3], [1, 0, 1], [1, 1, 1]]; | |
const res = part2(matrix, instructions); | |
assert.deepEqual(res, 14); | |
}); | |
it('should return solution', function () { | |
if (process.env.ALL_TESTS !== undefined) { | |
const data = fs.readFileSync('input.txt', 'utf8') | |
const instructions = data.split('\n'); | |
const matrix = Array(1000).fill().map(() => Array(1000).fill(0)); | |
const res = part2(matrix, instructions); | |
assert.deepEqual(res, 14687245); | |
} else { | |
this.skip(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment