Skip to content

Commit 2539be9

Browse files
feat(2021-day-10): find incomplete line errors
solves part 2
1 parent bb212f3 commit 2539be9

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

2021/day-10/linting.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,25 @@ const lintLine = (line) => {
3030
pos++
3131
}
3232

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

3943
const lintAll = (instructions) => {
4044
const errors = instructions.map(lintLine) // lint each line
4145
.map((error, idx) => {
4246
return { ...error, line: idx }
43-
}).filter((report) => !!(report.char)) // remove lines without errors
47+
}).filter((report) => !!(report.char) || !!(report.suggestion)) // remove lines without errors
4448

4549
console.log(`Linting found ${errors.length} errors in ${instructions.length} lines.`)
46-
console.debug(instructions)
47-
console.debug(errors)
50+
// console.debug(instructions)
51+
// console.debug(errors)
4852

4953
return errors
5054
}

2021/day-10/linting.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ const testData = `[({(<(())[]>[[{[]{<()<>>
2020
<{([([[(<>()){}]>(<<{{
2121
<{([{{}}[<[[[<>{}]]]>[]]`
2222

23+
const autocomplete = {
24+
'[({(<(())[]>[[{[]{<()<>>': '}}]])})]',
25+
'[(()[<>])]({[<{<<[]>>(': ')}>]})',
26+
'(((({<>}<{<{<>}{[]{[]{}': '}}>}>))))',
27+
'{<[[]]>}<{[{[{[]{()[[[]': ']]}}]}]}>',
28+
'<{([{{}}[<[[[<>{}]]]>[]]': '])}>'
29+
}
30+
2331
describe('--- Day 10: Syntax Scoring ---', () => {
2432
describe('Part 1', () => {
2533
describe('lintLine()', () => {
@@ -57,6 +65,7 @@ describe('--- Day 10: Syntax Scoring ---', () => {
5765
describe('lintAll', () => {
5866
it('finds all lines with linting errors', () => {
5967
const errors = lintAll(testData.split('\n'))
68+
.filter((err) => (err.char))
6069

6170
expect(errors.length).to.equal(5)
6271
expect(errors[0]).to.deep.equal({
@@ -90,6 +99,25 @@ describe('--- Day 10: Syntax Scoring ---', () => {
9099
found: '>'
91100
})
92101
})
102+
it('provides autocomplete suggestions for incomplete lines', () => {
103+
const data = testData.split('\n')
104+
const errors = lintAll(data)
105+
.filter((err) => !!err.suggestion)
106+
107+
expect(errors.length).to.equal(5)
108+
errors.forEach((err) => {
109+
expect(err.suggestion).to.equal(
110+
autocomplete[data[err.line]]
111+
)
112+
})
113+
})
114+
it('skips lines without errors', () => {
115+
const errors = lintAll([
116+
'[]',
117+
'[()]'
118+
])
119+
expect(errors.length).to.equal(0)
120+
})
93121
})
94122
})
95123
})

2021/day-10/solution.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { linesToArray } = require('../../2018/inputParser')
55
const { lintAll } = require('./linting')
6+
const { scoreAutocomplete, findMiddleScore } = require('./scoring')
67

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

2627
const errors = lintAll(data)
2728

28-
return errors.reduce((total, error) => total + points[error.found], 0)
29+
// Score the premature closure errors
30+
return errors.filter((err) => !!err.char)
31+
.reduce((total, error) => total + points[error.found], 0)
2932
}
3033

3134
const part2 = () => {
3235
const data = resetInput()
33-
console.debug(data)
34-
return 'No answer yet'
36+
// find the incomplete line errors
37+
const errors = lintAll(data).filter((err) => !!err.suggestion)
38+
39+
const scores = errors.map((err) => scoreAutocomplete(err.suggestion))
40+
41+
return findMiddleScore(scores)
3542
}
3643
const answers = []
3744
answers.push(part1())

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy