1
0
Fork 0

AOC 2023: day1, part1.

This commit is contained in:
Yohan Boujon 2023-12-03 11:53:46 +01:00
parent e8ec79650e
commit 163fd52444
3 changed files with 1029 additions and 2 deletions

4
2023/rust/day1/example Normal file
View file

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

1000
2023/rust/day1/input Normal file

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,26 @@
fn main() { use std::fs::read_to_string;
println!("Hello, world!");
fn part1(str: &String) -> Vec<u32> {
let mut num_vec = Vec::new();
let line : Vec<&str> = str.split('\n').collect();
for l in line {
let num : Vec<u8> = l.as_bytes().iter().filter(|ch| ch.is_ascii_digit()).cloned().collect();
let mut str_num = String::new();
if num.len() >= 2 {
str_num.push(num.first().unwrap().clone() as char);
str_num.push(num.last().unwrap().clone() as char);
} else {
str_num.push(num.first().unwrap().clone() as char);
str_num.push(num.first().unwrap().clone() as char);
}
num_vec.push(str_num);
}
println!("{:?}", num_vec);
num_vec.iter().map(|str| str.parse().unwrap()).collect()
}
fn main() {
let str = read_to_string("input").unwrap();
let part1_result : u32 = part1(&str).iter().sum();
println!("total part1: {}", part1_result);
} }