completed day1
This commit is contained in:
parent
dab145d2d4
commit
9fe0199321
7 changed files with 2133 additions and 3 deletions
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -3,5 +3,9 @@
|
|||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "helloworld"
|
||||
name = "day1"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
[workspace]
|
||||
members = ["day1"]
|
||||
members = ["day1", "day2"]
|
||||
resolver = "2"
|
||||
|
|
1001
day1/input.txt
Normal file
1001
day1/input.txt
Normal file
File diff suppressed because it is too large
Load diff
|
@ -1,3 +1,38 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
|
||||
let file_contents = fs::read_to_string("./input.txt").unwrap();
|
||||
|
||||
let sum: i64 = file_contents
|
||||
.lines()
|
||||
.map(|x| sum_first_last(x))
|
||||
.sum::<i64>();
|
||||
println!("Answer is: {}", sum);
|
||||
|
||||
}
|
||||
|
||||
fn sum_first_last(input: &str) -> i64 {
|
||||
let mut first: i64 = -64;
|
||||
let mut last: i64 = -64;
|
||||
let _: Vec<_> = input
|
||||
.chars()
|
||||
.map(|x| {
|
||||
if char::is_digit(x, 10) {
|
||||
if first == -64 {
|
||||
first = char::to_digit(x, 10).unwrap() as i64;
|
||||
} else {
|
||||
last = char::to_digit(x, 10).unwrap() as i64;
|
||||
}
|
||||
}
|
||||
}).collect();
|
||||
|
||||
if first == -64 {
|
||||
first = 0;
|
||||
}
|
||||
if last == -64 {
|
||||
last = first.clone();
|
||||
}
|
||||
|
||||
return first * 10 + last;
|
||||
}
|
||||
|
|
8
day2/Cargo.toml
Normal file
8
day2/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
1001
day2/input.txt
Normal file
1001
day2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
81
day2/src/main.rs
Normal file
81
day2/src/main.rs
Normal file
|
@ -0,0 +1,81 @@
|
|||
use std::fs;
|
||||
|
||||
|
||||
fn main() {
|
||||
|
||||
let file_contents = fs::read_to_string("./input.txt").unwrap();
|
||||
|
||||
let sum: i64 = file_contents
|
||||
.lines()
|
||||
.map(|x| sum_first_last(x))
|
||||
.sum::<i64>();
|
||||
println!("Answer is: {}", sum);
|
||||
|
||||
}
|
||||
|
||||
fn sum_first_last(input: &str) -> i64 {
|
||||
let mut first: i64 = -64;
|
||||
let mut last: i64 = -64;
|
||||
|
||||
|
||||
let _: Vec<_> = input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
// if !line.contains(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]){
|
||||
for n in 0..line.len() {
|
||||
if char::is_digit(line.chars().nth(n).unwrap(), 10){
|
||||
if first == -64 {
|
||||
first = char::to_digit(line.chars().nth(n).unwrap(), 10).unwrap() as i64;
|
||||
} else {
|
||||
last = char::to_digit(line.chars().nth(n).unwrap(), 10).unwrap() as i64;
|
||||
}
|
||||
} else {
|
||||
let val = get_str_num(line.get(n..).unwrap());
|
||||
match val {
|
||||
Some(val) => {
|
||||
if first == -64 {
|
||||
first = val;
|
||||
} else {
|
||||
last = val;
|
||||
}
|
||||
},
|
||||
None => {}
|
||||
};
|
||||
}
|
||||
}
|
||||
}).collect();
|
||||
|
||||
if first == -64 {
|
||||
first = 0;
|
||||
}
|
||||
if last == -64 {
|
||||
last = first.clone();
|
||||
}
|
||||
|
||||
return first * 10 + last;
|
||||
}
|
||||
|
||||
fn get_str_num(x: &str) -> Option<i64> {
|
||||
// check timing of branch pre-check version
|
||||
if x.starts_with("one") {
|
||||
Some(1)
|
||||
} else if x.starts_with("two") {
|
||||
Some(2)
|
||||
} else if x.starts_with("three") {
|
||||
Some(3)
|
||||
} else if x.starts_with("four") {
|
||||
Some(4)
|
||||
} else if x.starts_with("five") {
|
||||
Some(5)
|
||||
} else if x.starts_with("six") {
|
||||
Some(6)
|
||||
} else if x.starts_with("seven") {
|
||||
Some(7)
|
||||
} else if x.starts_with("eight") {
|
||||
Some(8)
|
||||
} else if x.starts_with("nine") {
|
||||
Some(9)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue