diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index d406aa3..1c46822 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,33 +1,14 @@ -# This file is a template, and might need editing before it works on your project. -# To contribute improvements to CI/CD templates, please follow the Development guide at: -# https://docs.gitlab.com/ee/development/cicd/templates.html -# This specific template is located at: -# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Rust.gitlab-ci.yml - -# Official language image. Look for the different tagged releases at: -# https://hub.docker.com/r/library/rust/tags/ image: "rust:latest" -# Optional: Pick zero or more services to be used on all builds. -# Only needed when using a docker container to run your tests in. -# Check out: http://docs.gitlab.com/ee/ci/docker/using_docker_images.html#what-is-a-service -# services: -# - mysql:latest -# - redis:latest -# - postgres:latest - -# Optional: Install a C compiler, cmake and git into the container. -# You will often need this when you (or any of your dependencies) depends on C code. -# before_script: -# - apt-get update -yqq -# - apt-get install -yqq --no-install-recommends build-essential - # Use cargo to test the project -test:cargo: +test:2020: + script: + - cd 2020 + - rustc --version && cargo --version # Print version info for debugging + - cargo test --workspace --verbose -j 8 + +test:2021: script: - cd 2021 - rustc --version && cargo --version # Print version info for debugging - cargo test --workspace --verbose -j 8 - - cd ../2020 - - rustc --version && cargo --version # Print version info for debugging - - cargo test --workspace --verbose -j 8 diff --git a/2020/src/day20.rs b/2020/src/day20.rs index 12cf496..4684b87 100644 --- a/2020/src/day20.rs +++ b/2020/src/day20.rs @@ -83,13 +83,10 @@ fn configure_board( || (y != 0 && tile.fits_with_right_border_of(board[x][y - 1].as_ref().unwrap())) { board[x][y] = Some(tile.clone()); - // println!("found match for {x}, {y}, {tile}"); if let Some(ans) = configure_board(board, tiles_left, new_x, new_y, n) { return Some(ans); } - } else { - // println!("no match found for {x}, {y}, {tile}"); } tile.flip(); } @@ -104,8 +101,7 @@ fn part_one(mut input: HashSet) -> u128 { let n2 = input.len(); let n = (n2 as f64).sqrt() as usize; let mut board = vec![vec![None; n]; n]; - let value = configure_board(&mut board, &mut input, 0, 0, n); - let board = value.unwrap(); + let board = configure_board(&mut board, &mut input, 0, 0, n).unwrap(); board[0][0].id * board[0][n - 1].id * board[n - 1][0].id * board[n - 1][n - 1].id } @@ -114,22 +110,19 @@ fn part_two(mut input: HashSet) -> u128 { let n = (n2 as f64).sqrt() as usize; let mut board = vec![vec![None; n]; n]; let board = configure_board(&mut board, &mut input, 0, 0, n).unwrap(); + + let mut pic = vec![vec![]; n]; let tile_size = board[0][0].grid.len(); - let _picture = { - let mut pic = vec![vec![]; n]; - for (x, board_row) in board.iter().enumerate() { - for tile in board_row { - for (i, tile_row) in tile.grid.iter().enumerate() { - for c in tile_row { - pic[x * (tile_size - 2) + i].push(*c); - } + for (x, board_row) in board.iter().enumerate() { + for tile in board_row { + for (i, tile_row) in tile.grid.iter().enumerate() { + for c in tile_row { + pic[x * (tile_size - 2) + i].push(*c); } } } - pic - }; + } - // turn board into picture // flip/rotate pitctures // traverse each image for sea monsters @@ -159,25 +152,16 @@ mod tests { } fn parse_inputs(input: &str) -> HashSet { - let mut tiles = HashSet::new(); - let chunks: Vec<&str> = input.split("\n\n").collect(); - for chunk in &chunks[0..chunks.len() - 1] { - let mut line_it = chunk.split('\n'); - let tile_id_line = line_it.next().unwrap(); - let id = tile_id_line[5..tile_id_line.chars().count() - 1] - .parse() - .unwrap(); - let mut grid = vec![]; - for line in line_it { - grid.push(line.chars().collect()); - } - tiles.insert(Tile { id, grid }); - } - tiles + input + .trim() + .split("\n\n") + .map(str::parse) + .map(Result::unwrap) + .collect() } } -// utility traits for data structures and debugging +// utility traits for parsing, hashing, equality, and printing tiles impl PartialEq for Tile { fn eq(&self, other: &Self) -> bool { self.id == other.id @@ -188,6 +172,22 @@ impl std::hash::Hash for Tile { self.id.hash(state); } } +impl std::str::FromStr for Tile { + type Err = std::string::ParseError; + + fn from_str(s: &str) -> Result { + let (id_line, grid_lines) = s.split_once('\n').unwrap(); + Ok(Tile { + id: id_line[5..id_line.chars().count() - 1].parse().unwrap(), + grid: grid_lines + .trim() + .split('\n') + .map(str::chars) + .map(Iterator::collect) + .collect(), + }) + } +} impl Display for Tile { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { writeln!(f, "tile: {}", self.id)?;