day 1 part 1

This commit is contained in:
Adrian Hedqvist 2023-12-05 18:56:32 +01:00
commit 7a7202d5c8
5 changed files with 1049 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
target/

7
day1/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day1"
version = "0.1.0"

8
day1/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "day1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1000
day1/input.txt Normal file

File diff suppressed because it is too large Load diff

33
day1/src/main.rs Normal file
View file

@ -0,0 +1,33 @@
fn main() {
let input = std::fs::read_to_string("input.txt").unwrap();
let result = calibration_value(&input);
println!("Result: {}", result);
}
fn calibration_value(input: &str) -> u32 {
input.lines()
.map(|line| {
let digits: Vec<char> = line.chars().filter(|c| c.is_numeric()).collect();
let first = digits.first().unwrap().to_digit(10).unwrap();
let last = digits.last().unwrap().to_digit(10).unwrap();
dbg!(first * 10 + last)
})
.sum()
}
#[cfg(test)]
mod tests {
use crate::calibration_value;
const EXAMPLE: &str = "1abc2\npqr3stu8vwx\na1b2c3d4e5f\ntreb7uchet";
#[test]
fn example() {
let result = calibration_value(EXAMPLE);
assert_eq!(142, result);
}
}