2018: switch to anyhow

This commit is contained in:
Antoine Martin 2020-12-14 20:24:09 +01:00
parent 885081d3fe
commit 60b5398737
8 changed files with 33 additions and 36 deletions

1
Cargo.lock generated
View file

@ -27,6 +27,7 @@ dependencies = [
name = "aoc2018"
version = "0.1.0"
dependencies = [
"anyhow",
"aoc",
"criterion",
]

View file

@ -13,6 +13,7 @@ criterion = "0.3"
[dependencies]
aoc = { path = "../aoc" }
anyhow = "1.0"
[lib]
path = "src/lib.rs"

View file

@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::fmt::Write;
use aoc::Result;
use anyhow::Result;
const INPUT: &str = include_str!("../input/day01.txt");

View file

@ -1,7 +1,7 @@
use std::collections::HashMap;
use std::fmt::Write;
use aoc::Result;
use anyhow::Result;
const INPUT: &str = include_str!("../input/day02.txt");

View file

@ -1,11 +1,9 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
use aoc::Result;
use anyhow::{Context, Result};
const INPUT: &str = include_str!("../input/day03.txt");
@ -27,7 +25,7 @@ struct Claim {
}
impl FromStr for Claim {
type Err = Box<dyn Error>;
type Err = anyhow::Error;
/// Parses a claim from a line
///
@ -41,22 +39,20 @@ impl FromStr for Claim {
let s = &s[1..];
// find ' @ ' separator
let at = s
.find(" @ ")
.ok_or_else(|| err!("` @ ` delimiter not found"))?;
let at = s.find(" @ ").context("` @ ` delimiter not found")?;
let id = s[..at].parse()?;
let s = &s[(at + 3)..];
// parse 'X,Y: WxH
let comma = s.find(',').ok_or_else(|| err!("`,` delimiter not found"))?;
let colon = s.find(':').ok_or_else(|| err!("`:` delimiter not found"))?;
let comma = s.find(',').context("`,` delimiter not found")?;
let colon = s.find(':').context("`:` delimiter not found")?;
let x = s[..comma].parse()?;
let y = s[(comma + 1)..colon].parse()?;
// reduce line to 'WxH'
let s = &s[(colon + 2)..];
let sep = s.find('x').ok_or_else(|| err!("`x` delimiter not found"))?;
let sep = s.find('x').context("`x` delimiter not found")?;
let width = s[..sep].parse()?;
let height = s[(sep + 1)..].parse()?;
@ -77,7 +73,7 @@ fn part1(input: &str) -> Result<u64> {
for line in input.lines() {
let claim: Claim = line
.parse()
.or_else(|e| Err(err!("couldn't parse line: `{}`, {}", line, e)))?;
.with_context(|| format!("couldn't parse line: `{}`", line))?;
for i in 0..claim.width {
for j in 0..claim.height {
@ -106,7 +102,7 @@ fn part2(input: &str) -> Result<usize> {
for line in input.lines() {
let claim: Claim = line
.parse()
.or_else(|e| Err(err!("couldn't parse line: `{}`, {}", line, e)))?;
.with_context(|| format!("couldn't parse line: `{}`", line))?;
set.insert(claim.id);
for i in 0..claim.width {

View file

@ -1,10 +1,8 @@
use std::collections::HashMap;
use std::error::Error;
use std::fmt::Write;
use std::str::FromStr;
use aoc::err;
use aoc::Result;
use anyhow::{anyhow, bail, Context, Result};
const INPUT: &str = include_str!("../input/day04.txt");
@ -30,7 +28,7 @@ enum Event {
}
impl FromStr for Event {
type Err = Box<dyn Error>;
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
if s.find("wakes up").is_some() {
@ -38,13 +36,13 @@ impl FromStr for Event {
} else if s.find("falls asleep").is_some() {
Ok(Event::FallAsleep)
} else if s.find("begins shift").is_some() {
let pound = s.find('#').ok_or_else(|| err!("`#` not found"))?;
let pound = s.find('#').context("`#` not found")?;
let s = &s[(pound + 1)..];
let space = s.find(' ').ok_or_else(|| err!("` ` not found after `#`"))?;
let space = s.find(' ').context("` ` not found after `#`")?;
let id = s[..space].parse()?;
Ok(Event::ShiftChange(id))
} else {
Err(err!("unknown event type"))
Err(anyhow!("unknown event type"))
}
}
}
@ -60,28 +58,28 @@ struct Date {
}
impl FromStr for Date {
type Err = Box<dyn Error>;
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
let lbracket = s.find('[').ok_or_else(|| err!("`[` not found"))?;
let lbracket = s.find('[').context("`[` not found")?;
let s = &s[(lbracket + 1)..];
let dash = s.find('-').ok_or_else(|| err!("`-` not found"))?;
let dash = s.find('-').context("`-` not found")?;
let year = s[..dash].parse()?;
let s = &s[(dash + 1)..];
let dash = s.find('-').ok_or_else(|| err!("`-` not found"))?;
let dash = s.find('-').context("`-` not found")?;
let month = s[..dash].parse()?;
let s = &s[(dash + 1)..];
let space = s.find(' ').ok_or_else(|| err!("` ` not found"))?;
let space = s.find(' ').context("` ` not found")?;
let day = s[..space].parse()?;
let s = &s[(space + 1)..];
let colon = s.find(':').ok_or_else(|| err!("`:` not found"))?;
let colon = s.find(':').context("`:` not found")?;
let hour = s[..colon].parse()?;
let s = &s[(colon + 1)..];
let rbracket = s.find(']').ok_or_else(|| err!("`]` not found"))?;
let rbracket = s.find(']').context("`]` not found")?;
let minute = s[..rbracket].parse()?;
@ -102,11 +100,11 @@ struct LogEntry {
}
impl FromStr for LogEntry {
type Err = Box<dyn Error>;
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
let date: Date = s.parse().map_err(|e| err!("couldn't parse date: {}", e))?;
let event = s.parse().map_err(|e| err!("couldn't parse event: {}", e))?;
let date: Date = s.parse().context("couldn't parse date")?;
let event = s.parse().context("couldn't parse event")?;
let entry = LogEntry { date, event };
@ -134,7 +132,7 @@ fn part1(input: &str) -> Result<u64> {
match guard_id {
Some(id) => map.entry(id).or_default().push(log_entry),
None => return Err(err!("event before first shift")),
None => bail!("event before first shift"),
}
}
@ -157,13 +155,13 @@ fn part1(input: &str) -> Result<u64> {
}
fell_asleep = None;
}
None => return Err(err!("woke up before falling asleep")),
None => bail!("woke up before falling asleep"),
},
}
}
if fell_asleep.is_some() {
return Err(err!("fell asleep but never woke up!"));
bail!("fell asleep but never woke up!");
}
sleep_freq_per_guard.insert(id, sleep_freq);

View file

@ -1,7 +1,7 @@
use std::collections::HashSet;
use std::fmt::Write;
use aoc::Result;
use anyhow::Result;
const INPUT: &str = include_str!("../input/day05.txt");

View file

@ -1,5 +1,6 @@
use anyhow::Result;
use aoc::DayFunc;
use aoc::Result;
use aoc2018::day01;
use aoc2018::day02;