2020: switch to anyhow
This commit is contained in:
parent
f148347e5b
commit
d91165207f
18 changed files with 161 additions and 179 deletions
|
|
@ -1,10 +1,10 @@
|
|||
use std::fmt::Write;
|
||||
|
||||
use aoc::err;
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
const INPUT: &str = include_str!("../input/day13.txt");
|
||||
|
||||
pub fn run() -> aoc::Result<String> {
|
||||
pub fn run() -> Result<String> {
|
||||
let mut res = String::with_capacity(128);
|
||||
|
||||
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
||||
|
|
@ -13,27 +13,27 @@ pub fn run() -> aoc::Result<String> {
|
|||
Ok(res)
|
||||
}
|
||||
|
||||
fn part1(input: &str) -> aoc::Result<u64> {
|
||||
fn part1(input: &str) -> Result<u64> {
|
||||
let mut lines = input.lines();
|
||||
|
||||
let earliest_timestamp = lines
|
||||
.next()
|
||||
.ok_or_else(|| err!("input was empty"))?
|
||||
.context("input was empty")?
|
||||
.parse::<u64>()
|
||||
.map_err(|e| err!("couldn't parse first line: {}", e))?;
|
||||
.context("couldn't parse first line")?;
|
||||
|
||||
let bus_ids = lines
|
||||
.next()
|
||||
.ok_or_else(|| err!("no second line"))?
|
||||
.context("no second line")?
|
||||
.split(',')
|
||||
.filter_map(|num| {
|
||||
if num == "x" {
|
||||
None
|
||||
} else {
|
||||
Some(num.parse::<u64>().map_err(|e| err!("{}", e)))
|
||||
Some(num.parse::<u64>().map_err(anyhow::Error::new))
|
||||
}
|
||||
})
|
||||
.collect::<aoc::Result<Vec<_>>>()?;
|
||||
.collect::<Result<Vec<_>>>()?;
|
||||
|
||||
let (bus_id, earliest_departure) = bus_ids
|
||||
.iter()
|
||||
|
|
@ -47,16 +47,16 @@ fn part1(input: &str) -> aoc::Result<u64> {
|
|||
Ok(bus_id * (earliest_departure - earliest_timestamp))
|
||||
}
|
||||
|
||||
fn part2(input: &str) -> aoc::Result<u64> {
|
||||
fn part2(input: &str) -> Result<u64> {
|
||||
let mut lines = input.lines();
|
||||
|
||||
// we don't need the first line anymore, skip it
|
||||
lines.next().ok_or_else(|| err!("input was empty"))?;
|
||||
lines.next().context("input was empty")?;
|
||||
|
||||
find_timestamp(lines.next().ok_or_else(|| err!("no second line"))?)
|
||||
find_timestamp(lines.next().context("no second line")?)
|
||||
}
|
||||
|
||||
fn find_timestamp(input: &str) -> aoc::Result<u64> {
|
||||
fn find_timestamp(input: &str) -> Result<u64> {
|
||||
let bus_ids: Vec<(u64, u64)> = input
|
||||
.split(',')
|
||||
.enumerate()
|
||||
|
|
@ -64,14 +64,14 @@ fn find_timestamp(input: &str) -> aoc::Result<u64> {
|
|||
if num == "x" {
|
||||
None
|
||||
} else {
|
||||
Some((idx as u64, num.parse::<u64>().map_err(|e| err!("{}", e))))
|
||||
Some((idx as u64, num.parse::<u64>().map_err(anyhow::Error::new)))
|
||||
}
|
||||
})
|
||||
.map(|(idx, res)| match res {
|
||||
Ok(num) => Ok((idx, num)),
|
||||
Err(e) => Err(e),
|
||||
})
|
||||
.collect::<aoc::Result<_>>()?;
|
||||
.collect::<Result<_>>()?;
|
||||
|
||||
// previous constraints is empty for now
|
||||
let mut current_solution = 0;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue