Skip to content

Instantly share code, notes, and snippets.

@skjalgsm
Created January 5, 2024 12:42
Show Gist options
  • Save skjalgsm/72d328dc6790096dcfb3e635aca63080 to your computer and use it in GitHub Desktop.
Save skjalgsm/72d328dc6790096dcfb3e635aca63080 to your computer and use it in GitHub Desktop.
Advent of code day 1 part 2 rust
use tracing;
use std::fs;
use miette::Result;
use crate::aoc_error;
pub fn main() {
let contents = fs::read_to_string("2023-day-01-part2")
.expect("Should have been able to read the file");
let result = calculate(contents); //55172
match result {
Ok(v) => println!("Result is: {v:?}"),
Err(e) => println!("Error!: {e:?}")
}
}
#[tracing::instrument]
fn calculate(input : String) -> Result<u32, aoc_error::AoCError> {
let mut sum = 0;
let lines = input.lines();
for line in lines {
let mut first = 10;
let mut last = 10;
let length = line.len();
let chars: Vec<char> = line.chars().collect();
for first_index in 0..chars.len() {
if first == 10 {
let first_char = chars[first_index];
let first_digit = first_char.to_digit(10);
match first_digit {
None => {
if first_char == 'o' && chars[first_index + 1] == 'n' && chars[first_index + 2] == 'e' {
first = 1;
}
else if first_char == 't' && chars[first_index + 1] == 'w' && chars[first_index + 2] == 'o' {
first = 2;
}
else if first_char == 't' && chars[first_index + 1] == 'h' && chars[first_index + 2] == 'r' && chars[first_index + 3] == 'e' && chars[first_index + 4] == 'e' {
first = 3;
}
else if first_char == 'f' && chars[first_index + 1] == 'o' && chars[first_index + 2] == 'u' && chars[first_index + 3] == 'r' {
first = 4;
}
else if first_char == 'f' && chars[first_index + 1] == 'i' && chars[first_index + 2] == 'v' && chars[first_index + 3] == 'e' {
first = 5;
}
else if first_char == 's' && chars[first_index + 1] == 'i' && chars[first_index + 2] == 'x' {
first = 6;
}
else if first_char == 's' && chars[first_index + 1] == 'e' && chars[first_index + 2] == 'v' && chars[first_index + 3] == 'e' && chars[first_index + 4] == 'n' {
first = 7;
}
else if first_char == 'e' && chars[first_index + 1] == 'i' && chars[first_index + 2] == 'g' && chars[first_index + 3] == 'h' && chars[first_index + 4] == 't' {
first = 8;
}
else if first_char == 'n' && chars[first_index + 1] == 'i' && chars[first_index + 2] == 'n' && chars[first_index + 3] == 'e' {
first = 9;
}
}
Some(value) => {
first = value;
}
}
}
if last == 10 {
let last_index = (length - 1) - first_index;
let last_char = chars[last_index];
let last_digit = last_char.to_digit(10);
match last_digit {
None => {
if last_char == 'e' && chars[last_index - 1] == 'n' && chars[last_index - 2] == 'o' {
last = 1;
}
else if last_char == 'o' && chars[last_index - 1] == 'w' && chars[last_index - 2] == 't' {
last = 2;
}
else if last_char == 'e' && chars[last_index - 1] == 'e' && chars[last_index - 2] == 'r' && chars[last_index - 3] == 'h' && chars[last_index - 4] == 't' {
last = 3;
}
else if last_char == 'r' && chars[last_index - 1] == 'u' && chars[last_index - 2] == 'o' && chars[last_index - 3] == 'f' {
last = 4;
}
else if last_char == 'e' && chars[last_index - 1] == 'v' && chars[last_index - 2] == 'i' && chars[last_index - 3] == 'f' {
last = 5;
}
else if last_char == 'x' && chars[last_index - 1] == 'i' && chars[last_index - 2] == 's' {
last = 6;
}
else if last_char == 'n' && chars[last_index - 1] == 'e' && chars[last_index - 2] == 'v' && chars[last_index - 3] == 'e' && chars[last_index - 4] == 's' {
last = 7;
}
else if last_char == 't' && chars[last_index - 1] == 'h' && chars[last_index - 2] == 'g' && chars[last_index - 3] == 'i' && chars[last_index - 4] == 'e' {
last = 8;
}
else if last_char == 'e' && chars[last_index - 1] == 'n' && chars[last_index - 2] == 'i' && chars[last_index - 3] == 'n' {
last = 9;
}
}
Some(value) => {
last = value;
}
}
}
}
let number = first * 10 + last;
sum += number;
}
return Result::<u32, aoc_error::AoCError>::Ok(sum);
}
#[test]
fn it_works() -> Result<()> {
let input = "two1nine //29
eightwothree //83
abcone2threexyz //13
xtwone3four //24
4nineeightseven2 //42
zoneight234 //14
7pqrstsixteen"; //76
assert_eq!(281, calculate(input.to_string())?);
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment