Compare commits
2 Commits
de452d97c0
...
c5e0ac820a
Author | SHA1 | Date |
---|---|---|
|
c5e0ac820a | |
|
6b55dd0f52 |
|
@ -0,0 +1,7 @@
|
||||||
|
# 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
|
@ -0,0 +1 @@
|
||||||
|
tab_spaces = 2
|
103
src/main.rs
103
src/main.rs
|
@ -1,3 +1,102 @@
|
||||||
fn main() {
|
use std::io;
|
||||||
println!("Hello, world!");
|
|
||||||
|
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());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue