Skip to content

Instantly share code, notes, and snippets.

@pauloricardokoch
Created March 31, 2022 23:09
Show Gist options
  • Save pauloricardokoch/e169ca3fcdb94d3bc0829e6e348e1bf4 to your computer and use it in GitHub Desktop.
Save pauloricardokoch/e169ca3fcdb94d3bc0829e6e348e1bf4 to your computer and use it in GitHub Desktop.
Day 6 - Advent of Code (part one)
const regex = /(turn (on|off)|toggle) (\d+),(\d+) through (\d+),(\d+)/
const operations = []
operations['toggle'] = (light) => Number(!light)
operations['turn on'] = (light) => 1
operations['turn off'] = (light) => 0
const parseLine = (s) => {
let [, operation, , x_start, y_start, x_finish, y_finish] = s.match(regex)
return {
operation,
range: {
start: [parseInt(x_start), parseInt(y_start)],
finish: [parseInt(x_finish), parseInt(y_finish)],
}
};
}
const initLights = () => {
return [...Array(1000)].map(() => Array(1000).fill(0))
}
const changeLights = (lights, operation, range) => {
const [sx, sy] = range.start
const [fx, fy] = range.finish
for (x = sx; x <= fx; x++) {
for (y = sy; y <= fy; y++) {
lights[x][y] = operations[operation](lights[x][y])
}
}
return lights
}
const countLitLights = (lights) => {
const reducer = arr => arr.reduce((prev, current) => prev + current, 0)
return reducer(lights.map(reducer))
}
const main = (instructions) => {
let lights = initLights()
instructions = instructions
.split('\n')
.filter(line => regex.test(line)) // filter out invalid instructions
.forEach(inst => {
const action = parseLine(inst)
lights = changeLights(lights, action.operation, action.range)
});
return countLitLights(lights)
}
module.exports = {
parseLine: parseLine,
initLights: initLights,
changeLights: changeLights,
countLitLights: countLitLights,
main: main,
};
var assert = require('assert')
var { parseLine, initLights, changeLights, countLitLights, main } = require('../src/dojo')
describe('Test parseLine', () => {
const tests = [
{
input: 'toggle 461,550 through 564,900',
operation: 'toggle',
range: {
start: [461, 550],
finish: [564, 900]
}
},
{
input: 'turn off 370,39 through 425,839',
operation: 'turn off',
range: {
start: [370, 39],
finish: [425, 839]
}
},
{
input: 'toggle turn on 599,989 through 806,993',
operation: 'turn on',
range: {
start: [599, 989],
finish: [806, 993]
}
}
]
tests.forEach(test => {
describe('with: ' + test.input, function () {
let response = parseLine(test.input)
it('should return ' + test.operation + ' for operation', function () {
assert.equal(response.operation, test.operation)
})
it('should return ' + test.range.start[0] + ' for range.start[0]', function () {
assert.equal(response.range.start[0], test.range.start[0])
})
it('should return ' + test.range.start[1] + ' for range.start[1]', function () {
assert.equal(response.range.start[1], test.range.start[1])
})
it('should return ' + test.range.finish[0] + ' for range.finish[0]', function () {
assert.equal(response.range.finish[0], test.range.finish[0])
})
it('should return ' + test.range.finish[1] + ' for range.finish[1]', function () {
assert.equal(response.range.finish[1], test.range.finish[1])
})
})
})
})
describe('Test initLights', function () {
it('should return a 1000 x 1000 matrix', function () {
let lights = initLights()
assert.equal(lights.length, 1000)
assert.equal(lights[0].length, 1000)
})
})
describe('Test changeLights', function () {
it('should return a unlit lights', function () {
let lights = initLights()
lights[10][10] = 1
lights[10][20] = 1
lights[11][10] = 1
lights[11][20] = 1
lights = changeLights(lights, 'turn off', {
start: [10, 11],
finish: [11,20]
})
assert.equal(lights[10][11], 0)
assert.equal(lights[10][20], 0)
assert.equal(lights[11][11], 0)
assert.equal(lights[11][20], 0)
})
})
describe('Test changeLights', function () {
it('should return lit lights', function () {
let lights = initLights()
lights[10][10] = 1
lights[10][20] = 1
lights[11][10] = 1
lights[11][20] = 1
lights = changeLights(lights, 'turn off', {
start: [10, 11],
finish: [11,20]
})
assert.equal(lights[10][11], 0)
assert.equal(lights[10][20], 0)
assert.equal(lights[11][11], 0)
assert.equal(lights[11][20], 0)
})
})
describe('Test changeLights', function () {
it('should return 10 lit lights', function () {
let lights = initLights()
lights = changeLights(lights, 'turn on', {
start: [10, 0],
finish: [10, 9],
})
assert.equal(countLitLights(lights), 10)
})
it('should return 100 lit lights', function () {
let lights = initLights()
lights = changeLights(lights, 'turn on', {
start: [0, 0],
finish: [9, 9],
})
assert.equal(countLitLights(lights), 100)
})
})
describe('Test main', function () {
it('should return 90 lit lights', function () {
const instructions = 'toggle 0,0 through 9,9\n'
+ 'toggle 0,0 through 9,0'
assert.equal(main(instructions), 90)
})
it('should return 110 lit lights', function () {
const instructions = 'toggle 0,0 through 9,9\n'
+ 'turn on 20,20 through 20,29'
assert.equal(main(instructions), 110)
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment