Advent of Code 2023 - Day 06
I'ts that time of year again, the Advent of Code! I'm completing the challenges this year using Rust 🦀 Day 6 was really chill!
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 6 Challenge
BTW: Complete Part 1 to have access to Part 2 😉
Solution for Part 1
use std::fs::File;
use std::io::Read;
use std::io::Result;
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_file_path = "path_to_input_file";
let mut number_of_ways = 0;
let mut file_as_str = read_file(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
while file_as_str.contains(" ") {
file_as_str = file_as_str.replace(" ", " ")
}
let (time, distance) = file_as_str.split_once("\r\n").unwrap();
let times = time.split(' ')
.filter_map(|i| i32::from_str(i).ok())
.collect::<Vec<i32>>();
let distances = distance.split(' ')
.filter_map(|i| i32::from_str(i).ok())
.collect::<Vec<i32>>();
for idx in 0..times.len() {
let race_time = times[idx];
let distance_to_beat = distances[idx];
let mut winning_opportunities = 0;
for time in 0..race_time {
let distance = (race_time - time) * (race_time - (race_time - time));
if distance > distance_to_beat {
winning_opportunities += 1;
}
}
if number_of_ways == 0 {
number_of_ways = winning_opportunities;
} else {
number_of_ways *= winning_opportunities;
}
}
println!("Number of ways to win: {}", number_of_ways);
}
fn read_file<P: AsRef<Path>>(filename: P) -> Result<String> {
let mut file = File::open(filename)?;
let mut file_as_str = String::new();
File::read_to_string(&mut file,&mut file_as_str)?;
Ok(file_as_str)
}
Solution for Part 2
use std::fs::File;
use std::io::Read;
use std::io::Result;
use std::path::Path;
use std::str::FromStr;
fn main() {
let input_file_path = "path_to_input_file";
let mut file_as_str = read_file(input_file_path)
.unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
while file_as_str.contains(" ") {
file_as_str = file_as_str.replace(" ", " ")
}
let (time, distance) = file_as_str.split_once("\r\n").unwrap();
let (_, time) = time.split_once(": ").unwrap();
let time = i64::from_str(&time.replace(' ', "")).unwrap();
let (_, distance) = distance.split_once(": ").unwrap();
let distance = i64::from_str(&distance.replace(' ', "")).unwrap();
let race_time = time;
let distance_to_beat = distance;
let mut winning_opportunities = 0;
let mut last_winning_distance = 0;
for time in 0..race_time {
let distance = (race_time - time) * (race_time - (race_time - time));
if distance > distance_to_beat {
winning_opportunities += 1;
}
if distance < last_winning_distance && distance < distance_to_beat {
// We have gone through all the winning combinations. Give up!
break
} else {
last_winning_distance = distance;
}
}
println!("Number of ways to win: {}", winning_opportunities);
}
fn read_file<P: AsRef<Path>>(filename: P) -> Result<String> {
let mut file = File::open(filename)?;
let mut file_as_str = String::new();
File::read_to_string(&mut file, &mut file_as_str)?;
Ok(file_as_str)
}