setup
This commit is contained in:
parent
83f51b4452
commit
3bff18f34f
7 changed files with 48 additions and 22 deletions
|
|
@ -1,5 +1,5 @@
|
|||
use shared::{Answer, Solution};
|
||||
use regex::{Regex};
|
||||
use regex::Regex;
|
||||
|
||||
pub struct Day01;
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ impl Solution for Day01 {
|
|||
}
|
||||
}
|
||||
|
||||
sum += 10 * first.expect(word) + last.expect(word)
|
||||
sum += 10 * first.unwrap_or(0) + last.unwrap_or(0)
|
||||
}
|
||||
|
||||
Answer::from(sum)
|
||||
|
|
@ -40,23 +40,19 @@ impl Solution for Day01 {
|
|||
).unwrap();
|
||||
|
||||
Answer::from(words.iter()
|
||||
.map(|word| {10 * matches(&first.captures(&word).unwrap()) + matches(&last.captures(word).unwrap())})
|
||||
.map(|word| {10 * matches(first.captures(word)) + matches(last.captures(word))})
|
||||
.collect::<Vec<_>>()
|
||||
.iter()
|
||||
.sum::<u64>())
|
||||
}
|
||||
}
|
||||
|
||||
fn matches(cap: ®ex::Captures) -> u64 {
|
||||
fn matches(cap: Option<regex::Captures>) -> u64 {
|
||||
if cap.is_none() { return 0} ;
|
||||
|
||||
for i in 1..10 {
|
||||
if cap.get(i).is_some() { return i as u64 };
|
||||
if cap.as_ref().unwrap().get(i).is_some() { return i as u64 };
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn example() {
|
||||
assert_eq!(Day01.part_1(load_test(2023, 1)?.as_str()), 142);
|
||||
assert_eq!(Day01.part_2(load_test(2023, 1)?.as_str()), 281);
|
||||
0
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue