Refactor complete

master
Joey Pollack 1 year ago
parent 56a724a487
commit 47b08aec38

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

@ -16,14 +16,21 @@ 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
{
@ -50,21 +57,116 @@ impl PointIdx
}
#[derive(Copy, Clone, PartialEq, Debug)]
//|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// 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
{
@ -102,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)
{
@ -142,9 +244,11 @@ impl Map
{
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))
{
@ -155,6 +259,8 @@ impl Map
}
}
}
}
}
true
@ -164,7 +270,7 @@ impl Map
{
if include_antinodes
{
print!("MAP (w/ antinodes):")
print!("MAP (w/ antinodes, * = antinode that covers an antenna):")
}
else
{
@ -175,13 +281,36 @@ impl Map
print!("\n\t");
for (_j, c) in row.iter().enumerate()
{
match c
let has_antenna = c.has_antenna();
let mut has_antinode = c.has_antinode();
if !include_antinodes
{
has_antinode = false;
}
if has_antenna && has_antinode
{
MapCell::Empty => print!("."),
MapCell::Freq(f) =>
print!("*");
}
else if has_antenna
{
print!("{}", *f as char);
},
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!("#");
}
else
{
print!(".");
}
}
}
@ -195,33 +324,55 @@ impl Map
pub fn dump_map_data_by_freq(self: &Map, filename: &str)
{
let mut file = File::create("foo.txt").expect(&format!("Failed to create file {}", filename));
for (freq, _locations) in &self.antenna_locations
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
{
file.write_all(&format!("\nMap for freq: {}", *freq as char).as_bytes()).expect("Failed to write to file!");
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();
for (_i, row) in self.map.iter().enumerate()
if let Some(ant_freq) = c.get_antenna_freq()
{
file.write_all(b"\n\t").expect("Failed to write to file again");
for (_j, c) in row.iter().enumerate()
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;
}
}
match c
if has_antenna && has_antinode
{
MapCell::Empty => file.write_all(b".").expect("Failed to write to file (in match statement)"),
MapCell::Freq(f) =>
file.write_all(b"*").expect("Failed to write to dump file [1]");
}
else if has_antenna
{
if *freq == *f
file.write_all(&format!("{}", *freq as char).as_bytes()).expect("Failed to write to dump file [2]")
}
else if has_antinode
{
file.write_all(&format!("{}", *f as char).as_bytes()).expect("Failed to write to file (in match statement 2)");
file.write_all(b"#").expect("Failed to write to dump file [3]");
}
else
{
file.write_all(b".").expect("Failed to write to file (in match statement 3)");
}
},
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]");
}
}
@ -289,6 +440,7 @@ impl Map
if !antinodes.contains(&antinode)
{
self.map[antinode.location.i][antinode.location.j].contents.push(MapObject::Antinode(antinode));
antinodes.push(antinode);
}

Loading…
Cancel
Save