init project, add day 1
This commit is contained in:
commit
9999f9397a
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
58
Cargo.lock
generated
Normal file
58
Cargo.lock
generated
Normal file
|
@ -0,0 +1,58 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "day_1-1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day_1-2"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-automata",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
"regex-syntax",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
|
7
Cargo.toml
Normal file
7
Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[workspace]
|
||||
|
||||
resolver = "2"
|
||||
members = [
|
||||
"day_1-1",
|
||||
"day_1-2",
|
||||
]
|
8
day_1-1/Cargo.toml
Normal file
8
day_1-1/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day_1-1"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1000
day_1-1/input.txt
Normal file
1000
day_1-1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
23
day_1-1/src/main.rs
Normal file
23
day_1-1/src/main.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
fn main() {
|
||||
let input = include_str!("../input.txt");
|
||||
let mut results: Vec<i32> = Vec::new();
|
||||
|
||||
for line in input
|
||||
.split("\n")
|
||||
.filter(|line| !line.is_empty()
|
||||
) {
|
||||
let mut numbers: Vec<char> = Vec::new();
|
||||
for char in line.chars() {
|
||||
if char.is_numeric() {
|
||||
numbers.push(char);
|
||||
}
|
||||
}
|
||||
|
||||
let num: i32 = format!("{}{}", numbers.first().unwrap(), numbers.last().unwrap())
|
||||
.parse()
|
||||
.unwrap();
|
||||
results.push(num);
|
||||
}
|
||||
|
||||
println!("Result: {}", results.iter().sum::<i32>());
|
||||
}
|
9
day_1-2/Cargo.toml
Normal file
9
day_1-2/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "day_1-2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
regex = "1.10.2"
|
1000
day_1-2/input.txt
Normal file
1000
day_1-2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
52
day_1-2/src/main.rs
Normal file
52
day_1-2/src/main.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use regex::Regex;
|
||||
|
||||
const WORDS: [&str; 19] = [
|
||||
"one",
|
||||
"two",
|
||||
"three",
|
||||
"four",
|
||||
"five",
|
||||
"six",
|
||||
"seven",
|
||||
"eight",
|
||||
"nine",
|
||||
"1", "2", "3", "4", "5",
|
||||
"6", "7", "8", "9", "0",
|
||||
];
|
||||
|
||||
fn main() {
|
||||
let regex = Regex::new(WORDS.join("|").as_str()).unwrap();
|
||||
let input = include_str!("../input.txt");
|
||||
let mut results: Vec<i32> = Vec::new();
|
||||
|
||||
for line in input
|
||||
.split("\n")
|
||||
.filter(|line| !line.is_empty()
|
||||
) {
|
||||
let mut numbers: Vec<char> = Vec::new();
|
||||
let mut index: i32 = 0;
|
||||
|
||||
while let Some(m) = regex.find_at(&line, index as usize) {
|
||||
index = m.start() as i32 + 1;
|
||||
numbers.push(match m.as_str() {
|
||||
"one" => '1',
|
||||
"two" => '2',
|
||||
"three" => '3',
|
||||
"four" => '4',
|
||||
"five" => '5',
|
||||
"six" => '6',
|
||||
"seven" => '7',
|
||||
"eight" => '8',
|
||||
"nine" => '9',
|
||||
x => x.chars().next().unwrap()
|
||||
});
|
||||
}
|
||||
|
||||
let num: i32 = format!("{}{}", numbers.first().unwrap(), numbers.last().unwrap())
|
||||
.parse()
|
||||
.unwrap();
|
||||
results.push(num);
|
||||
}
|
||||
|
||||
println!("Result: {}", results.iter().sum::<i32>());
|
||||
}
|
Loading…
Reference in a new issue