Skip to content

Commit

Permalink
Day 1 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
connorslade committed Dec 1, 2023
1 parent 262da23 commit 302ed63
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions aoc_2023/src/day_01.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ impl Solution for Day01 {
let mut digits = line.chars().filter_map(|c| c.to_digit(10));
let first = digits.next().unwrap();
let last = digits.last().unwrap();

sum += format!("{}{}", first, last).parse::<u32>().unwrap();
sum += first * 10 + last;
}

sum.into()
Expand All @@ -31,33 +30,42 @@ impl Solution for Day01 {
let first = digits[0];
let last = digits[digits.len() - 1];

sum += format!("{}{}", first, last).parse::<u32>().unwrap();
sum += first * 10 + last;
}

sum.into()
}
}

fn digits(i: &str) -> Vec<u8> {
fn digits(i: &str) -> [u32; 2] {
let mut first = None;
let mut last = 0;

let mut digit = |c| {
last = c;
if first.is_none() {
first = Some(c);
}
};

let chars = i.as_bytes();
let mut out = Vec::new();
let mut i = 0;

while i < chars.len() {
let c = chars[i];
if c.is_ascii_digit() {
out.push(c - b'0');
digit((c - b'0') as u32);
} else {
for (j, d) in DIGITS.iter().enumerate() {
if chars[i..].starts_with(d.as_bytes()) {
out.push(j as u8 + 1);
digit(j as u32 + 1);
}
}
}
i += 1;
}

out
[first.unwrap(), last]
}

#[cfg(test)]
Expand Down

0 comments on commit 302ed63

Please sign in to comment.