2018: day03: simplify error
This commit is contained in:
parent
2274074cdd
commit
f23ec6ce90
|
@ -1,7 +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;
|
use std::fmt;
|
||||||
|
|
||||||
|
use super::err;
|
||||||
use super::Result;
|
use super::Result;
|
||||||
|
|
||||||
const INPUT: &str = include_str!("../input/day03.txt");
|
const INPUT: &str = include_str!("../input/day03.txt");
|
||||||
|
@ -13,31 +15,6 @@ pub fn run() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct ParseError {
|
|
||||||
line: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ParseError {
|
|
||||||
fn new(line: &str) -> Self {
|
|
||||||
ParseError {
|
|
||||||
line: line.to_string(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for ParseError {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
||||||
write!(f, "{}", self.line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::error::Error for ParseError {
|
|
||||||
fn description(&self) -> &str {
|
|
||||||
&self.line
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Claim {
|
struct Claim {
|
||||||
x: usize,
|
x: usize,
|
||||||
y: usize,
|
y: usize,
|
||||||
|
@ -89,7 +66,7 @@ fn part1(input: &str) -> Result<u64> {
|
||||||
let mut map: HashMap<(usize, usize), u64> = HashMap::default();
|
let mut map: HashMap<(usize, usize), u64> = HashMap::default();
|
||||||
|
|
||||||
for line in input.lines() {
|
for line in input.lines() {
|
||||||
let claim = parse(line).ok_or(ParseError::new(line))?;
|
let claim = parse(line).ok_or(err!("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 {
|
||||||
|
@ -116,7 +93,7 @@ fn part2(input: &str) -> Result<usize> {
|
||||||
let mut set = HashSet::new();
|
let mut set = HashSet::new();
|
||||||
|
|
||||||
for line in input.lines() {
|
for line in input.lines() {
|
||||||
let claim = parse(line).ok_or(ParseError::new(line))?;
|
let claim = parse(line).ok_or(err!("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 {
|
||||||
|
|
|
@ -3,3 +3,8 @@ pub mod day02;
|
||||||
pub mod day03;
|
pub mod day03;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! err {
|
||||||
|
($($string:expr),+) => (Box::<dyn Error>::from(format!($($string),+)))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue