-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b27f5e7
commit b93ddb3
Showing
7 changed files
with
175 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
//! # Mine Cart Madness | ||
//! | ||
//! Simulates the mine carts for both parts. When checking the grid we only care about `\`, `/` | ||
//! and `+` characters, all other characters can be ignored. Carts are sorted by `y` and then by | ||
//! `x` before each tick as the movement order is important to resolve collisions or near misses | ||
//! correctly. | ||
use crate::util::grid::*; | ||
use crate::util::point::*; | ||
|
||
pub struct Input { | ||
grid: Grid<u8>, | ||
carts: Vec<Cart>, | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct Cart { | ||
position: Point, | ||
direction: Point, | ||
turns: u32, | ||
active: bool, | ||
} | ||
|
||
impl Cart { | ||
fn new(position: Point, direction: Point) -> Cart { | ||
Cart { position, direction, turns: 0, active: true } | ||
} | ||
|
||
fn tick(&mut self, grid: &Grid<u8>) { | ||
self.position += self.direction; | ||
|
||
match grid[self.position] { | ||
b'\\' => { | ||
self.direction = match self.direction { | ||
UP => LEFT, | ||
DOWN => RIGHT, | ||
LEFT => UP, | ||
RIGHT => DOWN, | ||
_ => unreachable!(), | ||
} | ||
} | ||
b'/' => { | ||
self.direction = match self.direction { | ||
UP => RIGHT, | ||
DOWN => LEFT, | ||
LEFT => DOWN, | ||
RIGHT => UP, | ||
_ => unreachable!(), | ||
} | ||
} | ||
b'+' => { | ||
self.direction = match self.turns { | ||
0 => self.direction.counter_clockwise(), | ||
1 => self.direction, | ||
2 => self.direction.clockwise(), | ||
_ => unreachable!(), | ||
}; | ||
self.turns = (self.turns + 1) % 3; | ||
} | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
pub fn parse(input: &str) -> Input { | ||
let grid = Grid::parse(input); | ||
let mut carts = Vec::new(); | ||
|
||
for (i, b) in grid.bytes.iter().enumerate() { | ||
let result = match b { | ||
b'^' => Some(UP), | ||
b'v' => Some(DOWN), | ||
b'<' => Some(LEFT), | ||
b'>' => Some(RIGHT), | ||
_ => None, | ||
}; | ||
|
||
if let Some(direction) = result { | ||
let x = i as i32 % grid.width; | ||
let y = i as i32 / grid.width; | ||
carts.push(Cart::new(Point::new(x, y), direction)); | ||
} | ||
} | ||
|
||
Input { grid, carts } | ||
} | ||
|
||
pub fn part1(input: &Input) -> String { | ||
let mut carts = input.carts.clone(); | ||
let mut occupied = input.grid.default_copy(); | ||
|
||
loop { | ||
// Turn order is important. | ||
carts.sort_unstable_by_key(|c| input.grid.width * c.position.y + c.position.x); | ||
|
||
for cart in &mut carts { | ||
// Follow tracks to next position. | ||
occupied[cart.position] = false; | ||
cart.tick(&input.grid); | ||
let next = cart.position; | ||
|
||
if occupied[next] { | ||
return format!("{},{}", next.x, next.y); | ||
} | ||
|
||
occupied[next] = true; | ||
} | ||
} | ||
} | ||
|
||
pub fn part2(input: &Input) -> String { | ||
let mut carts = input.carts.clone(); | ||
let mut occupied = input.grid.default_copy(); | ||
|
||
while carts.len() > 1 { | ||
// Turn order is important. | ||
carts.sort_unstable_by_key(|c| input.grid.width * c.position.y + c.position.x); | ||
|
||
for i in 0..carts.len() { | ||
// Crashed carts may not have been removed yet. | ||
if carts[i].active { | ||
// Follow tracks to next position. | ||
occupied[carts[i].position] = false; | ||
carts[i].tick(&input.grid); | ||
let next = carts[i].position; | ||
|
||
if occupied[next] { | ||
// Mark both carts as crashed. | ||
carts.iter_mut().filter(|c| c.position == next).for_each(|c| c.active = false); | ||
occupied[next] = false; | ||
} else { | ||
occupied[next] = true; | ||
} | ||
} | ||
} | ||
|
||
// Removed crashed carts to speed up future ticks. | ||
carts.retain(|c| c.active); | ||
} | ||
|
||
let last = carts[0].position; | ||
format!("{},{}", last.x, last.y) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -125,6 +125,7 @@ mod year2018 { | |
mod day10_test; | ||
mod day11_test; | ||
mod day12_test; | ||
mod day13_test; | ||
} | ||
|
||
mod year2019 { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use aoc::year2018::day13::*; | ||
|
||
const FIRST_EXAMPLE: &str = r"/->-\ . | ||
| | /----\ . | ||
| /-+--+-\ | . | ||
| | | | v | . | ||
\-+-/ \-+--/ . | ||
\------/ ."; | ||
|
||
const SECOND_EXAMPLE: &str = r"/>-<\ . | ||
| | . | ||
| /<+-\ . | ||
| | | v . | ||
\>+</ | . | ||
| ^ . | ||
\<->/ ."; | ||
|
||
#[test] | ||
fn part1_test() { | ||
let input = parse(FIRST_EXAMPLE); | ||
assert_eq!(part1(&input), "7,3"); | ||
} | ||
|
||
#[test] | ||
fn part2_test() { | ||
let input = parse(SECOND_EXAMPLE); | ||
assert_eq!(part2(&input), "6,4"); | ||
} |