2023: day01: part 1
This commit is contained in:
parent
516fed49be
commit
0df2dd8dd8
8 changed files with 1099 additions and 1 deletions
50
aoc2023/src/day01.rs
Normal file
50
aoc2023/src/day01.rs
Normal 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
1
aoc2023/src/lib.rs
Normal file
|
|
@ -0,0 +1 @@
|
|||
pub mod day01;
|
||||
11
aoc2023/src/main.rs
Normal file
11
aoc2023/src/main.rs
Normal 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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue