Skip to content

Commit

Permalink
day 1 in the books
Browse files Browse the repository at this point in the history
  • Loading branch information
BakerNet committed Dec 2, 2023
1 parent 86f4dc2 commit e8ea999
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
10 changes: 10 additions & 0 deletions data/examples/01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
two1nine
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
80 changes: 80 additions & 0 deletions src/bin/01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
advent_of_code::solution!(1);

pub fn part_one(input: &str) -> Option<u32> {
Some(
input
.lines()
.map(|line| {
let mut digits = line.char_indices().filter_map(|c| c.1.to_digit(10));
let first = digits.next().expect("Expected a digit");
let last = if let Some(last) = digits.last() {
last
} else {
first
};
first * 10 + last
})
.sum(),
)
}

pub fn part_two(input: &str) -> Option<u32> {
Some(
input
.lines()
.map(|line| {
let mut digits = line.char_indices().filter_map(|c| {
if let Some(digit) = c.1.to_digit(10) {
Some(digit)
} else {
if line[c.0..].starts_with("one") {
Some(1)
} else if line[c.0..].starts_with("two") {
Some(2)
} else if line[c.0..].starts_with("three") {
Some(3)
} else if line[c.0..].starts_with("four") {
Some(4)
} else if line[c.0..].starts_with("five") {
Some(5)
} else if line[c.0..].starts_with("six") {
Some(6)
} else if line[c.0..].starts_with("seven") {
Some(7)
} else if line[c.0..].starts_with("eight") {
Some(8)
} else if line[c.0..].starts_with("nine") {
Some(9)
} else {
None
}
}
});
let first = digits.next().expect("Expected a digit");
let last = if let Some(last) = digits.last() {
last
} else {
first
};
first * 10 + last
})
.sum(),
)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_part_one() {
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(142 + 209));
}

#[test]
fn test_part_two() {
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
assert_eq!(result, Some(142 + 198));
}
}

0 comments on commit e8ea999

Please sign in to comment.