Advent of Code 2023 - Day 01
I'ts that time of year again, the Advent of Code 2023 released its first day of challenges. I'm completing the challenges this year using Rust.
I'll be posting them here, just to document how I'm solving each challenge, and I highly encourage you to check them out before reading my solutions!
Here's the Day 1 Challenge
BTW: Complete Part 1 to have access to Part 2 😉
Solution for Part 1
// https://gist.github.com/Incognitowski/056d29f20c9b0e4475efa6593c136cde
use std::fs::File;
use std::io::{BufRead, BufReader, Lines, Result};
use std::path::{Path};
use std::str::FromStr;
fn main() {
let input_file_path = "path_to_your_absolute_file";
let mut curr_sum = 0;
let pattern: &_ = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
let iterator = for_each_line(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
for line in iterator.flatten() {
let mut num_as_str = String::from("");
if let Some(pos) = line.find(pattern) {
num_as_str.push_str(&line[pos..=pos]);
}
if let Some(pos) = line.rfind(pattern) {
num_as_str.push_str(&line[pos..=pos]);
}
curr_sum += i32::from_str(&num_as_str).unwrap_or(0);
}
println!("The sum of all calibration values is: {}", curr_sum);
}
fn for_each_line<P: AsRef<Path>>(filename: P) -> Result<Lines<BufReader<File>>> {
let file = File::open(filename)?;
Ok(BufReader::new(file).lines())
}
Solution for Part 2
// https://gist.github.com/Incognitowski/30328092b0bce7a60da092a0c7fd1461
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufRead, BufReader, Lines, Result};
use std::path::{Path};
use std::str::FromStr;
use lazy_static::lazy_static;
lazy_static! {
static ref TRANSLATION_MAP: HashMap<&'static str, &'static str> = {
let mut m = HashMap::new();
m.insert("zero", "0");
m.insert("one", "1");
m.insert("two", "2");
m.insert("three", "3");
m.insert("four", "4");
m.insert("five", "5");
m.insert("six", "6");
m.insert("seven", "7");
m.insert("eight", "8");
m.insert("nine", "9");
m
};
}
const PATTERN: &[char; 10] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
fn main() {
let input_file_path = "absolute_path_to_input_file";
let mut curr_sum = 0;
let iterator = for_each_line(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
for line in iterator.flatten() {
println!("Raw Line-------: {}", line);
let line = replace_written_numbers(line);
println!("Translated Line: {}", line);
let mut num_as_str = String::from("");
if let Some(pos) = line.find(PATTERN) {
num_as_str.push_str(&line[pos..=pos]);
}
if let Some(pos) = line.rfind(PATTERN) {
num_as_str.push_str(&line[pos..=pos]);
}
curr_sum += i32::from_str(&num_as_str).unwrap_or(0);
}
println!("The sum of all calibration values is: {}", curr_sum);
}
fn replace_written_numbers(raw_line: String) -> String {
let mut translated_line = String::new();
for (idx, char) in raw_line.chars().enumerate() {
if PATTERN.contains(&char) {
translated_line.push(char);
continue;
}
for (key, value) in TRANSLATION_MAP.iter() {
if !key.starts_with(char) {
continue;
}
if raw_line.len() < idx+key.len() {
continue;
}
if raw_line[idx..idx+key.len()].eq(*key) {
translated_line.push_str(value)
}
}
}
translated_line
}
fn for_each_line<P: AsRef<Path>>(filename: P) -> Result<Lines<BufReader<File>>> {
let file = File::open(filename)?;
Ok(BufReader::new(file).lines())
}