[WIP] Still working on day 20
This commit is contained in:
90
src/day20.rs
90
src/day20.rs
@@ -37,16 +37,16 @@ pub fn solve() {
|
||||
// println!("{:?}", piece_matches);
|
||||
|
||||
// And now we find which pieces are adjacent (same edge) to which other pieces (not caring about orientation at all)
|
||||
let mut edge_matches: HashMap<u32, usize> = HashMap::new();
|
||||
let mut matched_edge_count: HashMap<u32, usize> = HashMap::new();
|
||||
for piece_match in piece_matches {
|
||||
if piece_match.1.len() == 1 {
|
||||
continue;
|
||||
}
|
||||
piece_match.1.iter().for_each(|m| { edge_matches.entry(m.clone()).and_modify(|c| *c += 1 ).or_insert(1); })
|
||||
piece_match.1.iter().for_each(|m| { matched_edge_count.entry(m.clone()).and_modify(|c| *c += 1 ).or_insert(1); })
|
||||
}
|
||||
|
||||
println!("{:?}", edge_matches);
|
||||
let corner_pieces: Vec<u32> = edge_matches.iter().filter(|e| e.1 == &2).map(|e| e.0.clone()).collect::<Vec<_>>();
|
||||
println!("{:?}", matched_edge_count);
|
||||
let corner_pieces: Vec<u32> = matched_edge_count.iter().filter(|e| e.1 == &2).map(|e| e.0.clone()).collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(corner_pieces.len(), 4);
|
||||
|
||||
@@ -58,6 +58,34 @@ pub fn solve() {
|
||||
println!("Day X Part 2: {}", part2);
|
||||
}
|
||||
|
||||
fn make_puzzle(pieces: &HashMap<u32, Piece>, piece_matches: &HashMap<u16, Vec<u32>>, matched_edge_count: &HashMap<u32, usize>) {
|
||||
|
||||
let corner_pieces = matched_edge_count.iter().filter(|e| e.1 == &2).map(|e| e.0.clone()).collect::<Vec<u32>>();
|
||||
let edge_pieces = matched_edge_count.iter().filter(|e| e.1 == &3).map(|e| e.0.clone()).collect::<Vec<u32>>();
|
||||
let center_pieces = matched_edge_count.iter().filter(|e| e.1 == &4).map(|e| e.0.clone()).collect::<Vec<u32>>();
|
||||
|
||||
let mut puzzle = Puzzle { state: Vec::new() };
|
||||
|
||||
|
||||
// First let's make the edges:
|
||||
let first_corner_piece = pieces.get(&corner_pieces[0]);
|
||||
// TODO Find out what side of the corner pieces were matched and orient it correctly:
|
||||
|
||||
|
||||
// let last_piece = pieces.get(&corner_pieces[0]).unwrap();
|
||||
// let last_piece_state = PieceState {
|
||||
// piece_id: corner_pieces[0],
|
||||
// degrees: 0,
|
||||
// flip_horizontal: false,
|
||||
// flip_vertical: false
|
||||
// };
|
||||
puzzle.pieces.push(vec![Option::Some(last_piece_state)]);
|
||||
// loop {
|
||||
//
|
||||
//
|
||||
// }
|
||||
}
|
||||
|
||||
fn add_to_piece_matches(piece_id: u32, edge: &PieceLine, flipped_edge: &PieceLine, piece_matches: &mut HashMap<u16, Vec<u32>>) {
|
||||
|
||||
let use_flipped = flipped_edge.data < edge.data;
|
||||
@@ -66,6 +94,60 @@ fn add_to_piece_matches(piece_id: u32, edge: &PieceLine, flipped_edge: &PieceLin
|
||||
piece_matches.entry(bookkeeping_edge.data).and_modify(|d| d.push(piece_id)).or_insert(vec![ piece_id ]);
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Puzzle {
|
||||
state: Vec<Vec<Option<PieceState>>>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct PieceState {
|
||||
// 0, 90, 180 or 270
|
||||
piece_id: u32,
|
||||
degrees: u16,
|
||||
flipped: bool,
|
||||
|
||||
}
|
||||
|
||||
impl PieceState {
|
||||
|
||||
/// Returns the edge on the right, given the current piece state (i.e. this might be actually the edge on top or a flipped
|
||||
/// edge depending on the rotation
|
||||
fn right_edge(&self, piece: &Piece) -> &PieceLine {
|
||||
|
||||
return match self.degrees {
|
||||
0 =>
|
||||
if self.flipped {
|
||||
&piece.left_edge
|
||||
} else {
|
||||
&piece.right_edge
|
||||
}
|
||||
90 =>
|
||||
if self.flipped {
|
||||
&piece.flipped_top_edge
|
||||
} else {
|
||||
&piece.top_edge
|
||||
}
|
||||
180 => {
|
||||
if self.flipped {
|
||||
&piece.flipped_right_edge
|
||||
} else {
|
||||
&piece.flipped_left_edge
|
||||
}
|
||||
}
|
||||
270 => {
|
||||
if self.flipped {
|
||||
&piece.bottom_edge
|
||||
} else {
|
||||
&piece.flipped_bottom_edge
|
||||
}
|
||||
}
|
||||
_ => panic!("Unsupported rotation");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Piece {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user