return Result

This commit is contained in:
Antoine Martin 2019-11-14 19:16:49 +01:00
parent b80637585b
commit ecfd0c10bf
2 changed files with 10 additions and 12 deletions

View file

@ -14,29 +14,29 @@ fn main() -> Result<()> {
.expect("Please provide the path to the input file"), .expect("Please provide the path to the input file"),
)?; )?;
part1(&input); println!("part 1: {}", part1(&input)?);
part2(&input); println!("part 2: {}", part2(&input)?);
Ok(()) Ok(())
} }
fn part1(input: &str) { fn part1(input: &str) -> Result<i32> {
let freq = input let freq = input
.lines() .lines()
.map(|line| line.parse::<i32>().unwrap()) .map(|line| line.parse::<i32>().unwrap())
.sum::<i32>(); .sum::<i32>();
println!("{}", freq); Ok(freq)
} }
fn part2(input: &str) { fn part2(input: &str) -> Result<i32> {
let mut freqs = HashSet::new(); let mut freqs = HashSet::new();
let mut freq = 0; let mut freq = 0;
loop { loop {
for line in input.lines() { for line in input.lines() {
freq += line.parse::<i32>().unwrap(); freq += line.parse::<i32>()?;
if freqs.contains(&freq) { if freqs.contains(&freq) {
println!("{}", freq); println!("{}", freq);
return; return Ok(freq);
} else { } else {
freqs.insert(freq); freqs.insert(freq);
} }

View file

@ -14,12 +14,12 @@ fn main() -> Result<()> {
.expect("Please provide the path to the input file"), .expect("Please provide the path to the input file"),
)?; )?;
part1(&input)?; println!("part 1: {}", part1(&input)?);
Ok(()) Ok(())
} }
fn part1(input: &str) -> Result<()> { fn part1(input: &str) -> Result<u32> {
let mut twice = 0; let mut twice = 0;
let mut thrice = 0; let mut thrice = 0;
@ -38,7 +38,5 @@ fn part1(input: &str) -> Result<()> {
} }
} }
println!("{}", twice * thrice); Ok(twice * thrice)
Ok(())
} }