MCPcopy Create free account
hub / github.com/AxlLind/AdventOfCode / build_image

Function build_image

2020/src/bin/20.rs:88–117  ·  view source on GitHub ↗
(matches: &BorderMatches, corner: usize)

Source from the content-addressed store, hash-verified

86}
87
88fn build_image(matches: &BorderMatches, corner: usize) -> Vec<Vec<char>> {
89 let images = IMAGES.iter().copied().collect::<HashMap<_,_>>();
90
91 // align the corner to fit in the top-left
92 let mut starting_corner = Tile::from_input(&images[&corner], corner);
93 while [0,3].iter().any(|&d| starting_corner.get_neighbour(matches, d).is_some()) {
94 starting_corner.rotate();
95 }
96
97 let mut image = vec![vec![Tile::default(); 12]; 12];
98 image[0][0] = starting_corner;
99 // match the first tile in each row to the one above
100 for i in 1..12 {
101 image[i][0] = image[i-1][0].match_down(matches, &images);
102 }
103 // for all other tiles match to the previous tile in the row
104 for (i,j) in (0..12).cartesian_product(1..12) {
105 image[i][j] = image[i][j-1].match_right(matches, &images);
106 }
107
108 // tiles are placed and rotated correctly, now build the actual image
109 let mut actual_image = vec![Vec::new(); 8 * 12];
110 for (i,j) in (0..12).cartesian_product(0..12) {
111 let tile = &image[i][j];
112 for k in 1..9 {
113 actual_image[i * 8 + (k-1)].extend(&tile.v[k][1..9]);
114 }
115 }
116 actual_image
117}
118
119fn find_monsters(image: &[Vec<char>], monster_coords: &HashSet<(isize,isize)>) -> usize {
120 let positions = image.iter()

Callers 1

part_twoFunction · 0.85

Calls 4

get_neighbourMethod · 0.80
rotateMethod · 0.80
match_downMethod · 0.80
match_rightMethod · 0.80

Tested by

no test coverage detected