DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: 6aac5b0c9e2301b47075fe0fcb317f507d16e504 aoc2024/src/day9/part1.rs -rw-r--r-- 2.6 KiB
6aac5b0cJonni Liljamo feat: day9 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::{
    fs::File,
    io::{BufRead, BufReader},
    path::Path,
};

#[derive(Debug)]
struct FS {
    blocks: Vec<FSBlock>,
}

impl FS {
    fn _print_blocks(&self) {
        for b in &self.blocks {
            match b {
                FSBlock::File(id) => print!("{}", id),
                FSBlock::Free => print!("."),
            }
        }
        println!();
    }

    fn from_disk_map(disk_map: Vec<usize>) -> Self {
        let mut fs = FS { blocks: vec![] };
        let mut current_id = 0;
        for (i, n) in disk_map.iter().enumerate() {
            if i % 2 == 0 {
                for _ in 0..*n {
                    fs.blocks.push(FSBlock::File(current_id));
                }
                current_id += 1;
            } else {
                for _ in 0..*n {
                    fs.blocks.push(FSBlock::Free);
                }
            }
        }
        fs
    }

    fn compact(&mut self) {
        loop {
            let last_file_block = self
                .blocks
                .iter()
                .enumerate()
                .filter_map(|(i, b)| match b {
                    FSBlock::File(_) => Some(i),
                    _ => None,
                })
                .last()
                .unwrap();
            let first_free_block = self
                .blocks
                .iter()
                .enumerate()
                .filter_map(|(i, b)| match b {
                    FSBlock::Free => Some(i),
                    _ => None,
                })
                .rev()
                .last()
                .unwrap();

            if last_file_block > first_free_block {
                self.blocks.swap(first_free_block, last_file_block);
                //self.print_blocks();
            } else {
                break;
            }
        }
    }

    fn checksum(&self) -> usize {
        self.blocks
            .iter()
            .enumerate()
            .filter_map(|(i, b)| match b {
                FSBlock::File(id) => Some(i * id),
                _ => None,
            })
            .collect::<Vec<usize>>()
            .iter()
            .sum()
    }
}

#[derive(Debug, Clone)]
enum FSBlock {
    File(usize),
    Free,
}

pub 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 input = input_str
        .trim()
        .chars()
        .map(|c| -> usize { c.to_string().parse().unwrap() })
        .collect();
    let mut fs = FS::from_disk_map(input);
    //fs.print_blocks();
    fs.compact();

    Ok(fs.checksum())
}