Compare commits
No commits in common. "11744d1fe9721dca3b7fd75abc430535109fa5fd" and "eca0b7d3a190b247a41ed0dfb3fd685345ba630a" have entirely different histories.
11744d1fe9
...
eca0b7d3a1
7
2021/Cargo.lock
generated
7
2021/Cargo.lock
generated
@ -6,17 +6,10 @@ version = 3
|
||||
name = "advent2021"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"aoc-runner",
|
||||
"aoc-runner-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.51"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8b26702f315f53b6071259e15dd9d64528213b44d61de1ec926eca7715d62203"
|
||||
|
||||
[[package]]
|
||||
name = "aoc-runner"
|
||||
version = "0.3.0"
|
||||
|
||||
@ -6,6 +6,5 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.45"
|
||||
aoc-runner = "0.3.0"
|
||||
aoc-runner-derive = "0.3.0"
|
||||
|
||||
@ -73,34 +73,36 @@
|
||||
//!
|
||||
//! Consider sums of a three-measurement sliding window. How many sums are larger than the previous sum?
|
||||
|
||||
use anyhow::Result;
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`.
|
||||
/// Reads text file containing one integer per line, and parses them into `Vec<u32>`. Any
|
||||
/// non-number will result in a panice.
|
||||
#[aoc_generator(day1)]
|
||||
fn parse(input: &str) -> Result<Vec<u32>> {
|
||||
input.split("\n").map(|s| Ok(s.parse()?)).collect()
|
||||
fn parse(input: &str) -> Vec<u32> {
|
||||
input
|
||||
.split("\n")
|
||||
.map(|s| s.parse().expect("not a number"))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[aoc(day1, part1)]
|
||||
fn part1(depths: &[u32]) -> Result<u32> {
|
||||
Ok(depths
|
||||
fn part1(depths: &[u32]) -> u32 {
|
||||
depths
|
||||
.windows(2)
|
||||
.map(|s| if s[0] < s[1] { 1 } else { 0 })
|
||||
.sum())
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[aoc(day1, part2)]
|
||||
fn part2(depths: &[u32]) -> Result<u32> {
|
||||
fn part2(depths: &[u32]) -> u32 {
|
||||
let sums: Vec<u32> = depths.windows(3).map(|s| s.iter().sum()).collect();
|
||||
Ok(sums
|
||||
.windows(2)
|
||||
sums.windows(2)
|
||||
.map(|s| if s[0] < s[1] { 1 } else { 0 })
|
||||
.sum())
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1() -> Result<()> {
|
||||
fn test_part1() {
|
||||
assert_eq!(
|
||||
part1(&parse(
|
||||
r#"199
|
||||
@ -113,14 +115,13 @@ fn test_part1() -> Result<()> {
|
||||
269
|
||||
260
|
||||
263"#
|
||||
)?)?,
|
||||
)),
|
||||
7
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() -> Result<()> {
|
||||
fn test_part2() {
|
||||
assert_eq!(
|
||||
part2(&parse(
|
||||
r#"199
|
||||
@ -133,8 +134,7 @@ fn test_part2() -> Result<()> {
|
||||
269
|
||||
260
|
||||
263"#
|
||||
)?)?,
|
||||
)),
|
||||
5
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -52,50 +52,49 @@
|
||||
//!
|
||||
//! 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};
|
||||
|
||||
#[aoc(day2, part1)]
|
||||
fn part1(input: &str) -> Result<i32> {
|
||||
fn part1(input: &str) -> i32 {
|
||||
let mut horizontal: i32 = 0;
|
||||
let mut depth: i32 = 0;
|
||||
for l in input.split("\n") {
|
||||
input.split("\n").for_each(|l| {
|
||||
let p: Vec<_> = l.split(" ").collect();
|
||||
|
||||
match p[0] {
|
||||
"forward" => horizontal += p[1].parse::<i32>()?,
|
||||
"up" => depth -= p[1].parse::<i32>()?,
|
||||
"down" => depth += p[1].parse::<i32>()?,
|
||||
"forward" => horizontal += p[1].parse::<i32>().expect("forward"),
|
||||
"up" => depth -= p[1].parse::<i32>().expect("up"),
|
||||
"down" => depth += p[1].parse::<i32>().expect("down"),
|
||||
_ => panic!("unknown command {}", p[0]),
|
||||
}
|
||||
}
|
||||
Ok(horizontal * depth)
|
||||
});
|
||||
horizontal * depth
|
||||
}
|
||||
|
||||
#[aoc(day2, part2)]
|
||||
fn part2(input: &str) -> Result<i32> {
|
||||
fn part2(input: &str) -> i32 {
|
||||
let mut horizontal: i32 = 0;
|
||||
let mut depth: i32 = 0;
|
||||
let mut aim: i32 = 0;
|
||||
for l in input.split("\n") {
|
||||
input.split("\n").for_each(|l| {
|
||||
let p: Vec<_> = l.split(" ").collect();
|
||||
|
||||
match p[0] {
|
||||
"forward" => {
|
||||
let v = p[1].parse::<i32>()?;
|
||||
let v = p[1].parse::<i32>().expect("forward");
|
||||
horizontal += v;
|
||||
depth += v * aim;
|
||||
}
|
||||
"up" => aim -= p[1].parse::<i32>()?,
|
||||
"down" => aim += p[1].parse::<i32>()?,
|
||||
"up" => aim -= p[1].parse::<i32>().expect("up"),
|
||||
"down" => aim += p[1].parse::<i32>().expect("down"),
|
||||
_ => panic!("unknown command {}", p[0]),
|
||||
}
|
||||
}
|
||||
Ok(horizontal * depth)
|
||||
});
|
||||
horizontal * depth
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part1() -> Result<()> {
|
||||
fn test_part1() {
|
||||
let input = r#"
|
||||
forward 5
|
||||
down 5
|
||||
@ -105,12 +104,11 @@ down 8
|
||||
forward 2
|
||||
"#
|
||||
.trim();
|
||||
assert_eq!(part1(input)?, 150);
|
||||
Ok(())
|
||||
assert_eq!(part1(input), 150);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_part2() -> Result<()> {
|
||||
fn test_part2() {
|
||||
let input = r#"
|
||||
forward 5
|
||||
down 5
|
||||
@ -120,6 +118,5 @@ down 8
|
||||
forward 2
|
||||
"#
|
||||
.trim();
|
||||
assert_eq!(part2(input)?, 900);
|
||||
Ok(())
|
||||
assert_eq!(part2(input), 900);
|
||||
}
|
||||
|
||||
@ -1,42 +1,36 @@
|
||||
use anyhow::Result;
|
||||
use aoc_runner_derive::{aoc, aoc_generator};
|
||||
|
||||
#[aoc_generator(dayX)]
|
||||
fn parse(input: &str) -> Result<Vec<u32>> {
|
||||
todo!("parse");
|
||||
Ok(Vec::new())
|
||||
fn parse(input: &str) -> Vec<u32> {
|
||||
todo!("parse")
|
||||
}
|
||||
|
||||
#[aoc(dayX, part1)]
|
||||
fn part1(depths: &[u32]) -> Result<u32> {
|
||||
todo!("part1");
|
||||
Ok(())
|
||||
fn part1(depths: &[u32]) -> u32 {
|
||||
todo!("part1")
|
||||
}
|
||||
|
||||
/*
|
||||
#[aoc(dayX, part2)]
|
||||
fn part2(depths: &[u32]) -> Result<u32> {
|
||||
fn part2(depths: &[u32]) -> u32 {
|
||||
todo!("part2")
|
||||
Ok(())
|
||||
}
|
||||
*/
|
||||
|
||||
#[test]
|
||||
fn test_part1() -> Result<()> {
|
||||
fn test_part1() {
|
||||
let input = r#"
|
||||
"#
|
||||
.trim();
|
||||
assert_eq!(part1(&parse(input)?)?, TODO);
|
||||
Ok(())
|
||||
assert_eq!(part1(&parse(input)), TODO);
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn test_part2()->Result<()> {
|
||||
fn test_part2() {
|
||||
let input = r#"
|
||||
"#
|
||||
.trim();
|
||||
assert_eq!(part2(&parse(input)?)?, TODO);
|
||||
Ok(())
|
||||
assert_eq!(part2(&parse(input)), TODO);
|
||||
}
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user