day 1
This commit is contained in:
commit
5aba614a2b
7 changed files with 1074 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
7
Cargo.lock
generated
Normal file
7
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 = "aoc2023"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "aoc2023"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
11
examples/day1_1.rs
Normal file
11
examples/day1_1.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
fn main() {
|
||||
let mut total = 0;
|
||||
for line in std::io::stdin().lines() {
|
||||
let line = line.unwrap();
|
||||
if line.len() == 0 { continue }
|
||||
let c1 = line.find(|c| ('0'..='9').contains(&c)).unwrap();
|
||||
let c2 = line.rfind(|c| ('0'..='9').contains(&c)).unwrap();
|
||||
total += (line.chars().nth(c1).unwrap() as u32 - '0' as u32) * 10 + (line.chars().nth(c2).unwrap() as u32 - '0' as u32);
|
||||
}
|
||||
println!("{}", total);
|
||||
}
|
43
examples/day1_2.rs
Normal file
43
examples/day1_2.rs
Normal file
|
@ -0,0 +1,43 @@
|
|||
const WORDS: [&'static str; 10] = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"];
|
||||
|
||||
fn main() {
|
||||
let mut total = 0;
|
||||
for line in std::io::stdin().lines() {
|
||||
let line = line.unwrap();
|
||||
if line.len() == 0 { continue }
|
||||
let mut c1 = line.len() - 1;
|
||||
let mut c2 = 0;
|
||||
let mut n1 = 0;
|
||||
let mut n2 = 0;
|
||||
for (i, word) in WORDS.iter().enumerate() {
|
||||
if let Some(d1) = line.find(word) {
|
||||
if d1 <= c1 {
|
||||
c1 = d1;
|
||||
n1 = i;
|
||||
}
|
||||
}
|
||||
if let Some(d2) = line.rfind(word) {
|
||||
if d2 >= c2 {
|
||||
c2 = d2;
|
||||
n2 = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i, word) in ('0'..='9').enumerate() {
|
||||
if let Some(d1) = line.find(word) {
|
||||
if d1 <= c1 {
|
||||
c1 = d1;
|
||||
n1 = i;
|
||||
}
|
||||
}
|
||||
if let Some(d2) = line.rfind(word) {
|
||||
if d2 >= c2 {
|
||||
c2 = d2;
|
||||
n2 = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
total += n1*10 + n2;
|
||||
}
|
||||
println!("{}", total);
|
||||
}
|
1001
inputs/day1
Normal file
1001
inputs/day1
Normal file
File diff suppressed because it is too large
Load diff
3
src/main.rs
Normal file
3
src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
Loading…
Reference in a new issue