2019-12-03 05:59:12 +01:00
use std ::io ;
use std ::io ::BufRead ;
pub fn main ( ) {
let input : Vec < usize > = io ::stdin ( )
. lock ( )
. lines ( )
. next ( )
. unwrap ( )
. unwrap ( )
. split ( ',' )
. map ( | n | n . parse ( ) . unwrap ( ) )
. collect ( ) ;
println! ( " Part 1: {} " , execute ( & mut input . clone ( ) , 12 , 2 ) ) ;
2019-12-03 06:10:59 +01:00
let part2_target = 19690720 ;
let part2 = ( 1 .. 99 )
. flat_map ( | x | ( 1 .. 99 ) . map ( move | y | ( x , y ) ) )
2019-12-03 06:22:37 +01:00
. filter ( | ( x , y ) | execute ( & mut input . clone ( ) , * x , * y ) = = part2_target )
. map ( | ( x , y ) | x * 100 + y )
2019-12-03 06:10:59 +01:00
. next ( )
. unwrap ( ) ;
println! ( " Part 2: {} " , part2 ) ;
2019-12-03 05:59:12 +01:00
}
fn execute ( input : & mut Vec < usize > , p1 : usize , p2 : usize ) -> usize {
input [ 1 ] = p1 ;
input [ 2 ] = p2 ;
for i in ( 0 .. ) . step_by ( 4 ) {
let opcode = input [ i ] ;
2019-12-03 06:10:59 +01:00
let first = input [ i + 1 ] ;
let second = input [ i + 2 ] ;
let target = input [ i + 3 ] ;
2019-12-03 05:59:12 +01:00
match opcode {
1 = > input [ target ] = input [ first ] + input [ second ] ,
2 = > input [ target ] = input [ first ] * input [ second ] ,
99 = > break ,
2019-12-03 06:10:59 +01:00
_ = > unreachable! ( " Invalid opcode " ) ,
2019-12-03 05:59:12 +01:00
}
}
input [ 0 ]
}