I'm tired don't judge my setup ill fix it later or something
This commit is contained in:
commit
ee572e5e06
5 changed files with 100 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
/**/input.txt
|
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 = "advent"
|
||||
version = "0.1.0"
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "advent"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
70
src/day1/mod.rs
Normal file
70
src/day1/mod.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
pub fn part1() {
|
||||
let input = include_str!("input.txt");
|
||||
|
||||
let mut values = Vec::<usize>::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let digits = line.chars().filter(|char| char.is_numeric()).collect::<Vec<_>>();
|
||||
let calibrartion_value = format!("{}{}", digits.first().unwrap(), digits.last().unwrap());
|
||||
values.push(calibrartion_value.parse().unwrap());
|
||||
}
|
||||
|
||||
println!("{}", values.into_iter().reduce(|acc, e| acc + e).unwrap());
|
||||
}
|
||||
|
||||
pub fn part2() {
|
||||
let input = include_str!("input.txt");
|
||||
|
||||
let mut values = Vec::<usize>::new();
|
||||
|
||||
for line in input.lines() {
|
||||
let line = line.to_owned();
|
||||
|
||||
// Find instances of number words and choose the closest, recursively replacing (Can't just .replace b/c of shit like "twone")
|
||||
let digits = vec![
|
||||
line.match_indices("one").collect::<Vec<_>>(),
|
||||
line.match_indices("two").collect::<Vec<_>>(),
|
||||
line.match_indices("three").collect::<Vec<_>>(),
|
||||
line.match_indices("four").collect::<Vec<_>>(),
|
||||
line.match_indices("five").collect::<Vec<_>>(),
|
||||
line.match_indices("six").collect::<Vec<_>>(),
|
||||
line.match_indices("seven").collect::<Vec<_>>(),
|
||||
line.match_indices("eight").collect::<Vec<_>>(),
|
||||
line.match_indices("nine").collect::<Vec<_>>(),
|
||||
line.match_indices("1").collect::<Vec<_>>(),
|
||||
line.match_indices("2").collect::<Vec<_>>(),
|
||||
line.match_indices("3").collect::<Vec<_>>(),
|
||||
line.match_indices("4").collect::<Vec<_>>(),
|
||||
line.match_indices("5").collect::<Vec<_>>(),
|
||||
line.match_indices("6").collect::<Vec<_>>(),
|
||||
line.match_indices("7").collect::<Vec<_>>(),
|
||||
line.match_indices("8").collect::<Vec<_>>(),
|
||||
line.match_indices("9").collect::<Vec<_>>(),
|
||||
].into_iter();
|
||||
|
||||
let mut flat = Vec::<(usize, String)>::new();
|
||||
for digit in digits {
|
||||
for (index, text) in digit {
|
||||
let text = text
|
||||
.to_owned()
|
||||
.replace("one", "1")
|
||||
.replace("two", "2")
|
||||
.replace("three", "3")
|
||||
.replace("four", "4")
|
||||
.replace("five", "5")
|
||||
.replace("six", "6")
|
||||
.replace("seven", "7")
|
||||
.replace("eight", "8")
|
||||
.replace("nine", "9");
|
||||
|
||||
flat.push((index, text));
|
||||
}
|
||||
}
|
||||
flat.sort_by(|a, b| a.0.cmp(&b.0));
|
||||
|
||||
let calibration_value = format!("{}{}", flat.first().unwrap().1, flat.last().unwrap().1);
|
||||
values.push(calibration_value.parse().unwrap());
|
||||
}
|
||||
|
||||
println!("{}", values.into_iter().reduce(|acc, e| acc + e).unwrap());
|
||||
}
|
13
src/main.rs
Normal file
13
src/main.rs
Normal file
|
@ -0,0 +1,13 @@
|
|||
pub mod day1;
|
||||
|
||||
fn main() {
|
||||
let args = std::env::args().collect::<Vec<_>>();
|
||||
|
||||
let day = args[1].as_str();
|
||||
let part = args[2].as_str();
|
||||
|
||||
match day {
|
||||
"1" => match part { "1" => day1::part1(), "2" => day1::part2(), _ => panic!("Invalid part #") },
|
||||
_ => panic!("Invalid day #")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue