@@ -22,15 +22,83 @@ const countDifferences = (data) => {
2222 console . debug ( `Joltage difference between ${ curr } ${ next } ${ delta }  ) 
2323 tallies [ delta ] ++ 
2424 } ) 
25- 
25+   console . debug ( 'Tallied voltage differences:' ,   tallies ) 
2626 return  tallies 
2727} 
2828
2929const  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
36104module . exports  =  { 
0 commit comments