-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
225 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
............ | ||
........0... | ||
.....0...... | ||
.......0.... | ||
....0....... | ||
......A..... | ||
............ | ||
............ | ||
........A... | ||
.........A.. | ||
............ | ||
............ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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....... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |