Compare commits

..

2 Commits

Author SHA1 Message Date
Joey Pollack 47b08aec38 Refactor complete 1 year ago
Joey Pollack 56a724a487 Stuck on day 8 1 year ago

1872
MAP_DUMP

File diff suppressed because it is too large Load Diff

@ -52,8 +52,10 @@ impl Day8
self.map.print(true); self.map.print(true);
} }
self.final_result = antinodes.len() as i32; 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.to_string() self.final_result.to_string()
} }
@ -98,6 +100,10 @@ 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,19 +11,26 @@
// single lowercase letter, uppercase letter, or digit. // single lowercase letter, uppercase letter, or digit.
use std::{collections::HashMap, mem}; use std::{io::prelude::*, fs::File, collections::HashMap, mem};
use nalgebra_glm::Vec2; use nalgebra_glm::Vec2;
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// MapObject
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, PartialEq, Debug)] #[derive(Copy, Clone, PartialEq, Debug)]
pub enum MapCell pub enum MapObject
{ {
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
{ {
@ -38,29 +45,149 @@ impl PointIdx
PointIdx { i, j } PointIdx { i, j }
} }
// pub fn as_glm_vec(self: & PointIdx) -> I32Vec2 pub fn from_glm_vec(glm_vec: &Vec2) -> PointIdx
// { {
// I32Vec2::new(self.i as i32, self.j as i32) PointIdx{ i: glm_vec.x as usize, j: glm_vec.y as usize }
// } }
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<PointIdx>, antinodes: Vec<Antinode>,
// 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![] } Map { map: vec![], antenna_locations: HashMap::new(), antinodes: vec![], first_processed_freq: 0 }
} }
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")
{ {
@ -77,10 +204,10 @@ impl Map
{ {
match *c match *c
{ {
b'.' => parsed_row.push(MapCell::Empty), b'.' => parsed_row.push(MapCell::new(vec![MapObject::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::Freq(*c)); parsed_row.push(MapCell::new(vec![MapObject::Freq(*c)]));
if !antenna_locations.contains_key(c) if !antenna_locations.contains_key(c)
{ {
@ -101,10 +228,14 @@ impl Map
} }
} }
map.push(parsed_row); map.push(parsed_row);
} }
Map { map, antenna_locations, antinodes: vec![] } // DEBUG:
// 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
@ -113,9 +244,11 @@ impl Map
{ {
for (j, c) in row.iter().enumerate() for (j, c) in row.iter().enumerate()
{ {
if mem::discriminant(c) == mem::discriminant(&MapCell::Freq(0)) for o in &c.contents
{
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))
{ {
@ -126,6 +259,8 @@ impl Map
} }
} }
} }
}
} }
true true
@ -135,28 +270,47 @@ impl Map
{ {
if include_antinodes if include_antinodes
{ {
print!("MAP (w/ antinodes):") print!("MAP (w/ antinodes, * = antinode that covers an antenna):")
} }
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()
{ {
if include_antinodes && self.antinodes.contains(&PointIdx::new(i, j)) let has_antenna = c.has_antenna();
let mut has_antinode = c.has_antinode();
if !include_antinodes
{ {
print!("#"); 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 else
{ {
match c panic!("MapCell says it has an antenna but won't give it's freq!");
}
}
else if has_antinode
{ {
MapCell::Empty => print!("."), print!("#");
MapCell::Freq(f) => print!("{}", *f as char),
} }
else
{
print!(".");
} }
} }
} }
@ -168,6 +322,62 @@ 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.
@ -185,11 +395,12 @@ impl Map
// .......... // ..........
// .......... // ..........
pub fn find_antinodes(self: &mut Map) -> Vec<PointIdx> pub fn find_antinodes(self: &mut Map) -> Vec<Antinode>
{ {
let mut antinodes: Vec<PointIdx> = vec![]; let mut antinodes: Vec<Antinode> = 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()
{ {
@ -214,26 +425,23 @@ 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_location = PointIdx::new(new_i as usize, new_j as usize); let antinode = Antinode::new(PointIdx::new(new_i as usize, new_j as usize), *freq);
if !antinodes.contains(&antinode_location) if !antinodes.contains(&antinode)
{ {
antinodes.push(antinode_location); self.map[antinode.location.i][antinode.location.j].contents.push(MapObject::Antinode(antinode));
antinodes.push(antinode);
} }
j += 1; j += 1;

Loading…
Cancel
Save