upload day 1
This commit is contained in:
7
day1/Cargo.lock
generated
Normal file
7
day1/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
day1/Cargo.toml
Normal file
8
day1/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
day1/input.txt
Normal file
1000
day1/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
48
day1/src/main.rs
Normal file
48
day1/src/main.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let input = fs::read_to_string("input.txt").unwrap();
|
||||
let input = input.split('\n');
|
||||
let mut sum = 0;
|
||||
for line in input {
|
||||
let str_nums: Vec<(&str, &str)> = vec![("one", "1"), ("two", "2"), ("three", "3"), ("four", "4"), ("five", "5"), ("six", "6"), ("seven", "7"), ("eight", "8"), ("nine", "9")];
|
||||
let mut matches: Vec<(usize, &str)> = vec![];
|
||||
for str_num in str_nums {
|
||||
// Get every alphabetic number in the string with it's index
|
||||
let str_match: Vec<_> = line.match_indices(str_num.0).collect();
|
||||
// convert the string to a numeral
|
||||
let mut str_match: Vec<_> = str_match.iter().map(|x| return (x.0, str_num.1)).collect();
|
||||
matches.append(&mut str_match);
|
||||
}
|
||||
// get the numerials from the line with their index
|
||||
let mut num_matches: Vec<(usize, &str)> = line.match_indices(|x: char| x.is_numeric()).collect();
|
||||
matches.append(&mut num_matches);
|
||||
// sort by index
|
||||
matches.sort_by(|lhs, rhs| lhs.cmp(rhs));
|
||||
let num = (matches.first().unwrap().1).to_owned() + (matches.last().unwrap().1);
|
||||
sum += num.parse::<i32>().unwrap();
|
||||
}
|
||||
|
||||
println!("{}", sum);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// use std::fs;
|
||||
|
||||
// fn main() {
|
||||
// let input = fs::read_to_string("input.txt").unwrap();
|
||||
// let input = input.split('\n');
|
||||
// let mut sum = 0;
|
||||
// for line in input {
|
||||
// let chars: Vec<char>= line.chars().filter(|x| x.is_numeric()).collect();
|
||||
// let mut num = chars.first().unwrap().to_string();
|
||||
// num += &chars.last().unwrap().to_string();
|
||||
// let num: u32 = num.parse().unwrap();
|
||||
// sum += num;
|
||||
// }
|
||||
|
||||
// println!("{}", sum);
|
||||
// }
|
Reference in New Issue
Block a user