Skip to content

Commit

Permalink
Advent of Code 2024
Browse files Browse the repository at this point in the history
[+] Day 8: Resonant Collinearity
  • Loading branch information
IvanDyachenko committed Dec 12, 2024
1 parent 4c2ed31 commit d5715d0
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
2 changes: 2 additions & 0 deletions adventofcode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 12 additions & 0 deletions adventofcode/year2024/day08/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
50 changes: 50 additions & 0 deletions adventofcode/year2024/day08/input.txt
Original file line number Diff line number Diff line change
@@ -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.......
78 changes: 78 additions & 0 deletions adventofcode/year2024/day08/part01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::{
cmp::{max, min},
collections::{HashMap, HashSet},
};

#[derive(Debug)]
pub struct Map {
width: usize,
height: usize,
antennas: HashMap<char, HashSet<(usize, usize)>>,
}

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);
}
83 changes: 83 additions & 0 deletions adventofcode/year2024/day08/part02.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
use std::{
cmp::max,
collections::{HashMap, HashSet},
};

#[derive(Debug)]
pub struct Map {
width: usize,
height: usize,
antennas: HashMap<char, HashSet<(usize, usize)>>,
}

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);
}

0 comments on commit d5715d0

Please sign in to comment.