Compare commits

...

2 Commits

Author SHA1 Message Date
Drew Galbraith c5e0ac820a Day 1 part 2. 2023-12-01 07:12:35 -08:00
Drew Galbraith 6b55dd0f52 Day 1 part 1 complete. 2023-12-01 06:24:49 -08:00
4 changed files with 1109 additions and 2 deletions

7
Cargo.lock generated Normal file
View File

@ -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"

1000
input/day01.txt Normal file

File diff suppressed because it is too large Load Diff

1
rustfmt.toml Normal file
View File

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

View File

@ -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());
} }