1+ use clap:: { App , Arg } ;
2+ use icfpc:: parse:: read_all_inputs;
3+ use icfpc:: parse:: read_commands;
4+ use std:: collections:: HashMap ;
5+
6+ use std:: fs:: File ;
7+ use std:: io:: Read ;
8+
9+ fn main ( ) {
10+ let matches = App :: new ( "Compare solutions" )
11+ . version ( "0.1.0" )
12+ . arg (
13+ Arg :: with_name ( "input" )
14+ . long ( "input" )
15+ . takes_value ( true )
16+ . help ( "input root directory" ) ,
17+ )
18+ . arg (
19+ Arg :: with_name ( "file" )
20+ . long ( "file" )
21+ . takes_value ( true )
22+ . help ( "solutions path file" ) ,
23+ )
24+ . arg (
25+ Arg :: with_name ( "output" )
26+ . long ( "output" )
27+ . takes_value ( true )
28+ . help ( "output root to generate" ) ,
29+ )
30+ . get_matches ( ) ;
31+
32+ let input_root = matches. value_of ( "input" ) . expect ( "no input specified" ) ;
33+ let inputs = read_all_inputs ( input_root) ;
34+ let output_root = matches. value_of ( "output" ) . expect ( "no output specified" ) ;
35+
36+ let path_file = matches. value_of ( "file" ) . expect ( "no file specified" ) ;
37+ let mut result: HashMap < String , Vec < ( usize , String ) > > = HashMap :: new ( ) ;
38+ for output_root in std:: fs:: read_to_string ( path_file) . unwrap ( ) . lines ( ) {
39+ for input in & inputs {
40+ let commands = {
41+ let output_path = format ! ( "{}/{}" , output_root, input. output_file_name( ) ) ;
42+ let mut output_file = File :: open ( & output_path) . unwrap ( ) ;
43+ let mut output_str = String :: new ( ) ;
44+ output_file. read_to_string ( & mut output_str) . unwrap ( ) ;
45+ read_commands ( & output_str)
46+ } ;
47+ result
48+ . entry ( input. id . to_owned ( ) )
49+ . or_default ( )
50+ . push ( ( commands. len ( ) , output_root. to_owned ( ) ) ) ;
51+ }
52+ }
53+ for input in & inputs {
54+ let v = result. get_mut ( & input. id ) . unwrap ( ) ;
55+ v. sort ( ) ;
56+ let best_root = & v[ 0 ] . 1 ;
57+ println ! ( "{}: {} ({} {})" , input. id, best_root, v[ 0 ] . 0 , v[ 1 ] . 0 ) ;
58+ let best_path = format ! ( "{}/{}" , best_root, input. output_file_name( ) ) ;
59+ let new_path = format ! ( "{}/{}" , output_root, input. output_file_name( ) ) ;
60+ std:: fs:: copy ( best_path, new_path) . unwrap ( ) ;
61+ }
62+ }
0 commit comments