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::fs;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
pub struct AgcdInput {
|
pub struct AgcdInput {
|
||||||
pub noise_bits: usize,
|
pub noise_bits: usize,
|
||||||
@ -39,10 +40,20 @@ pub fn parse_file(path: &Path) -> io::Result<AgcdInput> {
|
|||||||
|
|
||||||
// Parse numbers from remaining lines
|
// Parse numbers from remaining lines
|
||||||
let numbers: Vec<Integer> = lines
|
let numbers: Vec<Integer> = lines
|
||||||
.filter(|line| !line.trim().starts_with("//") && !line.trim().is_empty()) // ignore comment and empty lines
|
.filter(|line| {
|
||||||
.filter_map(|line| line.trim().parse::<u64>().ok())
|
let t = line.trim();
|
||||||
.map(Integer::from)
|
!t.starts_with("//") && !t.is_empty()
|
||||||
.collect();
|
})
|
||||||
|
.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 {
|
if numbers.len() < 2 {
|
||||||
return Err(io::Error::new(
|
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 path = path.as_deref().unwrap_or(Path::new("./input.txt"));
|
||||||
let input = parse_file(path)?;
|
let input = parse_file(path)?;
|
||||||
|
|
||||||
let result = agcd(input.numbers, input.noise_bits);
|
agcd(input.numbers, input.noise_bits);
|
||||||
println!(
|
|
||||||
"Approximate GCD with noise_bits={}: {}",
|
|
||||||
input.noise_bits, result
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user