Compare commits

..

No commits in common. '47b08aec38f23baf0b7b03a32f840cae03264cc4' and 'ca342cef94696829c374a09008fab7ee298a8d1c' have entirely different histories.

1872
MAP_DUMP

File diff suppressed because it is too large Load Diff

@ -52,10 +52,8 @@ impl Day8
self.map.print(true); self.map.print(true);
} }
self.map.dump_map_data_by_freq("MAP_DUMP");
// 223 -> That's not the right answer; your answer is too high.
self.final_result = antinodes.len() as i32; self.final_result = antinodes.len() as i32;
self.final_result.to_string() self.final_result.to_string()
} }
@ -100,10 +98,6 @@ impl Solver for Day8
println!("ERROR: Antenna locations map is broken!"); println!("ERROR: Antenna locations map is broken!");
//self.map.print_ant_locations(); //self.map.print_ant_locations();
} }
else
{
println!("Antenna locations OK");
}
} }
} }

@ -11,26 +11,19 @@
// single lowercase letter, uppercase letter, or digit. // single lowercase letter, uppercase letter, or digit.
use std::{io::prelude::*, fs::File, collections::HashMap, mem}; use std::{collections::HashMap, mem};
use nalgebra_glm::Vec2; use nalgebra_glm::Vec2;
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// MapObject
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
pub enum MapObject pub enum MapCell
{ {
Empty, Empty,
Freq(u8), Freq(u8),
Antinode(Antinode),
} }
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// PointIdx
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
pub struct PointIdx pub struct PointIdx
{ {
@ -45,149 +38,29 @@ impl PointIdx
PointIdx { i, j } PointIdx { i, j }
} }
pub fn from_glm_vec(glm_vec: &Vec2) -> PointIdx // pub fn as_glm_vec(self: & PointIdx) -> I32Vec2
{ // {
PointIdx{ i: glm_vec.x as usize, j: glm_vec.y as usize } // I32Vec2::new(self.i as i32, self.j as i32)
} // }
pub fn as_glm_vec(self: & PointIdx) -> Vec2
{
Vec2::new(self.i as f32, self.j as f32)
}
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Antinode
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, Debug)]
pub struct Antinode
{
location: PointIdx,
freq: u8,
}
impl PartialEq for Antinode
{
fn eq(&self, other: &Antinode) -> bool
{
self.location.i == other.location.i &&
self.location.j == other.location.j
}
}
impl Antinode
{
pub fn new(location: PointIdx, freq: u8) -> Antinode
{
Antinode { location, freq }
}
pub fn new_empty() -> Antinode
{
Antinode { location: PointIdx::new(0, 0), freq: 0 }
}
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// MapCell
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Clone, Debug)]
pub struct MapCell
{
contents: Vec<MapObject>,
}
impl MapCell
{
pub fn new(contents: Vec<MapObject>) -> MapCell
{
MapCell { contents }
}
pub fn has_antenna(self: &MapCell) -> bool
{
for mo in &self.contents
{
if mem::discriminant(mo) == mem::discriminant(&MapObject::Freq(0))
{
return true;
}
}
false
}
pub fn has_antinode(self: &MapCell) -> bool
{
for mo in &self.contents
{
if mem::discriminant(mo) == mem::discriminant(&MapObject::Antinode(Antinode::new_empty()))
{
return true;
}
}
false
}
pub fn get_antenna_freq(self: &MapCell) -> Option<u8>
{
for mo in &self.contents
{
match *mo
{
MapObject::Empty => (),
MapObject::Antinode(_) => (),
MapObject::Freq(freq) => return Some(freq),
}
}
None
}
pub fn get_antinode_freq(self: &MapCell) -> Option<u8>
{
for mo in &self.contents
{
match *mo
{
MapObject::Empty => (),
MapObject::Freq(_) => (),
MapObject::Antinode(anode) => return Some(anode.freq),
}
}
None
}
} }
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Map
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct Map pub struct Map
{ {
map: Vec<Vec<MapCell>>, map: Vec<Vec<MapCell>>,
antenna_locations: HashMap<u8, Vec<PointIdx>>, antenna_locations: HashMap<u8, Vec<PointIdx>>,
antinodes: Vec<Antinode>, antinodes: Vec<PointIdx>,
// DEBUG STUFF
first_processed_freq: u8,
} }
impl Map impl Map
{ {
pub fn new() -> Map pub fn new() -> Map
{ {
Map { map: vec![], antenna_locations: HashMap::new(), antinodes: vec![], first_processed_freq: 0 } Map { map: vec![], antenna_locations: HashMap::new(), antinodes: vec![] }
} }
pub fn parse(data: &str) -> Map pub fn parse(data: &str) -> Map
{ {
// let mut num_freqs = 0;
let mut split_pattern = "\n"; let mut split_pattern = "\n";
if data.contains("\r\n") if data.contains("\r\n")
{ {
@ -204,10 +77,10 @@ impl Map
{ {
match *c match *c
{ {
b'.' => parsed_row.push(MapCell::new(vec![MapObject::Empty])), b'.' => parsed_row.push(MapCell::Empty),
b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' => b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' =>
{ {
parsed_row.push(MapCell::new(vec![MapObject::Freq(*c)])); parsed_row.push(MapCell::Freq(*c));
if !antenna_locations.contains_key(c) if !antenna_locations.contains_key(c)
{ {
@ -228,14 +101,10 @@ impl Map
} }
} }
map.push(parsed_row); map.push(parsed_row);
} }
// DEBUG: Map { map, antenna_locations, antinodes: vec![] }
// println!("num freqs found: {}", antenna_locations.len());
Map { map, antenna_locations, antinodes: vec![], first_processed_freq: 0 }
} }
pub fn double_check_antenna_locations(self: &Map) -> bool pub fn double_check_antenna_locations(self: &Map) -> bool
@ -244,22 +113,18 @@ impl Map
{ {
for (j, c) in row.iter().enumerate() for (j, c) in row.iter().enumerate()
{ {
for o in &c.contents if mem::discriminant(c) == mem::discriminant(&MapCell::Freq(0))
{ {
if mem::discriminant(o) == mem::discriminant(&MapObject::Freq(0)) if let MapCell::Freq(f) = c
{ {
if let MapObject::Freq(f) = o if !self.antenna_locations[f].contains(&PointIdx::new(i, j))
{ {
if !self.antenna_locations[f].contains(&PointIdx::new(i, j)) println!("ERROR: Incorrect Antenna Location! Actual: ({}, {}), Locations for this frequency ({}): {:#?}",
{ i, j, *f as char, self.antenna_locations);
println!("ERROR: Incorrect Antenna Location! Actual: ({}, {}), Locations for this frequency ({}): {:#?}", return false;
i, j, *f as char, self.antenna_locations);
return false;
}
} }
} }
} }
} }
} }
@ -270,47 +135,28 @@ impl Map
{ {
if include_antinodes if include_antinodes
{ {
print!("MAP (w/ antinodes, * = antinode that covers an antenna):") print!("MAP (w/ antinodes):")
} }
else else
{ {
print!("MAP:"); print!("MAP:");
} }
for (_i, row) in self.map.iter().enumerate() for (i, row) in self.map.iter().enumerate()
{ {
print!("\n\t"); print!("\n\t");
for (_j, c) in row.iter().enumerate() for (j, c) in row.iter().enumerate()
{ {
let has_antenna = c.has_antenna(); if include_antinodes && self.antinodes.contains(&PointIdx::new(i, j))
let mut has_antinode = c.has_antinode();
if !include_antinodes
{
has_antinode = false;
}
if has_antenna && has_antinode
{
print!("*");
}
else if has_antenna
{
if let Some(freq) = c.get_antenna_freq()
{
print!("{}", freq as char);
}
else
{
panic!("MapCell says it has an antenna but won't give it's freq!");
}
}
else if has_antinode
{ {
print!("#"); print!("#");
} }
else else
{ {
print!("."); match c
{
MapCell::Empty => print!("."),
MapCell::Freq(f) => print!("{}", *f as char),
}
} }
} }
} }
@ -322,62 +168,6 @@ impl Map
println!("{:#?}", self.antenna_locations); println!("{:#?}", self.antenna_locations);
} }
pub fn dump_map_data_by_freq(self: &Map, filename: &str)
{
let mut file = File::create(filename).expect(&format!("Failed to create file {}", filename));
println!("DUMPING MAP FILE FOR EACH FREQUENCY");
for (freq, _) in &self.antenna_locations
{
println!("Dumping freq: {}", *freq as char);
file.write_all(&format!("\nMap for freq: {}\n", *freq as char).as_bytes()).expect("Failed to write to dump file [6]");
for row in &self.map
{
for c in row
{
let mut has_antenna = c.has_antenna();
if let Some(ant_freq) = c.get_antenna_freq()
{
if ant_freq != *freq
{
has_antenna = false;
}
}
let mut has_antinode = c.has_antinode();
if let Some(anode_freq) = c.get_antinode_freq()
{
if anode_freq != *freq
{
has_antinode = false;
}
}
if has_antenna && has_antinode
{
file.write_all(b"*").expect("Failed to write to dump file [1]");
}
else if has_antenna
{
file.write_all(&format!("{}", *freq as char).as_bytes()).expect("Failed to write to dump file [2]")
}
else if has_antinode
{
file.write_all(b"#").expect("Failed to write to dump file [3]");
}
else
{
file.write_all(b".").expect("Failed to write to dump file [4]");
}
}
file.write_all(b"\n").expect("Failed to write to dump file [5]");
}
}
}
// an antinode occurs at any point that is perfectly in line with two antennas of the same // an antinode occurs at any point that is perfectly in line with two antennas of the same
// frequency - but only when one of the antennas is twice as far away as the other // frequency - but only when one of the antennas is twice as far away as the other
// This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them. // This means that for any pair of antennas with the same frequency, there are two antinodes, one on either side of them.
@ -395,12 +185,11 @@ impl Map
// .......... // ..........
// .......... // ..........
pub fn find_antinodes(self: &mut Map) -> Vec<Antinode> pub fn find_antinodes(self: &mut Map) -> Vec<PointIdx>
{ {
let mut antinodes: Vec<Antinode> = vec![]; let mut antinodes: Vec<PointIdx> = vec![];
for (freq, locations) in &self.antenna_locations for (_freq, locations) in &self.antenna_locations
{ {
self.first_processed_freq = *freq;
let mut i = 0; let mut i = 0;
while i < locations.len() while i < locations.len()
{ {
@ -425,23 +214,26 @@ impl Map
let direction = (second_glm - first_glm).normalize(); let direction = (second_glm - first_glm).normalize();
let new_i = locations[i].i as f32 + direction.x * dist; let new_i = (locations[i].i as f32 + direction.x * dist);
let new_j = locations[i].j as f32 + direction.y * dist; let new_j = (locations[i].j as f32 + direction.y * dist);
if new_i < 0.0 || new_j < 0.0 || if new_i < 0.0 || new_j < 0.0
new_i >= self.map.len() as f32 || {
j += 1;
continue;
}
if new_i >= self.map.len() as f32 ||
new_j >= self.map[0].len() as f32 new_j >= self.map[0].len() as f32
{ {
j += 1; j += 1;
continue; continue;
} }
let antinode = Antinode::new(PointIdx::new(new_i as usize, new_j as usize), *freq); let antinode_location = PointIdx::new(new_i as usize, new_j as usize);
if !antinodes.contains(&antinode) if !antinodes.contains(&antinode_location)
{ {
self.map[antinode.location.i][antinode.location.j].contents.push(MapObject::Antinode(antinode)); antinodes.push(antinode_location);
antinodes.push(antinode);
} }
j += 1; j += 1;

Loading…
Cancel
Save