11use aoc_utils:: * ;
22use itertools:: Itertools ;
3- use z3:: {
4- ast:: { Ast , Int } ,
5- Config , Context , Optimize , SatResult ,
6- } ;
3+ use z3:: { ast:: Int , Optimize , SatResult } ;
74
85advent_of_code:: solution!( 10 ) ;
96
@@ -66,13 +63,11 @@ pub fn part_one(input: &str) -> Option<u64> {
6663}
6764
6865fn solve_part2 ( buttons : & [ Vec < usize > ] , joltages : & [ u64 ] ) -> u64 {
69- let cfg = Config :: new ( ) ;
70- let ctx = Context :: new ( & cfg) ;
71- let opt = Optimize :: new ( & ctx) ;
66+ let opt = Optimize :: new ( ) ;
7267
7368 // target_counts = number of times to press each button (what we're solving for)
7469 let target_counts: Vec < Int > = ( 0 ..buttons. len ( ) )
75- . map ( |i| Int :: new_const ( & ctx , format ! ( "p{i}" ) ) )
70+ . map ( |i| Int :: new_const ( format ! ( "p{i}" ) ) )
7671 . collect ( ) ;
7772
7873 // Constraint: (sum of target_counts[i] for all i where pos in buttons[i]) == joltages[pos]
@@ -84,19 +79,19 @@ fn solve_part2(buttons: &[Vec<usize>], joltages: &[u64]) -> u64 {
8479 . map ( |( i, _) | & target_counts[ i] )
8580 . collect ( ) ;
8681
87- let sum = Int :: add ( & ctx , & terms) ;
88- opt. assert ( & sum. _eq ( & Int :: from_u64 ( & ctx , jolt) ) ) ;
82+ let sum = Int :: add ( & terms) ;
83+ opt. assert ( & sum. eq ( & Int :: from_u64 ( jolt) ) ) ;
8984 }
9085
9186 // Constraint: t >= 0 (can't press a button negative times)
92- let zero = Int :: from_u64 ( & ctx , 0 ) ;
87+ let zero = Int :: from_u64 ( 0 ) ;
9388 for t in & target_counts {
9489 opt. assert ( & t. ge ( & zero) ) ;
9590 }
9691
9792 // Minimize sum of target_counts
9893 let target_count_refs: Vec < _ > = target_counts. iter ( ) . collect ( ) ;
99- let total = Int :: add ( & ctx , & target_count_refs) ;
94+ let total = Int :: add ( & target_count_refs) ;
10095 opt. minimize ( & total) ;
10196
10297 // Solve and extract result
0 commit comments