Compare commits

..

No commits in common. "c5e0ac820a5018bcea08df3e59dc326789b354bc" and "de452d97c031454a9d27f5e983a55da3e9b49844" have entirely different histories.

4 changed files with 1 additions and 1108 deletions

7
Cargo.lock generated
View File

@ -1,7 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc23"
version = "0.1.0"

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@
tab_spaces = 2

View File

@ -1,102 +1,3 @@
use std::io;
fn get_calibration_digits() -> u32 {
let mut buffer = String::new();
let mut sum = 0;
while io::stdin().read_line(&mut buffer).unwrap() > 0 {
let nums: Vec<u32> = buffer.chars().filter_map(|c| c.to_digit(10)).collect();
sum += nums.first().unwrap() * 10 + nums.last().unwrap();
buffer = String::new();
}
sum
}
const NUMBERS: [&'static str; 9] = [
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine",
];
enum SearchDir {
First,
Last,
}
fn get_digit(line: &String, dir: SearchDir) -> u32 {
let mut first: usize = 0;
let mut index: Option<usize> = None;
for n in 1..=9 {
let f = match &dir {
SearchDir::First => line.find(&n.to_string()),
SearchDir::Last => line.rfind(&n.to_string()),
};
if f == None {
continue;
}
let found = f.unwrap();
match index {
None => {
index = f;
first = n;
}
Some(i) => match &dir {
SearchDir::First => {
if found < i {
index = f;
first = n;
}
}
SearchDir::Last => {
if found > i {
index = f;
first = n;
}
}
},
}
}
for (num, num_str) in NUMBERS.iter().enumerate() {
let f = match &dir {
SearchDir::First => line.find(num_str),
SearchDir::Last => line.rfind(num_str),
};
if f == None {
continue;
}
let found = f.unwrap();
match index {
None => {
index = f;
first = num + 1;
}
Some(i) => match &dir {
SearchDir::First => {
if found < i {
index = f;
first = num + 1;
}
}
SearchDir::Last => {
if found > i {
index = f;
first = num + 1;
}
}
},
}
}
first.try_into().unwrap()
}
fn get_calibration_digits_alnum() -> u32 {
let mut buffer = String::new();
let mut sum = 0;
while io::stdin().read_line(&mut buffer).unwrap() > 0 {
let val = (get_digit(&buffer, SearchDir::First) * 10) + get_digit(&buffer, SearchDir::Last);
sum += val;
buffer = String::new();
}
sum
}
fn main() {
println!("Sum: {}", get_calibration_digits_alnum());
println!("Hello, world!");
}