day_01
This commit is contained in:
8
day_01/Cargo.toml
Normal file
8
day_01/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "day_01"
|
||||
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_01/input
Normal file
1000
day_01/input
Normal file
File diff suppressed because it is too large
Load Diff
105
day_01/src/main.rs
Normal file
105
day_01/src/main.rs
Normal file
@@ -0,0 +1,105 @@
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
// File input must exist in the current path
|
||||
if let Ok(lines) = read_lines("./input") {
|
||||
let mut total_part_1: u64 = 0;
|
||||
let mut total_part_2: u64 = 0;
|
||||
|
||||
// Consumes the iterator, returns an (Optional) String
|
||||
for line in lines {
|
||||
if let Ok(ip) = line {
|
||||
// Part 1
|
||||
let v: Vec<_> = ip.matches(char::is_numeric).collect();
|
||||
println!("{}{}", v[0], v[v.len() - 1]);
|
||||
let number_part_1: u64 = format!("{}{}", v[0], v[v.len() - 1])
|
||||
.parse::<u64>()
|
||||
.unwrap();
|
||||
total_part_1 = total_part_1 + number_part_1;
|
||||
|
||||
// Part 2
|
||||
// Match all numbers
|
||||
let numeric: Vec<_> = ip.match_indices(char::is_numeric).collect();
|
||||
let zero_str: Vec<_> = ip.match_indices("zero").collect();
|
||||
let one_str: Vec<_> = ip.match_indices("one").collect();
|
||||
let two_str: Vec<_> = ip.match_indices("two").collect();
|
||||
let three_str: Vec<_> = ip.match_indices("three").collect();
|
||||
let four_str: Vec<_> = ip.match_indices("four").collect();
|
||||
let five_str: Vec<_> = ip.match_indices("five").collect();
|
||||
let six_str: Vec<_> = ip.match_indices("six").collect();
|
||||
let seven_str: Vec<_> = ip.match_indices("seven").collect();
|
||||
let eight_str: Vec<_> = ip.match_indices("eight").collect();
|
||||
let nine_str: Vec<_> = ip.match_indices("nine").collect();
|
||||
|
||||
// Create one vector per line
|
||||
let mut v: Vec<_> = vec![];
|
||||
v.extend_from_slice(&numeric);
|
||||
v.extend_from_slice(&zero_str);
|
||||
v.extend_from_slice(&one_str);
|
||||
v.extend_from_slice(&two_str);
|
||||
v.extend_from_slice(&three_str);
|
||||
v.extend_from_slice(&four_str);
|
||||
v.extend_from_slice(&five_str);
|
||||
v.extend_from_slice(&six_str);
|
||||
v.extend_from_slice(&seven_str);
|
||||
v.extend_from_slice(&eight_str);
|
||||
v.extend_from_slice(&nine_str);
|
||||
|
||||
// Sort vector
|
||||
v.sort_by_key(|k| k.0);
|
||||
|
||||
// Convert written numbers
|
||||
let mut v_first = "";
|
||||
match v.first().unwrap().1 {
|
||||
"zero" => v_first = "0",
|
||||
"one" => v_first = "1",
|
||||
"two" => v_first = "2",
|
||||
"three" => v_first = "3",
|
||||
"four" => v_first = "4",
|
||||
"five" => v_first = "5",
|
||||
"six" => v_first = "6",
|
||||
"seven" => v_first = "7",
|
||||
"eight" => v_first = "8",
|
||||
"nine" => v_first = "9",
|
||||
_ => v_first = v.first().unwrap().1,
|
||||
}
|
||||
|
||||
let mut v_last = "";
|
||||
match v.last().unwrap().1 {
|
||||
"zero" => v_last = "0",
|
||||
"one" => v_last = "1",
|
||||
"two" => v_last = "2",
|
||||
"three" => v_last = "3",
|
||||
"four" => v_last = "4",
|
||||
"five" => v_last = "5",
|
||||
"six" => v_last = "6",
|
||||
"seven" => v_last = "7",
|
||||
"eight" => v_last = "8",
|
||||
"nine" => v_last = "9",
|
||||
_ => v_last = v.last().unwrap().1,
|
||||
}
|
||||
|
||||
// Create 2-digit number of first and last number per line
|
||||
let number_part_2: u64 = format!("{}{}", v_first, v_last).parse::<u64>().unwrap();
|
||||
|
||||
// Add up all numbers
|
||||
total_part_2 = total_part_2 + number_part_2;
|
||||
}
|
||||
}
|
||||
|
||||
println!("Total of part 1 is {}", total_part_1);
|
||||
println!("Total of part 2 is {}", total_part_2);
|
||||
}
|
||||
}
|
||||
|
||||
// The output is wrapped in a Result to allow matching on errors
|
||||
// Returns an Iterator to the Reader of the lines of the file.
|
||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
Reference in New Issue
Block a user