changed name of Map to World, to avoid confusion with the list comprehension
This commit is contained in:
parent
965bdca334
commit
62ff4680a5
12
src/day12.rs
12
src/day12.rs
|
@ -3,7 +3,7 @@ use crate::util::maps::*;
|
||||||
const DISCOUNT: bool = true;
|
const DISCOUNT: bool = true;
|
||||||
|
|
||||||
pub fn day12() {
|
pub fn day12() {
|
||||||
let mut plots: Map<char> = Map::from_string(&crate::input(12));
|
let mut plots: World<char> = World::from_string(&crate::input(12));
|
||||||
|
|
||||||
let mut regions: Vec<Region> = vec![];
|
let mut regions: Vec<Region> = vec![];
|
||||||
for (x, y) in plots.coordinates() {
|
for (x, y) in plots.coordinates() {
|
||||||
|
@ -78,20 +78,20 @@ impl Border {
|
||||||
|
|
||||||
struct Region {
|
struct Region {
|
||||||
pub borders: Vec<Border>,
|
pub borders: Vec<Border>,
|
||||||
pub tiles: Map<bool>,
|
pub tiles: World<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Region {
|
impl Region {
|
||||||
fn new(x: usize, y: usize, plots: &Map<char>) -> Self {
|
fn new(x: usize, y: usize, plots: &World<char>) -> Self {
|
||||||
let c = plots.get(x, y).unwrap();
|
let c = plots.get(x, y).unwrap();
|
||||||
let mut tiles = Map::<bool>::from_dimensions(plots.width(), plots.height(), false);
|
let mut tiles = World::<bool>::from_dimensions(plots.width(), plots.height(), false);
|
||||||
|
|
||||||
fn collect_plots(
|
fn collect_plots(
|
||||||
c: char,
|
c: char,
|
||||||
x: usize,
|
x: usize,
|
||||||
y: usize,
|
y: usize,
|
||||||
plots: &Map<char>,
|
plots: &World<char>,
|
||||||
tiles: &mut Map<bool>,
|
tiles: &mut World<bool>,
|
||||||
) -> Vec<(usize, usize)> {
|
) -> Vec<(usize, usize)> {
|
||||||
if plots.get(x, y) != Some(c) || tiles.get(x, y) == Some(true) {
|
if plots.get(x, y) != Some(c) || tiles.get(x, y) == Some(true) {
|
||||||
return vec![];
|
return vec![];
|
||||||
|
|
14
src/day15.rs
14
src/day15.rs
|
@ -40,11 +40,11 @@ pub fn day15() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_on_map(
|
fn move_on_map(
|
||||||
map: &Map<Terrain>,
|
map: &World<Terrain>,
|
||||||
x: usize,
|
x: usize,
|
||||||
y: usize,
|
y: usize,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
) -> Option<Map<Terrain>> {
|
) -> Option<World<Terrain>> {
|
||||||
let cascading_move = || {
|
let cascading_move = || {
|
||||||
let (nx, ny) = map.travel(x, y, direction)?;
|
let (nx, ny) = map.travel(x, y, direction)?;
|
||||||
let mut downstream = move_on_map(map, nx, ny, direction)?;
|
let mut downstream = move_on_map(map, nx, ny, direction)?;
|
||||||
|
@ -82,7 +82,7 @@ fn move_on_map(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn double_wide_map(map: Map<Terrain>) -> Map<Terrain> {
|
fn double_wide_map(map: World<Terrain>) -> World<Terrain> {
|
||||||
let new_map = map
|
let new_map = map
|
||||||
.iter_rows()
|
.iter_rows()
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
|
@ -100,7 +100,7 @@ fn double_wide_map(map: Map<Terrain>) -> Map<Terrain> {
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Map::from_2d_vec(new_map)
|
World::from_2d_vec(new_map)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||||
|
@ -129,7 +129,7 @@ impl Display for Terrain {
|
||||||
#[grammar = "grammars/day15.pest"]
|
#[grammar = "grammars/day15.pest"]
|
||||||
struct Day15Parser {}
|
struct Day15Parser {}
|
||||||
|
|
||||||
fn parse(input: &str) -> (Map<Terrain>, Vec<Direction>) {
|
fn parse(input: &str) -> (World<Terrain>, Vec<Direction>) {
|
||||||
let (map, directions) = Day15Parser::parse(Rule::input, input)
|
let (map, directions) = Day15Parser::parse(Rule::input, input)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.nth(0)
|
.nth(0)
|
||||||
|
@ -142,7 +142,7 @@ fn parse(input: &str) -> (Map<Terrain>, Vec<Direction>) {
|
||||||
(parse_map(map), parse_directions(directions))
|
(parse_map(map), parse_directions(directions))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_map(input: Pairs<'_, Rule>) -> Map<Terrain> {
|
fn parse_map(input: Pairs<'_, Rule>) -> World<Terrain> {
|
||||||
let map_vec = input
|
let map_vec = input
|
||||||
.map(Pair::<'_, Rule>::into_inner)
|
.map(Pair::<'_, Rule>::into_inner)
|
||||||
.map(|row| {
|
.map(|row| {
|
||||||
|
@ -156,7 +156,7 @@ fn parse_map(input: Pairs<'_, Rule>) -> Map<Terrain> {
|
||||||
.collect()
|
.collect()
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
Map::from_2d_vec(map_vec)
|
World::from_2d_vec(map_vec)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse_directions(input: Pairs<'_, Rule>) -> Vec<Direction> {
|
fn parse_directions(input: Pairs<'_, Rule>) -> Vec<Direction> {
|
||||||
|
|
|
@ -3,8 +3,8 @@ use itertools::Itertools;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
pub fn day16() {
|
pub fn day16() {
|
||||||
let input: Map<char> = Map::from_string(&crate::input(16));
|
let input: World<char> = World::from_string(&crate::input(16));
|
||||||
let maze: Map<bool> = input.map(|&c| c == '.' || c == 'S' || c == 'E');
|
let maze: World<bool> = input.map(|&c| c == '.' || c == 'S' || c == 'E');
|
||||||
|
|
||||||
let start: Transform = input
|
let start: Transform = input
|
||||||
.enumerate()
|
.enumerate()
|
||||||
|
@ -117,11 +117,11 @@ struct Transform<'a> {
|
||||||
x: usize,
|
x: usize,
|
||||||
y: usize,
|
y: usize,
|
||||||
direction: Direction,
|
direction: Direction,
|
||||||
map: &'a Map<bool>,
|
map: &'a World<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Transform<'a> {
|
impl<'a> Transform<'a> {
|
||||||
fn new(x: usize, y: usize, direction: Direction, map: &'a Map<bool>) -> Self {
|
fn new(x: usize, y: usize, direction: Direction, map: &'a World<bool>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
|
|
|
@ -48,16 +48,16 @@ impl Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Map<T>
|
pub struct World<T>
|
||||||
where
|
where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
pub map: Vec<Vec<T>>,
|
pub map: Vec<Vec<T>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Map<char> {
|
impl World<char> {
|
||||||
pub fn from_string(string: &str) -> Self {
|
pub fn from_string(string: &str) -> Self {
|
||||||
Map::from_2d_vec(
|
World::from_2d_vec(
|
||||||
string
|
string
|
||||||
.lines()
|
.lines()
|
||||||
.filter(|line| !line.is_empty())
|
.filter(|line| !line.is_empty())
|
||||||
|
@ -67,7 +67,7 @@ impl Map<char> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Map<T>
|
impl<T> World<T>
|
||||||
where
|
where
|
||||||
T: Copy,
|
T: Copy,
|
||||||
{
|
{
|
||||||
|
@ -153,13 +153,13 @@ where
|
||||||
self.coordinates().map(|(x, y)| (x, y, &self.map[y][x]))
|
self.coordinates().map(|(x, y)| (x, y, &self.map[y][x]))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn map<U: Copy, F: Fn(&T) -> U>(&self, f: F) -> Map<U> {
|
pub fn map<U: Copy, F: Fn(&T) -> U>(&self, f: F) -> World<U> {
|
||||||
let v = self.map.iter().map(|row| row.iter().map(|e| f(e)).collect()).collect();
|
let v = self.map.iter().map(|row| row.iter().map(|e| f(e)).collect()).collect();
|
||||||
Map::<U>::from_2d_vec(v)
|
World::<U>::from_2d_vec(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Display for Map<T>
|
impl<T> Display for World<T>
|
||||||
where
|
where
|
||||||
T: Copy + Display,
|
T: Copy + Display,
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue