Rust Day 1 Part 1
This commit is contained in:
parent
8384c6406d
commit
0dffb33f3b
7 changed files with 1074 additions and 2 deletions
2
rust/Cargo.lock
generated
2
rust/Cargo.lock
generated
|
@ -4,4 +4,4 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "advent-of-code"
|
||||
version = "23.0.0"
|
||||
version = "23.1.1"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "advent-of-code"
|
||||
version = "23.0.0"
|
||||
version = "23.1.1"
|
||||
|
||||
[profile.release]
|
||||
strip = "symbols"
|
||||
|
@ -22,3 +22,7 @@ name = "template"
|
|||
# day00 for alphabetic sorting
|
||||
path = "src/day00/main.rs"
|
||||
test = false
|
||||
|
||||
[[bin]]
|
||||
name = "day01"
|
||||
path = "src/day01/main.rs"
|
||||
|
|
1000
rust/src/day01/input.txt
Normal file
1000
rust/src/day01/input.txt
Normal file
File diff suppressed because it is too large
Load diff
11
rust/src/day01/main.rs
Normal file
11
rust/src/day01/main.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
const INPUT: &str = include_str!("input.txt");
|
||||
|
||||
mod part_1;
|
||||
use part_1::part_1;
|
||||
mod part_2;
|
||||
use part_2::part_2;
|
||||
|
||||
fn main() {
|
||||
println!("{}", part_1(INPUT));
|
||||
println!("{}", part_2(INPUT));
|
||||
}
|
29
rust/src/day01/part_1.rs
Normal file
29
rust/src/day01/part_1.rs
Normal file
|
@ -0,0 +1,29 @@
|
|||
pub(crate) fn part_1(input: &'static str) -> u64 {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
let mut digits = line.chars().filter(char::is_ascii_digit);
|
||||
let first_digit = digits.next().expect("At least 1 digit should be present");
|
||||
let last_digit = digits.last().unwrap_or(first_digit);
|
||||
|
||||
format!("{first_digit}{last_digit}")
|
||||
.parse::<u64>()
|
||||
.expect("Two digits should make a number")
|
||||
})
|
||||
.sum()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||
|
||||
#[test]
|
||||
fn test_with_sample_solution() {
|
||||
assert_eq!(super::part_1(SAMPLE_INPUT), 142)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(super::part_1(crate::INPUT), 55488);
|
||||
}
|
||||
}
|
24
rust/src/day01/part_2.rs
Normal file
24
rust/src/day01/part_2.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
pub(crate) fn part_2(_input: &'static str) -> u64 {
|
||||
todo!("Part 2")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
const SAMPLE_INPUT: &str = include_str!("sample_input.txt");
|
||||
|
||||
#[test]
|
||||
fn test_with_sample_solution() {
|
||||
assert_eq!(
|
||||
super::part_2(SAMPLE_INPUT),
|
||||
todo!("Add result from example part 2")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_with_solution() {
|
||||
assert_eq!(
|
||||
super::part_2(crate::INPUT),
|
||||
todo!("Add result for solved part 2")
|
||||
);
|
||||
}
|
||||
}
|
4
rust/src/day01/sample_input.txt
Normal file
4
rust/src/day01/sample_input.txt
Normal file
|
@ -0,0 +1,4 @@
|
|||
1bc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet
|
Loading…
Reference in a new issue