Use anyhow to make ? availble
This commit is contained in:
parent
eca0b7d3a1
commit
11c5dcaaaf
7
2021/Cargo.lock
generated
7
2021/Cargo.lock
generated
@ -6,10 +6,17 @@ version = 3
|
|||||||
name = "advent2021"
|
name = "advent2021"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
"aoc-runner",
|
"aoc-runner",
|
||||||
"aoc-runner-derive",
|
"aoc-runner-derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.51"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aoc-runner"
|
name = "aoc-runner"
|
||||||
version = "0.3.0"
|
version = "0.3.0"
|
||||||
|
|||||||
@ -6,5 +6,6 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
anyhow = "1.0.45"
|
||||||
aoc-runner = "0.3.0"
|
aoc-runner = "0.3.0"
|
||||||
aoc-runner-derive = "0.3.0"
|
aoc-runner-derive = "0.3.0"
|
||||||
|
|||||||
@ -52,49 +52,50 @@
|
|||||||
//!
|
//!
|
||||||
//! Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?
|
//! Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
use aoc_runner_derive::{aoc, aoc_generator};
|
use aoc_runner_derive::{aoc, aoc_generator};
|
||||||
|
|
||||||
#[aoc(day2, part1)]
|
#[aoc(day2, part1)]
|
||||||
fn part1(input: &str) -> i32 {
|
fn part1(input: &str) -> Result<i32> {
|
||||||
let mut horizontal: i32 = 0;
|
let mut horizontal: i32 = 0;
|
||||||
let mut depth: i32 = 0;
|
let mut depth: i32 = 0;
|
||||||
input.split("\n").for_each(|l| {
|
for l in input.split("\n") {
|
||||||
let p: Vec<_> = l.split(" ").collect();
|
let p: Vec<_> = l.split(" ").collect();
|
||||||
|
|
||||||
match p[0] {
|
match p[0] {
|
||||||
"forward" => horizontal += p[1].parse::<i32>().expect("forward"),
|
"forward" => horizontal += p[1].parse::<i32>()?,
|
||||||
"up" => depth -= p[1].parse::<i32>().expect("up"),
|
"up" => depth -= p[1].parse::<i32>()?,
|
||||||
"down" => depth += p[1].parse::<i32>().expect("down"),
|
"down" => depth += p[1].parse::<i32>()?,
|
||||||
_ => panic!("unknown command {}", p[0]),
|
_ => panic!("unknown command {}", p[0]),
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
horizontal * depth
|
Ok(horizontal * depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[aoc(day2, part2)]
|
#[aoc(day2, part2)]
|
||||||
fn part2(input: &str) -> i32 {
|
fn part2(input: &str) -> Result<i32> {
|
||||||
let mut horizontal: i32 = 0;
|
let mut horizontal: i32 = 0;
|
||||||
let mut depth: i32 = 0;
|
let mut depth: i32 = 0;
|
||||||
let mut aim: i32 = 0;
|
let mut aim: i32 = 0;
|
||||||
input.split("\n").for_each(|l| {
|
for l in input.split("\n") {
|
||||||
let p: Vec<_> = l.split(" ").collect();
|
let p: Vec<_> = l.split(" ").collect();
|
||||||
|
|
||||||
match p[0] {
|
match p[0] {
|
||||||
"forward" => {
|
"forward" => {
|
||||||
let v = p[1].parse::<i32>().expect("forward");
|
let v = p[1].parse::<i32>()?;
|
||||||
horizontal += v;
|
horizontal += v;
|
||||||
depth += v * aim;
|
depth += v * aim;
|
||||||
}
|
}
|
||||||
"up" => aim -= p[1].parse::<i32>().expect("up"),
|
"up" => aim -= p[1].parse::<i32>()?,
|
||||||
"down" => aim += p[1].parse::<i32>().expect("down"),
|
"down" => aim += p[1].parse::<i32>()?,
|
||||||
_ => panic!("unknown command {}", p[0]),
|
_ => panic!("unknown command {}", p[0]),
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
horizontal * depth
|
Ok(horizontal * depth)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part1() {
|
fn test_part1() -> Result<()> {
|
||||||
let input = r#"
|
let input = r#"
|
||||||
forward 5
|
forward 5
|
||||||
down 5
|
down 5
|
||||||
@ -104,11 +105,12 @@ down 8
|
|||||||
forward 2
|
forward 2
|
||||||
"#
|
"#
|
||||||
.trim();
|
.trim();
|
||||||
assert_eq!(part1(input), 150);
|
assert_eq!(part1(input)?, 150);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part2() {
|
fn test_part2() -> Result<()> {
|
||||||
let input = r#"
|
let input = r#"
|
||||||
forward 5
|
forward 5
|
||||||
down 5
|
down 5
|
||||||
@ -118,5 +120,6 @@ down 8
|
|||||||
forward 2
|
forward 2
|
||||||
"#
|
"#
|
||||||
.trim();
|
.trim();
|
||||||
assert_eq!(part2(input), 900);
|
assert_eq!(part2(input)?, 900);
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user