AOC 2023: day1, part1.
This commit is contained in:
parent
e8ec79650e
commit
163fd52444
3 changed files with 1029 additions and 2 deletions
4
2023/rust/day1/example
Normal file
4
2023/rust/day1/example
Normal file
|
@ -0,0 +1,4 @@
|
|||
1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
1000
2023/rust/day1/input
Normal file
1000
2023/rust/day1/input
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,3 +1,26 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
use std::fs::read_to_string;
|
||||
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue