mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 09:23:17 -09:00
refactor parsing; separate test suites
This commit is contained in:
parent
8deb5610a0
commit
28119de098
@ -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"
|
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
|
# 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:
|
script:
|
||||||
- cd 2021
|
- cd 2021
|
||||||
- rustc --version && cargo --version # Print version info for debugging
|
- rustc --version && cargo --version # Print version info for debugging
|
||||||
- cargo test --workspace --verbose -j 8
|
- cargo test --workspace --verbose -j 8
|
||||||
- cd ../2020
|
|
||||||
- rustc --version && cargo --version # Print version info for debugging
|
|
||||||
- cargo test --workspace --verbose -j 8
|
|
||||||
|
@ -83,13 +83,10 @@ fn configure_board(
|
|||||||
|| (y != 0 && tile.fits_with_right_border_of(board[x][y - 1].as_ref().unwrap()))
|
|| (y != 0 && tile.fits_with_right_border_of(board[x][y - 1].as_ref().unwrap()))
|
||||||
{
|
{
|
||||||
board[x][y] = Some(tile.clone());
|
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) {
|
if let Some(ans) = configure_board(board, tiles_left, new_x, new_y, n) {
|
||||||
return Some(ans);
|
return Some(ans);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// println!("no match found for {x}, {y}, {tile}");
|
|
||||||
}
|
}
|
||||||
tile.flip();
|
tile.flip();
|
||||||
}
|
}
|
||||||
@ -104,8 +101,7 @@ fn part_one(mut input: HashSet<Tile>) -> u128 {
|
|||||||
let n2 = input.len();
|
let n2 = input.len();
|
||||||
let n = (n2 as f64).sqrt() as usize;
|
let n = (n2 as f64).sqrt() as usize;
|
||||||
let mut board = vec![vec![None; n]; n];
|
let mut board = vec![vec![None; n]; n];
|
||||||
let value = configure_board(&mut board, &mut input, 0, 0, n);
|
let board = configure_board(&mut board, &mut input, 0, 0, n).unwrap();
|
||||||
let board = value.unwrap();
|
|
||||||
board[0][0].id * board[0][n - 1].id * board[n - 1][0].id * board[n - 1][n - 1].id
|
board[0][0].id * board[0][n - 1].id * board[n - 1][0].id * board[n - 1][n - 1].id
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,9 +110,9 @@ fn part_two(mut input: HashSet<Tile>) -> u128 {
|
|||||||
let n = (n2 as f64).sqrt() as usize;
|
let n = (n2 as f64).sqrt() as usize;
|
||||||
let mut board = vec![vec![None; n]; n];
|
let mut board = vec![vec![None; n]; n];
|
||||||
let board = configure_board(&mut board, &mut input, 0, 0, n).unwrap();
|
let board = configure_board(&mut board, &mut input, 0, 0, n).unwrap();
|
||||||
let tile_size = board[0][0].grid.len();
|
|
||||||
let _picture = {
|
|
||||||
let mut pic = vec![vec![]; n];
|
let mut pic = vec![vec![]; n];
|
||||||
|
let tile_size = board[0][0].grid.len();
|
||||||
for (x, board_row) in board.iter().enumerate() {
|
for (x, board_row) in board.iter().enumerate() {
|
||||||
for tile in board_row {
|
for tile in board_row {
|
||||||
for (i, tile_row) in tile.grid.iter().enumerate() {
|
for (i, tile_row) in tile.grid.iter().enumerate() {
|
||||||
@ -126,10 +122,7 @@ fn part_two(mut input: HashSet<Tile>) -> u128 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pic
|
|
||||||
};
|
|
||||||
|
|
||||||
// turn board into picture
|
|
||||||
// flip/rotate pitctures
|
// flip/rotate pitctures
|
||||||
// traverse each image for sea monsters
|
// traverse each image for sea monsters
|
||||||
|
|
||||||
@ -159,25 +152,16 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn parse_inputs(input: &str) -> HashSet<Tile> {
|
fn parse_inputs(input: &str) -> HashSet<Tile> {
|
||||||
let mut tiles = HashSet::new();
|
input
|
||||||
let chunks: Vec<&str> = input.split("\n\n").collect();
|
.trim()
|
||||||
for chunk in &chunks[0..chunks.len() - 1] {
|
.split("\n\n")
|
||||||
let mut line_it = chunk.split('\n');
|
.map(str::parse)
|
||||||
let tile_id_line = line_it.next().unwrap();
|
.map(Result::unwrap)
|
||||||
let id = tile_id_line[5..tile_id_line.chars().count() - 1]
|
.collect()
|
||||||
.parse()
|
|
||||||
.unwrap();
|
|
||||||
let mut grid = vec![];
|
|
||||||
for line in line_it {
|
|
||||||
grid.push(line.chars().collect());
|
|
||||||
}
|
|
||||||
tiles.insert(Tile { id, grid });
|
|
||||||
}
|
|
||||||
tiles
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// utility traits for data structures and debugging
|
// utility traits for parsing, hashing, equality, and printing tiles
|
||||||
impl PartialEq for Tile {
|
impl PartialEq for Tile {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.id == other.id
|
self.id == other.id
|
||||||
@ -188,6 +172,22 @@ impl std::hash::Hash for Tile {
|
|||||||
self.id.hash(state);
|
self.id.hash(state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
impl std::str::FromStr for Tile {
|
||||||
|
type Err = std::string::ParseError;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
|
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 {
|
impl Display for Tile {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
writeln!(f, "tile: {}", self.id)?;
|
writeln!(f, "tile: {}", self.id)?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user