Skip to content

Commit

Permalink
feat(2021-day-10): find incomplete line errors
Browse files Browse the repository at this point in the history
solves part 2
  • Loading branch information
amclin committed Dec 17, 2021
1 parent bb212f3 commit 2539be9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
14 changes: 9 additions & 5 deletions 2021/day-10/linting.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,25 @@ const lintLine = (line) => {
pos++
}

// if we run out of line, ignore per instructions for Step 1
// if we run out of characters in the line, that means it is
// incomplete, and we need to provide an autocomplete suggestion
if (expected.length > 0) {
// TODO - add the reporting when we need it in Step 2?
// Reversing the 'expected' string gives us the autocomplete suggestion
return {
suggestion: [...expected].reverse().join('')
}
}
}

const lintAll = (instructions) => {
const errors = instructions.map(lintLine) // lint each line
.map((error, idx) => {
return { ...error, line: idx }
}).filter((report) => !!(report.char)) // remove lines without errors
}).filter((report) => !!(report.char) || !!(report.suggestion)) // remove lines without errors

console.log(`Linting found ${errors.length} errors in ${instructions.length} lines.`)
console.debug(instructions)
console.debug(errors)
// console.debug(instructions)
// console.debug(errors)

return errors
}
Expand Down
28 changes: 28 additions & 0 deletions 2021/day-10/linting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ const testData = `[({(<(())[]>[[{[]{<()<>>
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]`

const autocomplete = {
'[({(<(())[]>[[{[]{<()<>>': '}}]])})]',
'[(()[<>])]({[<{<<[]>>(': ')}>]})',
'(((({<>}<{<{<>}{[]{[]{}': '}}>}>))))',
'{<[[]]>}<{[{[{[]{()[[[]': ']]}}]}]}>',
'<{([{{}}[<[[[<>{}]]]>[]]': '])}>'
}

describe('--- Day 10: Syntax Scoring ---', () => {
describe('Part 1', () => {
describe('lintLine()', () => {
Expand Down Expand Up @@ -57,6 +65,7 @@ describe('--- Day 10: Syntax Scoring ---', () => {
describe('lintAll', () => {
it('finds all lines with linting errors', () => {
const errors = lintAll(testData.split('\n'))
.filter((err) => (err.char))

expect(errors.length).to.equal(5)
expect(errors[0]).to.deep.equal({
Expand Down Expand Up @@ -90,6 +99,25 @@ describe('--- Day 10: Syntax Scoring ---', () => {
found: '>'
})
})
it('provides autocomplete suggestions for incomplete lines', () => {
const data = testData.split('\n')
const errors = lintAll(data)
.filter((err) => !!err.suggestion)

expect(errors.length).to.equal(5)
errors.forEach((err) => {
expect(err.suggestion).to.equal(
autocomplete[data[err.line]]
)
})
})
it('skips lines without errors', () => {
const errors = lintAll([
'[]',
'[()]'
])
expect(errors.length).to.equal(0)
})
})
})
})
13 changes: 10 additions & 3 deletions 2021/day-10/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path')
const filePath = path.join(__dirname, 'input.txt')
const { linesToArray } = require('../../2018/inputParser')
const { lintAll } = require('./linting')
const { scoreAutocomplete, findMiddleScore } = require('./scoring')

fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
if (err) throw err
Expand All @@ -25,13 +26,19 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {

const errors = lintAll(data)

return errors.reduce((total, error) => total + points[error.found], 0)
// Score the premature closure errors
return errors.filter((err) => !!err.char)
.reduce((total, error) => total + points[error.found], 0)
}

const part2 = () => {
const data = resetInput()
console.debug(data)
return 'No answer yet'
// find the incomplete line errors
const errors = lintAll(data).filter((err) => !!err.suggestion)

const scores = errors.map((err) => scoreAutocomplete(err.suggestion))

return findMiddleScore(scores)
}
const answers = []
answers.push(part1())
Expand Down

0 comments on commit 2539be9

Please sign in to comment.