|
| 1 | +const countDifferences = (data) => { |
| 2 | + const tallies = Array(4).fill(0) |
| 3 | + // Always account for the outlet |
| 4 | + data.push(0) |
| 5 | + // Always add the native adapter at the end |
| 6 | + tallies[3]++ |
| 7 | + |
| 8 | + // Iterate through the adapters |
| 9 | + data.sort((a, b) => a - b) |
| 10 | + .forEach((curr, idx) => { |
| 11 | + if (!data[idx + 1]) { |
| 12 | + // end of array, nothing to do |
| 13 | + return |
| 14 | + } |
| 15 | + const next = data[idx + 1] |
| 16 | + const delta = next - curr |
| 17 | + if (delta > 3) { |
| 18 | + // Problem with data. Gap in joltages greater than allowed |
| 19 | + throw new Error(`Joltage difference between ${curr} and ${next} is greater than allowed.`) |
| 20 | + } |
| 21 | + |
| 22 | + console.debug(`Joltage difference between ${curr} and ${next} is ${delta}.`) |
| 23 | + tallies[delta]++ |
| 24 | + }) |
| 25 | + console.debug('Tallied voltage differences:', tallies) |
| 26 | + return tallies |
| 27 | +} |
| 28 | + |
| 29 | +const countCombinations = (data) => { |
| 30 | + const tallies = Array(5).fill(0) |
| 31 | + const delta = (idx) => { |
| 32 | + return data[idx] - data[idx - 1] |
| 33 | + } |
| 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 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +module.exports = { |
| 105 | + countDifferences, |
| 106 | + countCombinations |
| 107 | +} |
0 commit comments