Add day one

This commit is contained in:
2023-12-01 18:51:27 +01:00
commit a3c6a7f80e
10 changed files with 2157 additions and 0 deletions

1
2023/1/1/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
2023/1/1/Cargo.lock generated Normal file
View 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/1/1/Cargo.toml Normal file
View 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/1/1/input Normal file

File diff suppressed because it is too large Load Diff

44
2023/1/1/src/main.rs Normal file
View 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);
}

1
2023/1/2/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/target

7
2023/1/2/Cargo.lock generated Normal file
View 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/1/2/Cargo.toml Normal file
View 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/1/2/input Normal file

File diff suppressed because it is too large Load Diff

81
2023/1/2/src/main.rs Normal file
View File

@@ -0,0 +1,81 @@
fn main() {
let input = std::fs::read_to_string("./input").unwrap();
#[derive(Debug)]
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())
}
}
struct SpelledOutNumber(u32);
impl SpelledOutNumber {
fn parse(value: &str) -> Option<Self> {
if value.starts_with("one") {
Some(Self(1))
} else if value.starts_with("two") {
Some(Self(2))
} else if value.starts_with("three") {
Some(Self(3))
} else if value.starts_with("four") {
Some(Self(4))
} else if value.starts_with("five") {
Some(Self(5))
} else if value.starts_with("six") {
Some(Self(6))
} else if value.starts_with("seven") {
Some(Self(7))
} else if value.starts_with("eight") {
Some(Self(8))
} else if value.starts_with("nine") {
Some(Self(9))
} else {
None
}
}
}
let out: u32 = input
.lines()
.map(|line| {
let line: Vec<char> = line.chars().collect();
let mut recorder = FirstLast::new();
for i in 0..line.len() {
let c = &line[i];
if let Some(digit) = c.to_digit(10) {
recorder.record(digit)
} else {
let rest = &line[i..].iter().collect::<String>();
if let Some(digit) = SpelledOutNumber::parse(rest.as_str()) {
recorder.record(digit.0)
}
}
}
recorder
})
.map(FirstLast::value)
.sum();
println!("{out}");
}