Skip to content

Commit

Permalink
feat(2021-day-08): count occurrences of numbers 1,4,7,8 in scrambled …
Browse files Browse the repository at this point in the history
…signals

solves part 1
  • Loading branch information
amclin committed Dec 16, 2021
1 parent 17cc8f7 commit 84894e9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 7 deletions.
31 changes: 31 additions & 0 deletions 2021/day-08/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@
// })
// }

/**
* Using a map of character codes, decode a signal
* @param {array} charCodes
* @param {string} signal
*/
const decodeSignal = (charCodes, signal) => {
console.debug('2021-day-08 decodeSignal()')

const digits = signal.split(' ')
.map(
(code) => {
const clean = code.split('').sort() // cleanup format to match expected map of codes
const matches = charCodes.filter((c) => {
// sort here on the charCode is just in case non-alphabatized data is provided
return (JSON.stringify(c.sort()) === JSON.stringify(clean))
})
if (matches.length < 1) {
throw new Error(`No match found for ${code} when cleaned up to ${clean}`)
}
if (matches.length > 1) {
throw new Error(`Too many matches for ${code} when cleaned up to ${clean}. This most likely indicates a bad list of character codes.`)
}

// The key in charCodes for the match is the decoded number we want
return charCodes.indexOf(matches[0])
}
)

return digits
}

/**
* Takes a string of scrambled codes and deduces which codes correspond
Expand Down Expand Up @@ -454,5 +484,6 @@ const descrambleSignal = (data) => {
}

module.exports = {
decodeSignal,
descrambleSignal
}
43 changes: 42 additions & 1 deletion 2021/day-08/display.test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
/* eslint-env mocha */
const { expect } = require('chai')
const { descrambleSignal } = require('./display')
const { descrambleSignal, decodeSignal } = require('./display')

const testSingle = 'acedgfb cdfbe gcdfa fbcad dab cefabd cdfgeb eafb cagedb ab | cdfeb fcadb cdfeb cdbaf'

const testMultiple = `be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce`.split('\n')

console.debug(
testSingle.split('|')[0].trim()
.split(' ').map(
Expand Down Expand Up @@ -31,5 +42,35 @@ describe('--- Day 8: Seven Segment Search ---', () => {
expect(charCodes.map(code => code.length)).to.deep.equal(expectedLengths)
})
})
describe('decodeSignal()', () => {
const testData = testMultiple[0].split('|').map((a) => a.trim())
const { charCodes } = descrambleSignal(testData[0])

it('decodes a display pattern using the provided map of display codes', () => {
const result = decodeSignal(charCodes, testData[1])
expect(result[0]).to.equal(8)
expect(result[3]).to.equal(4)
})
it('throws an error if a digit doesn`t have a matching code', () => {
expect(
() => decodeSignal(
[['a']],
'dcb'
)
).to.throw(
'No match found for dcb when cleaned up to b,c,d'
)
})
it('throws an error if a digit has multiple matches (meaning a bad codes map)', () => {
expect(
() => decodeSignal(
[['a'], ['d', 'c', 'b'], ['b', 'c', 'd']],
'dcb'
)
).to.throw(
'Too many matches for dcb when cleaned up to b,c,d. This most likely indicates a bad list of character codes.'
)
})
})
})
})
26 changes: 21 additions & 5 deletions 2021/day-08/solution.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, 'input.txt')
const { inputToArray } = require('../../2018/inputParser')
const { linesToArray } = require('../../2018/inputParser')
const { descrambleSignal, decodeSignal } = require('./display')

fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
if (err) throw err

initData = inputToArray(initData.trim())
initData = linesToArray(initData.trim())

const resetInput = () => {
// Deep copy to ensure we aren't mutating the original data
return JSON.parse(JSON.stringify(initData))
return JSON.parse(JSON.stringify(
initData.map(
(line) => line.split('|')
.map((e) => e.trim())
)
))
}

const part1 = () => {
const data = resetInput()
console.debug(data)
return 'No answer yet'

return data.map((entry) => {
const { charCodes } = descrambleSignal(entry[0])
return decodeSignal(charCodes, entry[1])
}).reduce((total, signal) => {
const search = [1, 4, 7, 8]

// Find how many of our desired numbers are in the signal
total += signal.filter((digit) => search.includes(digit)).length

return total
}, 0)
}

const part2 = () => {
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./2021/day-07/solution')
require('./2021/day-08/solution')

0 comments on commit 84894e9

Please sign in to comment.