Restructure folders
This commit is contained in:
1
2023/day1/part1/.gitignore
vendored
Normal file
1
2023/day1/part1/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/target
|
||||
7
2023/day1/part1/Cargo.lock
generated
Normal file
7
2023/day1/part1/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
8
2023/day1/part1/Cargo.toml
Normal file
8
2023/day1/part1/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
1000
2023/day1/part1/input
Normal file
1000
2023/day1/part1/input
Normal file
File diff suppressed because it is too large
Load Diff
44
2023/day1/part1/src/main.rs
Normal file
44
2023/day1/part1/src/main.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
fn main() {
|
||||
let input = std::fs::read_to_string("./input").unwrap();
|
||||
|
||||
struct FirstLast {
|
||||
first: Option<u32>,
|
||||
last: Option<u32>,
|
||||
}
|
||||
|
||||
impl FirstLast {
|
||||
fn new() -> Self {
|
||||
Self {
|
||||
first: None,
|
||||
last: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn record(&mut self, n: u32) {
|
||||
if self.first.is_none() {
|
||||
self.first = Some(n)
|
||||
} else {
|
||||
self.last = Some(n)
|
||||
}
|
||||
}
|
||||
|
||||
fn value(self) -> u32 {
|
||||
self.first.unwrap() * 10 + self.last.unwrap_or(self.first.unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
let out: u32 = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
line.chars().fold(FirstLast::new(), |mut val, char| {
|
||||
if let Some(digit) = char.to_digit(10) {
|
||||
val.record(digit)
|
||||
}
|
||||
val
|
||||
})
|
||||
})
|
||||
.map(FirstLast::value)
|
||||
.sum();
|
||||
|
||||
println!("{:?}", out);
|
||||
}
|
||||
Reference in New Issue
Block a user