feat: Day 6 Completed
Some checks failed
Continuous integration / Check (push) Successful in 38s
Continuous integration / Test Suite (push) Successful in 38s
Continuous integration / Rustfmt (push) Successful in 27s
Continuous integration / Clippy (push) Failing after 40s
Continuous integration / build (push) Successful in 39s

This commit is contained in:
2025-12-07 22:25:11 +00:00
parent 6806e5dd19
commit a1b55ca252
4 changed files with 146 additions and 6 deletions

View File

@@ -5,16 +5,143 @@ pub struct Day06 {}
impl Solution for Day06 {
fn part1(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
if input.is_empty() {
println!("No data found.");
return Ok(Box::new("No data"));
}
// Determine the number of columns by the length of the first line
let width = input[0].len();
// Collect numbers for each column (problem)
let mut problems: Vec<Vec<String>> = Vec::new();
// Initialize empty vectors for each potential problem
for _ in 0..width {
problems.push(Vec::new());
}
// Fill the problems with numbers and operations
for line in input {
for (i, n) in line.split_whitespace().enumerate() {
problems[i].push(n.to_string());
}
}
// Clean up columns and convert them into proper problems
let mut final_problems: Vec<(Vec<i64>, char)> = Vec::new();
for col in problems {
let mut numbers = Vec::new();
let mut op = None;
for token in col {
if token == "+" || token == "*" {
op = Some(token.chars().next().unwrap());
} else if !token.is_empty() {
numbers.push(token.parse::<i64>().unwrap());
}
}
if let Some(op) = op {
if !numbers.is_empty() {
final_problems.push((numbers, op));
}
}
}
// Solve each problem
let mut grand_total = 0;
for (numbers, op) in final_problems {
let result: i64 = match op {
'+' => numbers.iter().sum(),
'*' => numbers.iter().product(),
_ => unreachable!(),
};
println!("Problem: {:?} {}, Result: {}", numbers, op, result);
grand_total += result;
}
Ok(Box::new(grand_total))
}
fn part2(
&self,
_input: &mut Vec<String>,
input: &mut Vec<String>,
) -> Result<Box<dyn std::fmt::Display>, Box<dyn std::error::Error>> {
Ok(Box::new("Ready"))
let width = input[0].len();
let height = input.len();
// Convert input into a grid of chars
let grid: Vec<Vec<char>> = input.iter().map(|line| line.chars().collect()).collect();
let mut problems: Vec<Vec<char>> = Vec::new();
for col in 0..width {
let mut column_chars = Vec::new();
for row in 0..height {
column_chars.push(grid[row][col]);
}
problems.push(column_chars);
}
// Split columns into groups separated by all-space columns
let mut grouped_columns: Vec<Vec<Vec<char>>> = Vec::new();
let mut current_group: Vec<Vec<char>> = Vec::new();
for col in problems {
if col.iter().all(|c| c.is_whitespace()) {
if !current_group.is_empty() {
grouped_columns.push(current_group);
current_group = Vec::new();
}
} else {
current_group.push(col);
}
}
if !current_group.is_empty() {
grouped_columns.push(current_group);
}
// Right-to-left: reverse the groups
grouped_columns.reverse();
let mut grand_total = 0;
for group in grouped_columns {
let mut numbers: Vec<i64> = Vec::new();
let mut op = '+';
for col in group {
let mut num_str = String::new();
for &c in &col[..height - 1] {
// all but the last row
if c.is_digit(10) {
num_str.push(c);
}
}
if !num_str.is_empty() {
numbers.push(num_str.parse::<i64>().unwrap());
}
// Last row contains operator
let last_char = col[height - 1];
if last_char == '+' || last_char == '*' {
op = last_char;
}
}
let result: i64 = match op {
'+' => numbers.iter().sum(),
'*' => numbers.iter().product(),
_ => unreachable!(),
};
grand_total += result;
}
Ok(Box::new(grand_total))
}
fn get_day(&self) -> u8 {
@@ -44,7 +171,7 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "4277556");
}
#[test]
@@ -61,6 +188,6 @@ mod test {
.unwrap()
.to_string();
assert_eq!(answer, "Ready");
assert_eq!(answer, "3263827");
}
}