2023: day01: part 1

This commit is contained in:
Antoine Martin 2023-12-02 14:24:43 +01:00
parent 516fed49be
commit 0df2dd8dd8
8 changed files with 1099 additions and 1 deletions

9
Cargo.lock generated
View file

@ -115,6 +115,15 @@ dependencies = [
"criterion 0.4.0",
]
[[package]]
name = "aoc2023"
version = "0.1.0"
dependencies = [
"anyhow",
"aoc",
"criterion 0.4.0",
]
[[package]]
name = "atty"
version = "0.2.14"

View file

@ -1,7 +1,9 @@
[workspace]
members = ["aoc20*", "aoc20*/aoc20*_bench"]
default-members = ["aoc2022"]
default-members = ["aoc2023"]
resolver = "2"
[profile.release]
debug = true

21
aoc2023/Cargo.toml Normal file
View file

@ -0,0 +1,21 @@
[package]
name = "aoc2023"
version = "0.1.0"
authors = ["Antoine Martin <antoine@alarsyo.net>"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = { path = "../aoc" }
anyhow = "1.0"
[dev-dependencies]
criterion = { version = "0.4", default-features = false, features = [ "rayon" ] }
[lib]
path = "src/lib.rs"
[[bin]]
name = "aoc2023"
path = "src/main.rs"

1000
aoc2023/input/day01.txt Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,4 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet

50
aoc2023/src/day01.rs Normal file
View file

@ -0,0 +1,50 @@
use std::fmt::Write;
use anyhow::Result;
const INPUT: &str = include_str!("../input/day01.txt");
pub fn run() -> Result<String> {
let mut res = String::with_capacity(128);
writeln!(res, "part 1: {}", part1(INPUT)?)?;
writeln!(res, "part 2: {}", part2(INPUT)?)?;
Ok(res)
}
fn part1(input: &str) -> Result<u64> {
let digit_list = input
.lines()
.map(|s| s.chars().filter(char::is_ascii_digit).collect::<String>());
Ok(digit_list
.map(|digits| {
let first = digits.chars().next().unwrap().to_digit(10).unwrap() as u64;
let last = digits.chars().last().unwrap().to_digit(10).unwrap() as u64;
first * 10 + last
})
.sum())
}
fn part2(input: &str) -> Result<u64> {
todo!()
}
#[cfg(test)]
mod tests {
use super::*;
const PROVIDED: &str = include_str!("../input/day01_provided.txt");
#[test]
fn part1_provided() {
assert_eq!(part1(PROVIDED).unwrap(), 142);
}
#[test]
fn part1_real() {
assert_eq!(part1(INPUT).unwrap(), 54697);
}
}

1
aoc2023/src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod day01;

11
aoc2023/src/main.rs Normal file
View file

@ -0,0 +1,11 @@
use anyhow::Result;
use aoc::DayFunc;
use aoc2023::day01;
fn main() -> Result<()> {
let days: &[DayFunc] = &[day01::run];
aoc::run(days)
}