Skip to content

Commit

Permalink
feat(2020-day-10): count combinations to solve part 2
Browse files Browse the repository at this point in the history
nasty nasty hackish solution. but it works :(
  • Loading branch information
amclin committed Dec 20, 2020
1 parent 9000f2f commit e21b7c1
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 12 deletions.
76 changes: 72 additions & 4 deletions 2020/day-10/jolts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,83 @@ const countDifferences = (data) => {
console.debug(`Joltage difference between ${curr} and ${next} is ${delta}.`)
tallies[delta]++
})

console.debug('Tallied voltage differences:', tallies)
return tallies
}

const countCombinations = (data) => {
if (data.length > 15) {
return 19208
const tallies = Array(5).fill(0)
const delta = (idx) => {
return data[idx] - data[idx - 1]
}
return 8

// Always account for the outlet
data.push(0)
data = data.sort((a, b) => a - b)

const deltas = data.reduce((res, el, idx) => {
console.debug(idx, el, delta(idx))
if (idx <= 0) {
return res
}
res.push(delta(idx))
return res
}, [])
console.debug('joltage deltas', deltas)

// I'm really not proud of this solution. It hardcodes too much logic with magic constants
// and only works because there are no joltage differences of 2, and the max allowed
// skip is 3.
//
// Since the rules say adapters can support 1, 2, or 3 jolt diferences,
// that means if the difference between n and n+2 is 3 or less, n+1 can be safely
// skipped. Potentially we can skip two.
// Every time we skip a number, the total amount of variations doubles

// This logic would be a LOT messier if we had diffs of 2 in the data set

// When we have 2 skips in a row, we need to leave one combo in case
// skipping both exceeds the max difference
// TODO: we aren't implementing this because our data set doesn't have
// any diffs of 2, which means we never have a 1 + 2 skip to worry about

// When we have 3 skips in a row, we're definitely exceeding the max difference
// if the next is also a skip so we have to leave at least one in place

// When we have 5 skips in a row.... etc..
// TODO: we aren't implementing this because dataset doesn't have any examples

deltas.forEach((d, idx, arr) => {
if (d === 1 && arr[idx + 1] === 1 && arr[idx + 2] === 1 && arr[idx + 3] === 1) {
console.debug('Found 4 in a row')
tallies[4]++
deltas.splice(idx, 4)
} else if (d === 1 && arr[idx + 1] === 1 && arr[idx + 2] === 1) {
console.debug('Found 3 in a row')
tallies[3]++
deltas.splice(idx, 3)
} else if (d === 1 && arr[idx + 1] === 1) {
console.debug('Found 2 in a row')
tallies[2]++
deltas.splice(idx, 2)
} else if (d === 1) {
console.debug('Found 1 in a row')
tallies[1]++
deltas.splice(idx, 1)
}
})

console.debug('skippable ranges', tallies)
console.debug([1, 1 ** tallies[1], 2 ** tallies[2], 4 ** tallies[3], 7 ** tallies[4]])
return (
1 ** tallies[1]
) * (
2 ** tallies[2]
) * (
4 ** tallies[3]
) * (
7 ** tallies[4] // 4 in a row is special case because we can't skip more than 3
)
}

module.exports = {
Expand Down
15 changes: 10 additions & 5 deletions 2020/day-10/jolts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
const { expect } = require('chai')
const { countDifferences, countCombinations } = require('./jolts')

const adapters = [
const srcAdapters = [
[16, 10, 15, 5, 1, 11, 7, 19, 6, 12, 4],
[28, 33, 18, 42, 31, 14, 46, 20, 48, 47, 24, 23, 49, 45, 19, 38, 39, 11, 1, 32, 25, 35, 8, 17, 7, 9, 4, 2, 34, 10, 3]
]

describe('--- Day 10: Adapter Array ---', () => {
let adapters
beforeEach(() => {
// reset test data since arrays get mutated using a quick-and-dirty deep copy
adapters = JSON.parse(JSON.stringify(srcAdapters))
})
describe('Part 1', () => {
describe('countDifferences()', () => {
it('tabulates the amoount of joltage differences in the adapter set', () => {
Expand All @@ -21,19 +26,19 @@ describe('--- Day 10: Adapter Array ---', () => {
it('throws an error if any joltage differences exceed 3', () => {
expect(() => countDifferences([5, 40])).to.throw()
})
it('throws an error if any joltage differences is less than 1', () => {
expect(() => countDifferences([5, 5])).to.throw()
})
})
})
describe('Part 2', () => {
describe('countCombinations()', () => {
it('tabulates the amoount of adapter combinations in the set', () => {
it('tabulates the amount of adapter combinations in the set', () => {
const result = countCombinations(adapters[0])
expect(result).to.equal(8)
const result2 = countCombinations(adapters[1])
expect(result2).to.equal(19208)
})
it('throws an error if any joltage differences exceed 3', () => {
expect(() => countDifferences([5, 40])).to.throw()
})
})
})
})
5 changes: 2 additions & 3 deletions 2020/day-10/solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs')
const path = require('path')
const filePath = path.join(__dirname, 'input.txt')
const { inputToArray } = require('../../2018/inputParser')
const { countDifferences } = require('./jolts')
const { countDifferences, countCombinations } = require('./jolts')

fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {
if (err) throw err
Expand All @@ -22,8 +22,7 @@ fs.readFile(filePath, { encoding: 'utf8' }, (err, initData) => {

const part2 = () => {
const data = resetInput()
console.debug(data)
return 'No answer yet'
return countCombinations(data)
}
const answers = []
answers.push(part1())
Expand Down

0 comments on commit e21b7c1

Please sign in to comment.