@@ -16,8 +16,8 @@ const permute = (permArr, usedChars, input) => {
1616 return permArr ;
1717} ;
1818
19- const part1 = content => {
20- const testProgram = content [ 0 ] . split ( ',' ) . map ( n => parseInt ( n , 10 ) ) ;
19+ const part1 = ( content ) => {
20+ const testProgram = content [ 0 ] . split ( ',' ) . map ( ( n ) => parseInt ( n , 10 ) ) ;
2121
2222 const permArr = [ ] ;
2323 const usedChars = [ ] ;
@@ -109,9 +109,128 @@ const part1 = content => {
109109 return maxSignal ;
110110} ;
111111
112+ const part2 = ( content ) => {
113+ const testProgram = content [ 0 ] . split ( ',' ) . map ( ( n ) => parseInt ( n , 10 ) ) ;
114+
115+ const permArr = [ ] ;
116+ const usedChars = [ ] ;
117+
118+ const phaseSettings = permute ( permArr , usedChars , [ 5 , 6 , 7 , 8 , 9 ] ) ;
119+ const thrusterSignals = [ ] ;
120+
121+ for ( const phaseSetting of phaseSettings ) {
122+ let input = 0 ;
123+ let amplifierId = 0 ;
124+ let finished = false ;
125+ const programMemoriesPointers = [ ] ;
126+ const programMemories = [ ] ;
127+ const isFirstLoop = [ ] ; // for each amp
128+
129+ // initialize programs of the 5 amplifiers
130+ for ( let i = 0 ; i < 5 ; i ++ ) {
131+ programMemories . push ( [ ...testProgram ] ) ;
132+ isFirstLoop . push ( true ) ;
133+ }
134+
135+ while ( ! finished ) {
136+ let pointer =
137+ programMemoriesPointers [ amplifierId ] === undefined
138+ ? 0
139+ : programMemoriesPointers [ amplifierId ] ;
140+ const memory = programMemories [ amplifierId ] ;
141+
142+ // the program exits the loop when it encounters the halt code (9)
143+ while ( true ) {
144+ let operation = memory [ pointer ] ;
145+ if ( operation === 99 ) {
146+ finished = true ;
147+ break ;
148+ } else if ( operation === 3 ) {
149+ // takes input as value to save it
150+ // first it takes the phaseSetting as value, then the inputs from initial value or
151+ // from outputs of the previous amplifier
152+ memory [ memory [ pointer + 1 ] ] = isFirstLoop [ amplifierId ]
153+ ? phaseSetting [ amplifierId ]
154+ : input ;
155+ isFirstLoop [ amplifierId ] = false ;
156+ pointer += 2 ;
157+ } else {
158+ operation = operation . toString ( ) . split ( '' ) ;
159+ let intcode ;
160+ let firstMode = 0 ;
161+ let secondMode = 0 ;
162+
163+ for ( let i = operation . length - 1 ; i >= 0 ; i -- ) {
164+ if ( i === operation . length - 1 ) {
165+ intcode = operation [ i ] ;
166+ if ( intcode === '9' ) {
167+ finished = true ;
168+ break ;
169+ }
170+ } else if ( i === operation . length - 3 ) {
171+ firstMode = parseInt ( operation [ i ] , 10 ) ;
172+ } else if ( i === operation . length - 4 ) {
173+ secondMode = parseInt ( operation [ i ] , 10 ) ;
174+ }
175+ }
176+
177+ const firstVal = firstMode
178+ ? memory [ pointer + 1 ]
179+ : memory [ memory [ pointer + 1 ] ] ;
180+ const secondVal = secondMode
181+ ? memory [ pointer + 2 ]
182+ : memory [ memory [ pointer + 2 ] ] ;
183+
184+ if ( intcode === '1' ) {
185+ memory [ memory [ pointer + 3 ] ] = firstVal + secondVal ;
186+ pointer += 4 ;
187+ } else if ( intcode === '2' ) {
188+ memory [ memory [ pointer + 3 ] ] = firstVal * secondVal ;
189+ pointer += 4 ;
190+ } else if ( intcode === '4' ) {
191+ // saves value as input
192+ input = firstVal ;
193+ pointer += 2 ;
194+ // go to next amplifier
195+ programMemoriesPointers [ amplifierId ] = pointer ;
196+ amplifierId = amplifierId === 4 ? 0 : amplifierId + 1 ;
197+ break ;
198+ } else if ( intcode === '5' ) {
199+ if ( firstVal ) {
200+ pointer = secondVal ;
201+ continue ;
202+ }
203+ pointer += 3 ;
204+ } else if ( intcode === '6' ) {
205+ if ( ! firstVal ) {
206+ pointer = secondVal ;
207+ continue ;
208+ }
209+ pointer += 3 ;
210+ } else if ( intcode === '7' ) {
211+ memory [ memory [ pointer + 3 ] ] = firstVal < secondVal ? 1 : 0 ;
212+ pointer += 4 ;
213+ } else if ( intcode === '8' ) {
214+ memory [ memory [ pointer + 3 ] ] = firstVal === secondVal ? 1 : 0 ;
215+ pointer += 4 ;
216+ }
217+ }
218+ }
219+ }
220+ thrusterSignals . push ( input ) ;
221+ }
222+
223+ const maxSignal = thrusterSignals . reduce (
224+ ( max , value ) => ( max < value ? value : max ) ,
225+ 0
226+ ) ;
227+ return maxSignal ;
228+ } ;
229+
112230( ( ) => {
113231 const args = process . argv . slice ( 2 ) ;
114232 const filePath = args [ 0 ] || 'day7.txt' ;
115233 const content = parseFile ( filePath ) ;
116234 console . log ( `part1: ${ part1 ( content ) } ` ) ;
235+ console . log ( `part2: ${ part2 ( content ) } ` ) ;
117236} ) ( ) ;
0 commit comments