2018: switch to anyhow
This commit is contained in:
parent
885081d3fe
commit
60b5398737
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -27,6 +27,7 @@ dependencies = [
|
||||||
name = "aoc2018"
|
name = "aoc2018"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"aoc",
|
"aoc",
|
||||||
"criterion",
|
"criterion",
|
||||||
]
|
]
|
||||||
|
|
|
@ -13,6 +13,7 @@ criterion = "0.3"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
aoc = { path = "../aoc" }
|
aoc = { path = "../aoc" }
|
||||||
|
anyhow = "1.0"
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
path = "src/lib.rs"
|
path = "src/lib.rs"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use aoc::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../input/day01.txt");
|
const INPUT: &str = include_str!("../input/day01.txt");
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use aoc::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../input/day02.txt");
|
const INPUT: &str = include_str!("../input/day02.txt");
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::error::Error;
|
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use aoc::err;
|
use anyhow::{Context, Result};
|
||||||
use aoc::Result;
|
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../input/day03.txt");
|
const INPUT: &str = include_str!("../input/day03.txt");
|
||||||
|
|
||||||
|
@ -27,7 +25,7 @@ struct Claim {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Claim {
|
impl FromStr for Claim {
|
||||||
type Err = Box<dyn Error>;
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
/// Parses a claim from a line
|
/// Parses a claim from a line
|
||||||
///
|
///
|
||||||
|
@ -41,22 +39,20 @@ impl FromStr for Claim {
|
||||||
let s = &s[1..];
|
let s = &s[1..];
|
||||||
|
|
||||||
// find ' @ ' separator
|
// find ' @ ' separator
|
||||||
let at = s
|
let at = s.find(" @ ").context("` @ ` delimiter not found")?;
|
||||||
.find(" @ ")
|
|
||||||
.ok_or_else(|| err!("` @ ` delimiter not found"))?;
|
|
||||||
let id = s[..at].parse()?;
|
let id = s[..at].parse()?;
|
||||||
let s = &s[(at + 3)..];
|
let s = &s[(at + 3)..];
|
||||||
|
|
||||||
// parse 'X,Y: WxH
|
// parse 'X,Y: WxH
|
||||||
let comma = s.find(',').ok_or_else(|| err!("`,` delimiter not found"))?;
|
let comma = s.find(',').context("`,` delimiter not found")?;
|
||||||
let colon = s.find(':').ok_or_else(|| err!("`:` delimiter not found"))?;
|
let colon = s.find(':').context("`:` delimiter not found")?;
|
||||||
let x = s[..comma].parse()?;
|
let x = s[..comma].parse()?;
|
||||||
let y = s[(comma + 1)..colon].parse()?;
|
let y = s[(comma + 1)..colon].parse()?;
|
||||||
|
|
||||||
// reduce line to 'WxH'
|
// reduce line to 'WxH'
|
||||||
let s = &s[(colon + 2)..];
|
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 width = s[..sep].parse()?;
|
||||||
let height = s[(sep + 1)..].parse()?;
|
let height = s[(sep + 1)..].parse()?;
|
||||||
|
|
||||||
|
@ -77,7 +73,7 @@ fn part1(input: &str) -> Result<u64> {
|
||||||
for line in input.lines() {
|
for line in input.lines() {
|
||||||
let claim: Claim = line
|
let claim: Claim = line
|
||||||
.parse()
|
.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 i in 0..claim.width {
|
||||||
for j in 0..claim.height {
|
for j in 0..claim.height {
|
||||||
|
@ -106,7 +102,7 @@ fn part2(input: &str) -> Result<usize> {
|
||||||
for line in input.lines() {
|
for line in input.lines() {
|
||||||
let claim: Claim = line
|
let claim: Claim = line
|
||||||
.parse()
|
.parse()
|
||||||
.or_else(|e| Err(err!("couldn't parse line: `{}`, {}", line, e)))?;
|
.with_context(|| format!("couldn't parse line: `{}`", line))?;
|
||||||
set.insert(claim.id);
|
set.insert(claim.id);
|
||||||
|
|
||||||
for i in 0..claim.width {
|
for i in 0..claim.width {
|
||||||
|
|
|
@ -1,10 +1,8 @@
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::error::Error;
|
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use aoc::err;
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
use aoc::Result;
|
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../input/day04.txt");
|
const INPUT: &str = include_str!("../input/day04.txt");
|
||||||
|
|
||||||
|
@ -30,7 +28,7 @@ enum Event {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Event {
|
impl FromStr for Event {
|
||||||
type Err = Box<dyn Error>;
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
if s.find("wakes up").is_some() {
|
if s.find("wakes up").is_some() {
|
||||||
|
@ -38,13 +36,13 @@ impl FromStr for Event {
|
||||||
} else if s.find("falls asleep").is_some() {
|
} else if s.find("falls asleep").is_some() {
|
||||||
Ok(Event::FallAsleep)
|
Ok(Event::FallAsleep)
|
||||||
} else if s.find("begins shift").is_some() {
|
} 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 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()?;
|
let id = s[..space].parse()?;
|
||||||
Ok(Event::ShiftChange(id))
|
Ok(Event::ShiftChange(id))
|
||||||
} else {
|
} else {
|
||||||
Err(err!("unknown event type"))
|
Err(anyhow!("unknown event type"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -60,28 +58,28 @@ struct Date {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for Date {
|
impl FromStr for Date {
|
||||||
type Err = Box<dyn Error>;
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
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 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 year = s[..dash].parse()?;
|
||||||
let s = &s[(dash + 1)..];
|
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 month = s[..dash].parse()?;
|
||||||
let s = &s[(dash + 1)..];
|
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 day = s[..space].parse()?;
|
||||||
let s = &s[(space + 1)..];
|
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 hour = s[..colon].parse()?;
|
||||||
let s = &s[(colon + 1)..];
|
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()?;
|
let minute = s[..rbracket].parse()?;
|
||||||
|
|
||||||
|
@ -102,11 +100,11 @@ struct LogEntry {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FromStr for LogEntry {
|
impl FromStr for LogEntry {
|
||||||
type Err = Box<dyn Error>;
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self> {
|
fn from_str(s: &str) -> Result<Self> {
|
||||||
let date: Date = s.parse().map_err(|e| err!("couldn't parse date: {}", e))?;
|
let date: Date = s.parse().context("couldn't parse date")?;
|
||||||
let event = s.parse().map_err(|e| err!("couldn't parse event: {}", e))?;
|
let event = s.parse().context("couldn't parse event")?;
|
||||||
|
|
||||||
let entry = LogEntry { date, event };
|
let entry = LogEntry { date, event };
|
||||||
|
|
||||||
|
@ -134,7 +132,7 @@ fn part1(input: &str) -> Result<u64> {
|
||||||
|
|
||||||
match guard_id {
|
match guard_id {
|
||||||
Some(id) => map.entry(id).or_default().push(log_entry),
|
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;
|
fell_asleep = None;
|
||||||
}
|
}
|
||||||
None => return Err(err!("woke up before falling asleep")),
|
None => bail!("woke up before falling asleep"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if fell_asleep.is_some() {
|
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);
|
sleep_freq_per_guard.insert(id, sleep_freq);
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use aoc::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../input/day05.txt");
|
const INPUT: &str = include_str!("../input/day05.txt");
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
use aoc::DayFunc;
|
use aoc::DayFunc;
|
||||||
use aoc::Result;
|
|
||||||
|
|
||||||
use aoc2018::day01;
|
use aoc2018::day01;
|
||||||
use aoc2018::day02;
|
use aoc2018::day02;
|
||||||
|
|
Loading…
Reference in a new issue