Zytkowski's Thought Dumpster

Advent of Code 2023 - Day 14

I'ts that time of year again, the Advent of Code! I'm completing the challenges this year using Rust 🦀 Day 14 was fun, even though I only managed to complete part 1 again 😅

I'll be posting my solutions 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 14 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::path::Path;

fn main() {
    let input_file_path = "path_to_input";
    let mut curr_sum = 0;
    let file = file_as_string(input_file_path)
        .unwrap_or_else(|_| panic!("Could not find input file {}", input_file_path));
    let mut board : Vec<Vec<char>> = Vec::new();
    for row in file.split("\r\n") {
        board.push(row.chars().collect());
    }
    let mut board_is_stable = false;
    'main: while !board_is_stable {
        let board_copy = board.clone();
        for (row_num, row) in board_copy.iter().clone().enumerate() {
            if row_num == 0 {
                continue;
            }
            for (col_num, cell) in row.iter().enumerate() {
                if cell != &'O' {
                    continue;
                }
                let upper_cell = &mut board[row_num - 1][col_num];
                if upper_cell != &'.' {
                    continue;
                }
                board[row_num][col_num] = '.';
                board[row_num - 1][col_num] = 'O';
                continue 'main;
            }
        }
        board_is_stable = true
    }
    for (row_num, row) in board.iter().enumerate() {
        for cell in row.iter() {
            if cell == &'O' {
                curr_sum += board.len() - row_num;
            }
        }
    }
    println!("Total load on north beam: {}", curr_sum)
}

fn file_as_string<P: AsRef<Path>>(filename: P) -> std::io::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

// I will do this. One day. :P