Skip to content

Commit e21b7c1

Browse files
feat(2020-day-10): count combinations to solve part 2
nasty nasty hackish solution. but it works :(
1 parent 9000f2f commit e21b7c1

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

2020/day-10/jolts.js

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,83 @@ const countDifferences = (data) => {
2222
console.debug(`Joltage difference between ${curr} and ${next} is ${delta}.`)
2323
tallies[delta]++
2424
})
25-
25+
console.debug('Tallied voltage differences:', tallies)
2626
return tallies
2727
}
2828

2929
const countCombinations = (data) => {
30-
if (data.length > 15) {
31-
return 19208
30+
const tallies = Array(5).fill(0)
31+
const delta = (idx) => {
32+
return data[idx] - data[idx - 1]
3233
}
33-
return 8
34+
35+
// Always account for the outlet
36+
data.push(0)
37+
data = data.sort((a, b) => a - b)
38+
39+
const deltas = data.reduce((res, el, idx) => {
40+
console.debug(idx, el, delta(idx))
41+
if (idx <= 0) {
42+
return res
43+
}
44+
res.push(delta(idx))
45+
return res
46+
}, [])
47+
console.debug('joltage deltas', deltas)
48+
49+
// I'm really not proud of this solution. It hardcodes too much logic with magic constants
50+
// and only works because there are no joltage differences of 2, and the max allowed
51+
// skip is 3.
52+
//
53+
// Since the rules say adapters can support 1, 2, or 3 jolt diferences,
54+
// that means if the difference between n and n+2 is 3 or less, n+1 can be safely
55+
// skipped. Potentially we can skip two.
56+
// Every time we skip a number, the total amount of variations doubles
57+
58+
// This logic would be a LOT messier if we had diffs of 2 in the data set
59+
60+
// When we have 2 skips in a row, we need to leave one combo in case
61+
// skipping both exceeds the max difference
62+
// TODO: we aren't implementing this because our data set doesn't have
63+
// any diffs of 2, which means we never have a 1 + 2 skip to worry about
64+
65+
// When we have 3 skips in a row, we're definitely exceeding the max difference
66+
// if the next is also a skip so we have to leave at least one in place
67+
68+
// When we have 5 skips in a row.... etc..
69+
// TODO: we aren't implementing this because dataset doesn't have any examples
70+
71+
deltas.forEach((d, idx, arr) => {
72+
if (d === 1 && arr[idx + 1] === 1 && arr[idx + 2] === 1 && arr[idx + 3] === 1) {
73+
console.debug('Found 4 in a row')
74+
tallies[4]++
75+
deltas.splice(idx, 4)
76+
} else if (d === 1 && arr[idx + 1] === 1 && arr[idx + 2] === 1) {
77+
console.debug('Found 3 in a row')
78+
tallies[3]++
79+
deltas.splice(idx, 3)
80+
} else if (d === 1 && arr[idx + 1] === 1) {
81+
console.debug('Found 2 in a row')
82+
tallies[2]++
83+
deltas.splice(idx, 2)
84+
} else if (d === 1) {
85+
console.debug('Found 1 in a row')
86+
tallies[1]++
87+
deltas.splice(idx, 1)
88+
}
89+
})
90+
91+
console.debug('skippable ranges', tallies)
92+
console.debug([1, 1 ** tallies[1], 2 ** tallies[2], 4 ** tallies[3], 7 ** tallies[4]])
93+
return (
94+
1 ** tallies[1]
95+
) * (
96+
2 ** tallies[2]
97+
) * (
98+
4 ** tallies[3]
99+
) * (
100+
7 ** tallies[4] // 4 in a row is special case because we can't skip more than 3
101+
)
34102
}
35103

36104
module.exports = {

2020/day-10/jolts.test.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@
22
const { expect } = require('chai')
33
const { countDifferences, countCombinations } = require('./jolts')
44

5-
const adapters = [
5+
const srcAdapters = [
66
[16, 10, 15, 5, 1, 11, 7, 19, 6, 12, 4],
77
[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]
88
]
99

1010
describe('--- Day 10: Adapter Array ---', () => {
11+
let adapters
12+
beforeEach(() => {
13+
// reset test data since arrays get mutated using a quick-and-dirty deep copy
14+
adapters = JSON.parse(JSON.stringify(srcAdapters))
15+
})
1116
describe('Part 1', () => {
1217
describe('countDifferences()', () => {
1318
it('tabulates the amoount of joltage differences in the adapter set', () => {
@@ -21,19 +26,19 @@ describe('--- Day 10: Adapter Array ---', () => {
2126
it('throws an error if any joltage differences exceed 3', () => {
2227
expect(() => countDifferences([5, 40])).to.throw()
2328
})
29+
it('throws an error if any joltage differences is less than 1', () => {
30+
expect(() => countDifferences([5, 5])).to.throw()
31+
})
2432
})
2533
})
2634
describe('Part 2', () => {
2735
describe('countCombinations()', () => {
28-
it('tabulates the amoount of adapter combinations in the set', () => {
36+
it('tabulates the amount of adapter combinations in the set', () => {
2937
const result = countCombinations(adapters[0])
3038
expect(result).to.equal(8)
3139
const result2 = countCombinations(adapters[1])
3240
expect(result2).to.equal(19208)
3341
})
34-
it('throws an error if any joltage differences exceed 3', () => {
35-
expect(() => countDifferences([5, 40])).to.throw()
36-
})
3742
})
3843
})
3944
})

2020/day-10/solution.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const fs = require('fs')
22
const path = require('path')
33
const filePath = path.join(__dirname, 'input.txt')
44
const { inputToArray } = require('../../2018/inputParser')
5-
const { countDifferences } = require('./jolts')
5+
const { countDifferences, countCombinations } = require('./jolts')
66

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

2323
const part2 = () => {
2424
const data = resetInput()
25-
console.debug(data)
26-
return 'No answer yet'
25+
return countCombinations(data)
2726
}
2827
const answers = []
2928
answers.push(part1())

0 commit comments

Comments
 (0)