use std::fs::File; use std::io::{ self, BufRead }; struct Num { rdigits: Vec } impl Num { pub fn from(s: &String) -> Num { let rd: Vec = s.chars().rev() .map(|c| String::from(c).parse::().unwrap()) .collect(); Num { rdigits: rd } } pub fn add(&self, n: &Num) -> Num { let mut xs = self.rdigits.clone(); let mut ys = n.rdigits.clone(); let mut rs: Vec = Vec::new(); while xs.len() < ys.len() { xs.push(0); } while ys.len() < xs.len() { ys.push(0); } let mut rm = 0u8; for (a, b) in xs.iter().zip(ys.iter()) { if a + b + rm > 9 { rs.push((a + b + rm) - 10); rm = 1u8; } else { rs.push(a + b + rm); rm = 0u8; } } if rm > 0 { rs.push(1u8); } Num { rdigits: rs } } pub fn print_10(&self) { for d in self.rdigits.iter().rev().take(10) { print!("{}", d); } println!(""); } } fn main() { let file = File::open("nums.txt").unwrap(); let mut lines = io::BufReader::new(file).lines(); let mut ds = match lines.next() { Some(ln) => Num::from(&ln.unwrap()), None => Num::from(&String::from("0")) }; for ln in lines { let dn = Num::from(&ln.unwrap()); ds = ds.add(&dn); //println!("||||{}", ln.unwrap()); } ds.print_10(); }