Skip to content
Snippets Groups Projects
Verified Commit 832728e4 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

Dabbling in Rust, 2023 Day 01

parent e4b8d8e7
No related branches found
No related tags found
No related merge requests found
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent_of_code"
version = "0.1.0"
[package]
name = "advent_of_code"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fn day01(data: Vec<&str>) -> u32 {
let mut values: Vec<u32> = Vec::new();
for datum in data {
let mut first_digit: Option<u32> = None;
let mut last_digit: Option<u32> = Option::from(0);
for character in datum.chars() {
if character.is_digit(10) {
if first_digit == None {
first_digit = character.to_digit(10);
}
last_digit = character.to_digit(10);
}
}
if first_digit == None {
first_digit = Option::from(0);
}
values.push(10 * first_digit.expect("this shouldn't happen") + last_digit.expect("this shouldn't happen"));
}
let mut value: u32 = 0;
return loop {
if values.is_empty() {
break value;
} else {
value += values.pop().expect("this really shouldn't happen");
}
}
}
fn main() {
println!("Hello, world!");
let day01a_data = vec!["1abc2", "pqr3stu8vwx", "a1b2c3d4e5f", "treb7uchet"];
let day01b_data = vec!["two1nine", "eightwothree", "abcone2threexyz", "xtwone3four",
"4nineeightseven2", "zoneight234", "7pqrstsixteen"];
println!("{}", day01(day01a_data));
// day01(day01b_data);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment