DEVELOPMENT ENVIRONMENT

~liljamo/aoc2024

ref: 9bedcc71e44c678525219d72ec3a4b18d4687983 aoc2024/src/day8/part1.rs -rw-r--r-- 3.7 KiB
9bedcc71Jonni Liljamo feat: day8 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use std::{
    fs::File,
    io::{BufRead, BufReader},
    path::Path,
};

#[derive(Debug, Clone)]
struct Map {
    tiles: Vec<Vec<MapTile>>,
}

impl Map {
    fn print(&self) {
        for row in &self.tiles {
            for tile in row {
                match tile.tile_type {
                    MapTileType::Empty => print!(". "),
                    MapTileType::Antenna {
                        frequency,
                        also_antinode,
                    } => print!("{}{}", frequency, if also_antinode { "!" } else { " " }),
                    MapTileType::Antinode => print!("# "),
                }
            }

            println!();
        }
    }

    fn find_antennas(&self, freq: char) -> Vec<MapTile> {
        (self
            .tiles
            .iter()
            .flatten()
            .filter(|tile| match tile.tile_type {
                MapTileType::Antenna { frequency, .. } => frequency == freq,
                _ => false,
            })
            .cloned())
        .collect()
    }

    fn set_antinode(&mut self, x: usize, y: usize) {
        if let Some(row) = self.tiles.get_mut(y) {
            if let Some(tile) = row.get_mut(x) {
                match tile.tile_type {
                    MapTileType::Empty => self.tiles[y][x].tile_type = MapTileType::Antinode,
                    MapTileType::Antenna {
                        ref mut also_antinode,
                        ..
                    } => *also_antinode = true,
                    _ => {}
                }
            }
        }
    }
}

#[derive(Debug, Clone)]
struct MapTile {
    x: usize,
    y: usize,
    tile_type: MapTileType,
}

#[derive(Debug, Clone)]
enum MapTileType {
    Empty,
    Antenna {
        also_antinode: bool,
        frequency: char,
    },
    Antinode,
}

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

    let mut map: Map = Map { tiles: vec![] };
    for (y, line) in reader.lines().enumerate() {
        let mut row = vec![];
        for (x, c) in line?.chars().enumerate() {
            match c {
                '.' => row.push(MapTile {
                    x,
                    y,
                    tile_type: MapTileType::Empty,
                }),
                _ => row.push(MapTile {
                    x,
                    y,
                    tile_type: MapTileType::Antenna {
                        frequency: c,
                        also_antinode: false,
                    },
                }),
            }
        }
        map.tiles.push(row);
    }

    for (y, row) in map.clone().tiles.iter().enumerate() {
        for (x, tile) in row.iter().enumerate() {
            if let MapTileType::Antenna { frequency, .. } = tile.tile_type {
                let others = map.find_antennas(frequency);
                for other in others {
                    let x_diff: i32 = -(x as i32 - other.x as i32);
                    let y_diff: i32 = -(y as i32 - other.y as i32);
                    if y_diff == 0 && x_diff == 0 {
                        continue;
                    }
                    let antinode_x: i32 = other.x as i32 + x_diff;
                    let antinode_y: i32 = other.y as i32 + y_diff;
                    if antinode_x >= 0 && antinode_y >= 0 {
                        map.set_antinode(antinode_x as usize, antinode_y as usize);
                    }
                }
            }
        }
    }

    Ok(map
        .tiles
        .iter()
        .flatten()
        .filter(|tile| match tile.tile_type {
            MapTileType::Antenna { also_antinode, .. } => also_antinode,
            MapTileType::Antinode => true,
            _ => false,
        })
        .collect::<Vec<_>>()
        .len() as i32)
}