Hello again.
So remember how I said that I "[...] hope that I can actually stick to consistently writing now, for my own sake"?

It's been much busier than I anticipated and I just did not have the time to write anything, but enough excuses.
It's Advent of Code time.
Every year, AoC happens, and every year, I'll do the first few and neglect the rest, either because I run out of motivation, or I don't have enough time. I'm going to try to not make the same mistake this year and try to consistently do each day's problem (emphasis on "try" here).
Which starts with today, the very first problem. It's a pretty straightforward problem statement, boiling down to "in a list of numbers, find the top three common occurrences and find their product, and the same thing for the three least occurring numbers."
I ended up doing the solution in Rust because I've been meaning to get back into using it, and because it's the least used language I """know""", and I do seriously want to get better at using it, for hobby purposes.
(you know, "know" as in "i can write a 'hello world' program")
Which leads me to this abomination I wrote:
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::process;
fn main() {
let path: PathBuf = match env::args().nth(1) {
Some(arg) => PathBuf::from(arg),
None => {
let binary: String = env::args().next().unwrap();
eprintln!("Usage: {} <file>", binary);
process::exit(1);
}
};
let contents = match fs::read_to_string(&path) {
Ok(text) => text,
Err(e) => {
eprintln!("Couldn't read {}: {}", path.display(), e);
process::exit(1);
}
};
let mut counts: HashMap<&str, usize> = HashMap::new();
for line in contents.lines() {
*counts.entry(line).or_insert(0) += 1;
}
let mut counts_sorted: Vec<(&&str, &usize)> = counts.iter().collect();
counts_sorted.sort_by(|a, b| b.1.cmp(a.1));
let max_product = (counts_sorted[..3])
.iter()
.fold(1, |acc, x| acc * x.0.parse::<i32>().unwrap());
let min_product = (counts_sorted[counts_sorted.len() - 3..])
.iter()
.fold(1, |acc, x| acc * x.0.parse::<i32>().unwrap());
println!("Max: {}\tMin: {}", max_product, min_product);
}
It's probably not the most elegant solution, but it works. Granted, the bulk of this code is for processing the input you get for the problem. I essentially decided to count each number and store the counts in a hash map. From there, I can collect the values and sort it into an intermediate vector, which I then use to get the answers with folds. If you know of a better solution, please let me know.
Anyway, that's all for now. See ya when I see ya.