Skip to content

Instantly share code, notes, and snippets.

@tylercritchlow
Created December 1, 2023 18:54
Show Gist options
  • Save tylercritchlow/d2804bb3d918527fd518ca435f2175dc to your computer and use it in GitHub Desktop.
Save tylercritchlow/d2804bb3d918527fd518ca435f2175dc to your computer and use it in GitHub Desktop.
advent of code 2023 day 1
use std::fs::read_to_string;
use std::collections::HashMap;
// Handle case for "one", "two", etc.
//
fn main() {
let numberConversions = HashMap::from([
// key, value
("one", 1),
("two", 2),
("three", 3),
("four", 4),
("five", 5),
("six", 6),
("seven", 7),
("eight", 8),
("nine", 9),
]);
let mut lines = read_to_string("input.txt")
.unwrap()
.lines()
.map(String::from)
.collect();
let current_digits = Vec::new();
let mut sum = 0;
for line in lines {
let ptr1 = 0;
let ptr2 = line.len() - 1; // minus 1 due to indexing [0, 1, 2, 3] so len would return 4 (but we need index 3)
let running_ptr1 = true;
let running_ptr2 = true;
while ptr1 < ptr2 && (running_ptr1 || running_ptr2) {
// if this fails we know we only have 1 digit (eg. 77)
if (line[ptr1] as char).is_numeric() {
// sum += line[ptr1] as i32;
if running_ptr1 {
current_digits.push(line[ptr1] as i32);
running_ptr1 = false;
}
} else {
ptr1 += 1
}
if (line[ptr2] as char).is_numeric() {
// sum += line[ptr2] as i32;
if running_ptr2 {
current_digits.push(line[ptr2] as i32);
running_ptr2 = false;
}
} else {
ptr2 -= 1
}
}
if current_digits.len() <= 2 {
println!("{}", current_digits)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment