diff --git a/adventofcode/Cargo.toml b/adventofcode/Cargo.toml index 0ca5221..c30e0b5 100644 --- a/adventofcode/Cargo.toml +++ b/adventofcode/Cargo.toml @@ -13,6 +13,8 @@ bin = [ { name = "2024-06-02", path = "year2024/day06/part02.rs" }, { name = "2024-07-01", path = "year2024/day07/part01.rs" }, { name = "2024-07-02", path = "year2024/day07/part02.rs" }, + { name = "2024-08-01", path = "year2024/day08/part01.rs" }, + { name = "2024-08-02", path = "year2024/day08/part02.rs" }, ] [package] diff --git a/adventofcode/year2024/day08/example.txt b/adventofcode/year2024/day08/example.txt new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/adventofcode/year2024/day08/example.txt @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/adventofcode/year2024/day08/input.txt b/adventofcode/year2024/day08/input.txt new file mode 100644 index 0000000..4cfb162 --- /dev/null +++ b/adventofcode/year2024/day08/input.txt @@ -0,0 +1,50 @@ +..............U.............c.....3............... +.....p.........F.................................. +.....m..7....................4x............3...... +..e.............F..........c...YH..3.............. +.......e...................................c..E..8 +................a...U................8............ +..............................4.F...8....x........ +............7.....4............Hc..E.......x...... +........p..............................E.......... +.............U.e....................x....t........ +.7..........................Z.H....g.............. +.........7..m.....S.........................E..... +...F.....p...........6...SY....................... +.................6..k...................g......... +..........m......a........................g....... +.......M.......................................g.. +..............a............Y....C........H........ +....u.......6........a.........C.GY............... +.....M..................S......................2.. +..........M........S.....................2........ +........M.......................5.........z..f.... +.....................................Z........t.2. +..........6....................................... +......................................G........... +.........................A.........G9....Z........ +........................C......................... +.....k......................G......z..t........... +.......k......................zs....f........5...9 +................h........................9....2... +.............h.....0...........f.....K..ZX........ +..................................f............... +.......1....................9.........Xz.......... +...............1......B.s......X.................. +............h...............B..................... +..T.........k..................b.................. +...............u.................................. +.........u.............h..................0....... +..............y................................... +...............................t....X......5...... +.................A............................5... +................u..................s.............. +.T..........b....y................................ +............y............................K........ +..1...............................s....B.......... +..............Ay.............B...P................ +..........T.......................K...........0... +.............T..................P.........K....... +......A....P...................................... +....b.........1................................... +.........b................................P....... diff --git a/adventofcode/year2024/day08/part01.rs b/adventofcode/year2024/day08/part01.rs new file mode 100644 index 0000000..484990b --- /dev/null +++ b/adventofcode/year2024/day08/part01.rs @@ -0,0 +1,78 @@ +use std::{ + cmp::{max, min}, + collections::{HashMap, HashSet}, +}; + +#[derive(Debug)] +pub struct Map { + width: usize, + height: usize, + antennas: HashMap>, +} + +impl Map { + pub fn parse(text: &str) -> Self { + let (mut width, mut height) = (0, 0); + let mut antennas = HashMap::new(); + + text.split("\n") + .filter(|l| !l.is_empty()) + .enumerate() + .for_each(|(y, cs)| { + height = max(y, height); + cs.char_indices().for_each(|(x, c)| { + width = max(x, width); + if c != '.' { + antennas.entry(c).or_insert(HashSet::new()).insert((x, y)); + } + }) + }); + + Map { + width, + height, + antennas, + } + } + + pub fn antinodes(&self) -> HashSet<(usize, usize)> { + let mut antinodes = HashSet::new(); + let (width, height) = (self.width as isize, self.height as isize); + + self.antennas.values().for_each(|points| { + for (i, (px, py)) in points.iter().enumerate() { + let (px, py) = (*px, *py); + for (qx, qy) in points.iter().skip(i + 1) { + let (qx, qy) = (*qx, *qy); + let dx = px as isize - qx as isize; + let dy = py as isize - qy as isize; + + let ax = qx as isize - dx; + let ay = qy as isize - dy; + + if ax >= 0 && ax <= width && ay >= 0 && ay <= height { + antinodes.insert((ax as usize, ay as usize)); + } + + let bx = px as isize + dx; + let by = py as isize + dy; + + if bx >= 0 && bx <= width && by >= 0 && by <= height { + antinodes.insert((bx as usize, by as usize)); + } + } + } + }); + + antinodes + } +} + +fn main() { + let text = std::fs::read_to_string("input.txt").unwrap(); + let map = Map::parse(&text); + let antinodes = map.antinodes(); + + let answer = antinodes.len(); + println!("{}", answer); +} diff --git a/adventofcode/year2024/day08/part02.rs b/adventofcode/year2024/day08/part02.rs new file mode 100644 index 0000000..654e5a1 --- /dev/null +++ b/adventofcode/year2024/day08/part02.rs @@ -0,0 +1,83 @@ +use std::{ + cmp::max, + collections::{HashMap, HashSet}, +}; + +#[derive(Debug)] +pub struct Map { + width: usize, + height: usize, + antennas: HashMap>, +} + +impl Map { + pub fn parse(text: &str) -> Self { + let (mut width, mut height) = (0, 0); + let mut antennas = HashMap::new(); + + text.split("\n") + .filter(|l| !l.is_empty()) + .enumerate() + .for_each(|(y, cs)| { + height = max(y, height); + cs.char_indices().for_each(|(x, c)| { + width = max(x, width); + if c != '.' { + antennas.entry(c).or_insert(HashSet::new()).insert((x, y)); + } + }) + }); + + Map { + width, + height, + antennas, + } + } + + pub fn antinodes(&self) -> HashSet<(usize, usize)> { + let mut antinodes = HashSet::new(); + let (width, height) = (self.width as isize, self.height as isize); + + self.antennas.values().for_each(|points| { + for (px, py) in points.iter() { + let (px, py) = (*px, *py); + + for (qx, qy) in points.iter() { + let (qx, qy) = (*qx, *qy); + + if px == qx && py == qy { + continue; + } + + let dx = px as isize - qx as isize; + let dy = py as isize - qy as isize; + + let k = max(width / dx, height / dy); + + let mut x = qx as isize - k * dx; + let mut y = qy as isize - k * dy; + + for _ in 1..(2 * k) { + x += dx; + y += dy; + if x >= 0 && x <= width && y >= 0 && y <= height { + antinodes.insert((x as usize, y as usize)); + } + } + } + } + }); + + antinodes + } +} + +fn main() { + let text = std::fs::read_to_string("input.txt").unwrap(); + let map = Map::parse(&text); + let antinodes = map.antinodes(); + + let answer = antinodes.len(); + println!("{}", answer); +}