2
2
const expect = require ( 'chai' ) . expect
3
3
const {
4
4
calculateXAfterY,
5
+ findPattern,
5
6
loopRecipesForElves,
6
7
Recipes,
7
8
totalDigitsInArray
8
9
} = require ( './recipes' )
9
10
10
11
describe ( '--- Day 14: Chocolate Charts ---' , ( ) => {
12
+ let recipes
11
13
describe ( 'Part 1:' , ( ) => {
14
+ beforeEach ( ( ) => {
15
+ recipes = new Recipes ( [ 3 , 7 ] )
16
+ } )
12
17
describe ( 'new Recipes()' , ( ) => {
13
18
it ( 'builds a linked list' , ( ) => {
14
- const recipes = new Recipes ( 0 )
19
+ expect ( recipes . head . value ) . to . equal ( 7 )
15
20
for ( let x = 1 ; x <= 5 ; x ++ ) {
16
21
recipes . addRecipe ( x )
17
22
}
18
- expect ( recipes . length ) . to . equal ( 6 )
23
+ expect ( recipes . length ) . to . equal ( 7 )
19
24
expect ( recipes . head . value ) . to . equal ( 5 )
20
- expect ( recipes . tail . value ) . to . equal ( 0 )
25
+ expect ( recipes . tail . value ) . to . equal ( 3 )
21
26
expect ( recipes . tail . prev ) . to . equal ( recipes . head ) // circular linked list for prev
22
27
expect ( recipes . head . next ) . to . equal ( recipes . tail ) // circular linked list for next
23
28
} )
24
29
describe ( 'scoreRecipes()' , ( ) => {
25
30
it ( 'adds new recipes based on the provided score' , ( ) => {
26
- const recipes = new Recipes ( 0 )
27
31
for ( let x = 1 ; x <= 5 ; x ++ ) {
28
32
recipes . addRecipe ( x )
29
33
}
@@ -46,95 +50,80 @@ describe('--- Day 14: Chocolate Charts ---', () => {
46
50
describe ( 'loopRecipeForEleves()' , ( ) => {
47
51
it ( 'loops through the recipe object for the specified elves the specified number of times' , ( ) => {
48
52
const expected = '37101012451589167792' // list of recipe values in the last iteration of the example
49
- const elves = [ 3 , 7 ]
50
- const recipes = new Recipes ( elves [ 0 ] )
51
- let actual = ''
52
53
53
- elves . forEach ( ( elf , idx ) => {
54
- if ( idx === 0 ) {
55
- elves [ 0 ] = recipes . head
56
- } else {
57
- elves [ idx ] = recipes . addRecipe ( elf )
58
- }
59
- } )
54
+ loopRecipesForElves ( recipes , 15 )
55
+ let actual = recipes . tail . value . toString ( )
56
+ let iterator = recipes . tail . next
57
+ while ( iterator !== recipes . tail ) {
58
+ actual += iterator . value . toString ( )
59
+ iterator = iterator . next
60
+ }
60
61
61
- loopRecipesForElves ( elves , recipes , 15 )
62
+ expect ( expected ) . to . equal ( actual )
63
+ } )
64
+ it ( 'handles when the score is multidigit' , ( ) => {
65
+ const expected = '3710101245158916'
62
66
67
+ loopRecipesForElves ( recipes , 10 )
68
+ // next should be multidigit
69
+ loopRecipesForElves ( recipes , 1 )
70
+ let actual = recipes . tail . value . toString ( )
63
71
let iterator = recipes . tail . next
64
- actual += recipes . tail . value . toString ( )
65
72
while ( iterator !== recipes . tail ) {
66
73
actual += iterator . value . toString ( )
67
74
iterator = iterator . next
68
75
}
69
-
76
+ expect ( recipes . length ) . to . equal ( expected . length )
70
77
expect ( expected ) . to . equal ( actual )
71
78
} )
72
79
} )
73
- describe ( 'calculateXAfterY(x, y, recipe, elves )' , ( ) => {
80
+ describe ( 'calculateXAfterY(x, y, recipe)' , ( ) => {
74
81
it ( 'predicts the next X results after the elves have executed Y' , ( ) => {
75
- const elves = [ 3 , 7 ]
76
- const recipes = new Recipes ( elves [ 0 ] )
77
- let actual = ''
78
-
79
- elves . forEach ( ( elf , idx ) => {
80
- if ( idx === 0 ) {
81
- elves [ 0 ] = recipes . head
82
- } else {
83
- elves [ idx ] = recipes . addRecipe ( elf )
84
- }
85
- } )
86
-
87
- actual = calculateXAfterY ( 10 , 9 , recipes , elves )
82
+ let actual = calculateXAfterY ( 10 , 9 , recipes )
88
83
expect ( actual ) . to . equal ( '5158916779' )
89
84
} )
90
85
it ( 'predicts the next X results after the elves have executed Y' , ( ) => {
91
- const elves = [ 3 , 7 ]
92
- const recipes = new Recipes ( elves [ 0 ] )
93
- let actual = ''
94
-
95
- elves . forEach ( ( elf , idx ) => {
96
- if ( idx === 0 ) {
97
- elves [ 0 ] = recipes . head
98
- } else {
99
- elves [ idx ] = recipes . addRecipe ( elf )
100
- }
101
- } )
102
-
103
- actual = calculateXAfterY ( 10 , 5 , recipes , elves )
86
+ const actual = calculateXAfterY ( 10 , 5 , recipes )
104
87
expect ( actual ) . to . equal ( '0124515891' )
105
88
} )
106
89
it ( 'predicts the next X results after the elves have executed Y' , ( ) => {
107
- const elves = [ 3 , 7 ]
108
- const recipes = new Recipes ( elves [ 0 ] )
109
- let actual = ''
110
-
111
- elves . forEach ( ( elf , idx ) => {
112
- if ( idx === 0 ) {
113
- elves [ 0 ] = recipes . head
114
- } else {
115
- elves [ idx ] = recipes . addRecipe ( elf )
116
- }
117
- } )
118
-
119
- actual = calculateXAfterY ( 10 , 18 , recipes , elves )
90
+ const actual = calculateXAfterY ( 10 , 18 , recipes )
120
91
expect ( actual ) . to . equal ( '9251071085' )
121
92
} )
122
93
it ( 'predicts the next X results after the elves have executed Y' , ( ) => {
123
- const elves = [ 3 , 7 ]
124
- const recipes = new Recipes ( elves [ 0 ] )
125
- let actual = ''
126
-
127
- elves . forEach ( ( elf , idx ) => {
128
- if ( idx === 0 ) {
129
- elves [ 0 ] = recipes . head
130
- } else {
131
- elves [ idx ] = recipes . addRecipe ( elf )
132
- }
133
- } )
134
-
135
- actual = calculateXAfterY ( 10 , 2018 , recipes , elves )
94
+ const actual = calculateXAfterY ( 10 , 2018 , recipes )
136
95
expect ( actual ) . to . equal ( '5941429882' )
137
96
} )
97
+ it ( 'positions results correctly if X triggers 2 recipes being added' , ( ) => {
98
+ let actual = calculateXAfterY ( 3 , 15 , recipes )
99
+ expect ( actual ) . to . equal ( '677' )
100
+ } )
101
+ } )
102
+ describe ( 'findPattern()' , ( ) => {
103
+ it ( 'counts the number of recipes to the left of the specified pattern' , ( ) => {
104
+ const actual = findPattern ( '51589' , recipes )
105
+ expect ( actual ) . to . equal ( 9 )
106
+ } )
107
+ it ( 'counts the number of recipes to the left of the specified pattern' , ( ) => {
108
+ const actual = findPattern ( '01245' , recipes )
109
+ expect ( actual ) . to . equal ( 5 )
110
+ } )
111
+ it ( 'counts the number of recipes to the left of the specified pattern' , ( ) => {
112
+ const actual = findPattern ( '92510' , recipes )
113
+ expect ( actual ) . to . equal ( 18 )
114
+ } )
115
+ it ( 'counts the number of recipes to the left of the specified pattern' , ( ) => {
116
+ const actual = findPattern ( '59414' , recipes )
117
+ expect ( actual ) . to . equal ( 2018 )
118
+ } )
119
+ it ( 'accepts small search buffer sizes' , ( ) => {
120
+ const actual = findPattern ( '59414' , recipes , 20 )
121
+ expect ( actual ) . to . equal ( 2018 )
122
+ } )
123
+ it ( 'accepts large search buffer sizes' , ( ) => {
124
+ const actual = findPattern ( '59414' , recipes , 50000 )
125
+ expect ( actual ) . to . equal ( 2018 )
126
+ } )
138
127
} )
139
128
} )
140
129
} )
0 commit comments