parse big integer
This commit is contained in:
parent
d858373b51
commit
16520d06f5
19
src/file.rs
19
src/file.rs
@ -2,6 +2,7 @@ use rug::Integer;
|
||||
use std::fs;
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::str::FromStr;
|
||||
|
||||
pub struct AgcdInput {
|
||||
pub noise_bits: usize,
|
||||
@ -39,10 +40,20 @@ pub fn parse_file(path: &Path) -> io::Result<AgcdInput> {
|
||||
|
||||
// Parse numbers from remaining lines
|
||||
let numbers: Vec<Integer> = lines
|
||||
.filter(|line| !line.trim().starts_with("//") && !line.trim().is_empty()) // ignore comment and empty lines
|
||||
.filter_map(|line| line.trim().parse::<u64>().ok())
|
||||
.map(Integer::from)
|
||||
.collect();
|
||||
.filter(|line| {
|
||||
let t = line.trim();
|
||||
!t.starts_with("//") && !t.is_empty()
|
||||
})
|
||||
.map(|line| {
|
||||
let s = line.trim();
|
||||
Integer::from_str(s).map_err(|e| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidData,
|
||||
format!("Failed to parse integer '{}': {}", s, e),
|
||||
)
|
||||
})
|
||||
})
|
||||
.collect::<Result<_, io::Error>>()?;
|
||||
|
||||
if numbers.len() < 2 {
|
||||
return Err(io::Error::new(
|
||||
|
@ -34,11 +34,7 @@ fn main() -> std::io::Result<()> {
|
||||
let path = path.as_deref().unwrap_or(Path::new("./input.txt"));
|
||||
let input = parse_file(path)?;
|
||||
|
||||
let result = agcd(input.numbers, input.noise_bits);
|
||||
println!(
|
||||
"Approximate GCD with noise_bits={}: {}",
|
||||
input.noise_bits, result
|
||||
);
|
||||
agcd(input.numbers, input.noise_bits);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
Loading…
x
Reference in New Issue
Block a user