mirror of
https://github.com/kristoferssolo/Advent-of-Code.git
synced 2025-10-21 18:00:35 +00:00
Merge branch 'day-01'
This commit is contained in:
commit
97b9b6f430
9
day-01/Cargo.toml
Normal file
9
day-01/Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day-01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
color-eyre = { workspace = true }
|
||||
1000
day-01/input1.txt
Normal file
1000
day-01/input1.txt
Normal file
File diff suppressed because it is too large
Load Diff
1000
day-01/input2.txt
Normal file
1000
day-01/input2.txt
Normal file
File diff suppressed because it is too large
Load Diff
9
day-01/src/bin/part1.rs
Normal file
9
day-01/src/bin/part1.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use color_eyre::Result;
|
||||
use day_01::part1::process;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let file = include_str!("../../input1.txt");
|
||||
let result = process(file)?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
9
day-01/src/bin/part2.rs
Normal file
9
day-01/src/bin/part2.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use color_eyre::Result;
|
||||
use day_01::part2::process;
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let file = include_str!("../../input2.txt");
|
||||
let result = process(file)?;
|
||||
println!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
2
day-01/src/lib.rs
Normal file
2
day-01/src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod part1;
|
||||
pub mod part2;
|
||||
30
day-01/src/part1.rs
Normal file
30
day-01/src/part1.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use color_eyre::Result;
|
||||
|
||||
pub fn process(input: &str) -> Result<u32> {
|
||||
let sum: u32 = input
|
||||
.lines()
|
||||
.flat_map(|line| {
|
||||
let digits: Vec<u32> = line.chars().filter_map(|char| char.to_digit(10)).collect();
|
||||
match (digits.first(), digits.last()) {
|
||||
(Some(first), Some(last)) => Some(first * 10 + last),
|
||||
_ => None,
|
||||
}
|
||||
})
|
||||
.sum();
|
||||
|
||||
Ok(sum)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_process() -> Result<()> {
|
||||
let input = "1abc2
|
||||
pqr3stu8vwx
|
||||
a1b2c3d4e5f
|
||||
treb7uchet";
|
||||
assert_eq!(142, process(input)?);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
59
day-01/src/part2.rs
Normal file
59
day-01/src/part2.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use color_eyre::Result;
|
||||
|
||||
pub fn process(input: &str) -> Result<u32> {
|
||||
let sum: u32 = input.lines().map(process_line).sum();
|
||||
|
||||
Ok(sum)
|
||||
}
|
||||
|
||||
fn process_line(line: &str) -> u32 {
|
||||
let mut it = (0..line.len()).filter_map(|index| {
|
||||
let reduced_line = &line[index..];
|
||||
let result = if reduced_line.starts_with("one") {
|
||||
Some(1)
|
||||
} else if reduced_line.starts_with("two") {
|
||||
Some(2)
|
||||
} else if reduced_line.starts_with("three") {
|
||||
Some(3)
|
||||
} else if reduced_line.starts_with("four") {
|
||||
Some(4)
|
||||
} else if reduced_line.starts_with("five") {
|
||||
Some(5)
|
||||
} else if reduced_line.starts_with("six") {
|
||||
Some(6)
|
||||
} else if reduced_line.starts_with("seven") {
|
||||
Some(7)
|
||||
} else if reduced_line.starts_with("eight") {
|
||||
Some(8)
|
||||
} else if reduced_line.starts_with("nine") {
|
||||
Some(9)
|
||||
} else {
|
||||
reduced_line.chars().next().unwrap().to_digit(10)
|
||||
};
|
||||
result
|
||||
});
|
||||
|
||||
let first = it.next().expect("Should be a number");
|
||||
|
||||
match it.last() {
|
||||
Some(num) => first * 10 + num,
|
||||
None => first * 10 + first,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
#[test]
|
||||
fn test_process() -> Result<()> {
|
||||
let input = "two1nine
|
||||
eightwothree
|
||||
abcone2threexyz
|
||||
xtwone3four
|
||||
4nineeightseven2
|
||||
zoneight234
|
||||
7pqrstsixteen";
|
||||
assert_eq!(281, process(input)?);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user