DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

26815a6480e3fed9b1b8d27f514afa69a6f57c2e — Jonni Liljamo a month ago fe0defd
feat: day11
3 files changed, 97 insertions(+), 0 deletions(-)

A input/day11/example
A src/day11/mod.rs
M src/main.rs
A input/day11/example => input/day11/example +1 -0
@@ 0,0 1,1 @@
125 17

A src/day11/mod.rs => src/day11/mod.rs +88 -0
@@ 0,0 1,88 @@
use std::{
    collections::HashMap,
    fs::File,
    io::{BufRead, BufReader},
    path::Path,
};

pub fn solve(input: &Path) -> anyhow::Result<()> {
    println!("part one: {}", part_one(input)?);
    println!("part two: {}", part_two(input)?);

    Ok(())
}

fn part_one(input: &Path) -> anyhow::Result<usize> {
    let mut reader = BufReader::new(File::open(input)?);

    let mut input_str = String::new();
    let _ = reader.read_line(&mut input_str)?;
    let mut stones: Vec<Vec<usize>> = input_str
        .split_whitespace()
        .map(|c| -> Vec<usize> { vec![c.to_string().parse().unwrap()] })
        .collect();

    for _ in 0..25 {
        for stone in &mut stones {
            if stone[0] == 0 {
                stone[0] = 1;
                continue;
            }
            let stone_str = stone[0].to_string();
            if stone_str.len() % 2 == 0 {
                let (right, left) = stone_str.split_at(stone_str.len() / 2);
                *stone = vec![right.parse()?, left.parse()?];
            } else {
                stone[0] *= 2024;
            }
        }
        stones = stones
            .iter()
            .flatten()
            .map(|stone| -> Vec<usize> { vec![*stone] })
            .collect();
    }

    Ok(stones.iter().flatten().count())
}

fn part_two(input: &Path) -> anyhow::Result<usize> {
    let mut reader = BufReader::new(File::open(input)?);

    let mut input_str = String::new();
    let _ = reader.read_line(&mut input_str)?;
    let input_stones: Vec<usize> = input_str
        .split_whitespace()
        .map(|c| -> usize { c.to_string().parse().unwrap() })
        .collect();
    let mut stones: HashMap<usize, usize> = HashMap::new();
    for stone in input_stones {
        if let Some(amount) = stones.get_mut(&stone) {
            *amount += 1;
        } else {
            stones.insert(stone, 1);
        }
    }
    for _ in 0..75 {
        for (stone, amount) in &stones.clone() {
            *stones.entry(*stone).or_insert(0) -= amount;
            if *stone == 0 {
                *stones.entry(1).or_insert(0) += amount;
                continue;
            }
            let stone_str = stone.to_string();
            if stone_str.len() % 2 == 0 {
                let (right, left) = stone_str.split_at(stone_str.len() / 2);
                let right = right.parse()?;
                let left = left.parse()?;
                *stones.entry(right).or_insert(0) += amount;
                *stones.entry(left).or_insert(0) += amount;
            } else {
                let new = stone * 2024;
                *stones.entry(new).or_insert(0) += amount;
            }
        }
    }

    Ok(stones.values().sum())
}

M src/main.rs => src/main.rs +8 -0
@@ 4,6 4,7 @@ use clap::{Parser, Subcommand};

mod day1;
mod day10;
mod day11;
mod day2;
mod day3;
mod day4;


@@ 61,6 62,10 @@ enum DayArgs {
        #[arg(short)]
        input: PathBuf,
    },
    Day11 {
        #[arg(short)]
        input: PathBuf,
    },
}

fn main() -> anyhow::Result<()> {


@@ 97,6 102,9 @@ fn main() -> anyhow::Result<()> {
        DayArgs::Day10 { input } => {
            day10::solve(&input)?;
        }
        DayArgs::Day11 { input } => {
            day11::solve(&input)?;
        }
    }

    Ok(())