Aoc 2023 day 1 part 1 finished, warming my fingers
This commit is contained in:
8
advent_of_code/2023/1/Cargo.toml
Normal file
8
advent_of_code/2023/1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "main"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
1000
advent_of_code/2023/1/input.txt
Normal file
1000
advent_of_code/2023/1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
44
advent_of_code/2023/1/src/main.rs
Normal file
44
advent_of_code/2023/1/src/main.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
fn solve_1(input: &str) -> u32 {
|
||||
input
|
||||
.lines()
|
||||
.map(|l| {
|
||||
// Digits are 0-9, so we know that if they
|
||||
// are still 10 they haven't been changed
|
||||
let mut first = 10;
|
||||
let mut last = 10;
|
||||
|
||||
for c in l.chars() {
|
||||
if let Some(digit) = c.to_digit(10) {
|
||||
if first == 10 {
|
||||
first = digit;
|
||||
}
|
||||
last = digit;
|
||||
}
|
||||
}
|
||||
|
||||
first * 10 + last
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, this is Patrick!");
|
||||
|
||||
let input_txt = include_str!("../input.txt");
|
||||
|
||||
let result_1 = solve_1(input_txt);
|
||||
|
||||
println!("The sum of all calibration values is {}", result_1);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_1() {
|
||||
let test_input = include_str!("../test.txt");
|
||||
|
||||
assert_eq!(solve_1(test_input), 142);
|
||||
}
|
||||
}
|
||||
4
advent_of_code/2023/1/test.txt
Normal file
4
advent_of_code/2023/1/test.txt
Normal file
@@ -0,0 +1,4 @@
|
||||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
||||
Reference in New Issue
Block a user