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.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()
}
@ -98,6 +100,10 @@ impl Solver for Day8
println!("ERROR: Antenna locations map is broken!");
//self.map.print_ant_locations();
}
else
{
println!("Antenna locations OK");
}
}
}

@ -11,19 +11,26 @@
// 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;
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// MapObject
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum MapCell
pub enum MapObject
{
Empty,
Freq(u8),
Antinode(Antinode),
}
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// PointIdx
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct PointIdx
{
@ -38,29 +45,149 @@ impl PointIdx
PointIdx { i, j }
}
// pub fn as_glm_vec(self: & PointIdx) -> I32Vec2
// {
// I32Vec2::new(self.i as i32, self.j as i32)
// }
pub fn from_glm_vec(glm_vec: &Vec2) -> PointIdx
{
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)]
pub struct Map
{
map: Vec<Vec<MapCell>>,
antenna_locations: HashMap<u8, Vec<PointIdx>>,
antinodes: Vec<PointIdx>,
antinodes: Vec<Antinode>,
// DEBUG STUFF
first_processed_freq: u8,
}
impl 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
{
// let mut num_freqs = 0;
let mut split_pattern = "\n";
if data.contains("\r\n")
{
@ -77,10 +204,10 @@ impl Map
{
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' =>
{
parsed_row.push(MapCell::Freq(*c));
parsed_row.push(MapCell::new(vec![MapObject::Freq(*c)]));
if !antenna_locations.contains_key(c)
{
@ -101,10 +228,14 @@ impl Map
}
}
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
@ -113,18 +244,22 @@ impl Map
{
for (j, c) in row.iter().enumerate()
{
if mem::discriminant(c) == mem::discriminant(&MapCell::Freq(0))
for o in &c.contents
{
if let MapCell::Freq(f) = c
if mem::discriminant(o) == mem::discriminant(&MapObject::Freq(0))
{
if !self.antenna_locations[f].contains(&PointIdx::new(i, j))
if let MapObject::Freq(f) = o
{
println!("ERROR: Incorrect Antenna Location! Actual: ({}, {}), Locations for this frequency ({}): {:#?}",
i, j, *f as char, self.antenna_locations);
return false;
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);
return false;
}
}
}
}
}
}
@ -135,29 +270,48 @@ impl Map
{
if include_antinodes
{
print!("MAP (w/ antinodes):")
print!("MAP (w/ antinodes, * = antinode that covers an antenna):")
}
else
{
print!("MAP:");
}
for (i, row) in self.map.iter().enumerate()
for (_i, row) in self.map.iter().enumerate()
{
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;
}
else
if has_antenna && has_antinode
{
match c
print!("*");
}
else if has_antenna
{
if let Some(freq) = c.get_antenna_freq()
{
print!("{}", freq as char);
}
else
{
MapCell::Empty => print!("."),
MapCell::Freq(f) => print!("{}", *f as char),
panic!("MapCell says it has an antenna but won't give it's freq!");
}
}
else if has_antinode
{
print!("#");
}
else
{
print!(".");
}
}
}
println!();
@ -168,6 +322,62 @@ impl Map
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
// 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.
@ -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![];
for (_freq, locations) in &self.antenna_locations
let mut antinodes: Vec<Antinode> = vec![];
for (freq, locations) in &self.antenna_locations
{
self.first_processed_freq = *freq;
let mut i = 0;
while i < locations.len()
{
@ -214,26 +425,23 @@ impl Map
let direction = (second_glm - first_glm).normalize();
let new_i = (locations[i].i as f32 + direction.x * dist);
let new_j = (locations[i].j as f32 + direction.y * dist);
let new_i = locations[i].i as f32 + direction.x * dist;
let new_j = locations[i].j as f32 + direction.y * dist;
if new_i < 0.0 || new_j < 0.0
{
j += 1;
continue;
}
if new_i >= self.map.len() as f32 ||
if new_i < 0.0 || new_j < 0.0 ||
new_i >= self.map.len() as f32 ||
new_j >= self.map[0].len() as f32
{
j += 1;
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;

Loading…
Cancel
Save