|
|
@ -0,0 +1,50 @@
|
|
|
1
|
/*
|
|
|
2
|
* Terrible code for reading CNF formulas in DIMAC format.
|
|
|
3
|
*
|
|
|
4
|
* Don't read this, it's horrible. Although I'm hoping to clean it up
|
|
|
5
|
* at some point, I'm not sure when (if ever) that will be.
|
|
|
6
|
*
|
|
|
7
|
* You have been warned...
|
|
|
8
|
*/
|
|
|
9
|
|
|
|
10
|
use std::io;
|
|
|
11
|
use std::io::Read;
|
|
|
12
|
|
|
|
13
|
fn parse_dimac(dimac: &str) {
|
|
|
14
|
let mut lines = dimac.lines();
|
|
|
15
|
let mut num_vars = 0;
|
|
|
16
|
let mut num_clauses = 0;
|
|
|
17
|
|
|
|
18
|
match lines.next() {
|
|
|
19
|
None => { println!("Error: expected cnf description"); return }
|
|
|
20
|
Some(line) => {
|
|
|
21
|
let desc: Vec<&str> = line.split(" ").collect();
|
|
|
22
|
if desc.len() != 4 || desc[0] != "p" || desc[1] != "cnf" {
|
|
|
23
|
println!("Error: cnf description must be of the form 'p cnf <num vars> <num clauses>'");
|
|
|
24
|
return;
|
|
|
25
|
}
|
|
|
26
|
match desc[2].parse::<u32>() {
|
|
|
27
|
Ok(n) => { num_vars = n }
|
|
|
28
|
Err(e) => { println!("Error: <num vars> must be a positive integer: {}", e); return; }
|
|
|
29
|
}
|
|
|
30
|
|
|
|
31
|
match desc[3].parse::<u32>() {
|
|
|
32
|
Ok(n) => { num_clauses = n }
|
|
|
33
|
Err(e) => { println!("Error: <num clauses> must be a positive integer: {}", e); return; }
|
|
|
34
|
}
|
|
|
35
|
println!("cnf has {} variables and {} clauses", num_vars, num_clauses)
|
|
|
36
|
}
|
|
|
37
|
}
|
|
|
38
|
|
|
|
39
|
let clause_lines: Vec<&str> = lines.collect();
|
|
|
40
|
if clause_lines.len() as u32 != num_clauses {
|
|
|
41
|
println!("Error: Wrong number of clauses: Expected {}, but got {}", num_clauses, clause_lines.len());
|
|
|
42
|
return
|
|
|
43
|
}
|
|
|
44
|
}
|
|
|
45
|
|
|
|
46
|
fn main() {
|
|
|
47
|
let input: &mut String = &mut String::new();
|
|
|
48
|
io::stdin().read_to_string(input);
|
|
|
49
|
parse_dimac(input)
|
|
|
50
|
}
|