Aoc 2023 day 1 part 1 finished, warming my fingers
This commit is contained in:
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user