DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: 26815a6480e3fed9b1b8d27f514afa69a6f57c2e aoc2024/src/day11/mod.rs -rw-r--r-- 2.6 KiB
26815a64Jonni Liljamo feat: day11 a month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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())
}