-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
8 changed files
with
469 additions
and
166 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
\..\.....-............-......................-...................-............|.-..../..\/.|.......-.......... | ||
...............-....-.-...........\......-............/..\............|........|...........-.........../..\... | ||
-..............|..............-.\......................-.............../.............|..../......-/......-.... | ||
......................-....-.........-.........../-.\..........|./.......-................./....-..../........ | ||
.............................................|........./|.-.....|..................../......../........\...|.. | ||
.....-.........-.....|....-............................/-.............../..-.....-......\.......//\.../.-..... | ||
.........../.|...../........-...............|..|\.........|..........\.....................-.................. | ||
...................|..........-/|/........\.....................................\.../.|.....\..........-...... | ||
........./.............................-......../...........-...........-.....................\........./..... | ||
....-......./-......./.\-......|.....|...-......|............|...../...|...\.........\....|/.........|........ | ||
......./......-......-.....-.|.........../...........|.....................//.....-....-...................... | ||
..-................./../..........-..\.........\..\.............\/........\...../.........|....-|............. | ||
.....\................-...................\...................................................-|.\.........|.. | ||
..\.......-.\-....\................/..........|............../.../.......-............\.............|...|..... | ||
........./..........|....\................-...................|-.........................\.......|............ | ||
-..../..................\.....-........../........./\........./....\-..........\.............................. | ||
-........./........\....|.....\.|......../|................|..........................|...\.../...|........... | ||
.......||...............................\.......\.-.......................|...\.....\.......................\. | ||
............|....................|............./\............................-............-................... | ||
.......-...............\......-......|......................\\..../.....-.....-.|...........-.......-......... | ||
|........../......|..../...-...|...../..-../...............................|\.............../.../............. | ||
..............-...\.................-................-......|...-....\|.\..\.................................. | ||
...........-./......../.......................-|../...../..................|.................................. | ||
-.-............../................./-.........-............||...........-../....-...../..............|..|..... | ||
\-.\/.....................................-.........|.\..........|....../......\..........|.|................. | ||
.............-\..../.....-...../..\................................................-.|..\......|-..\...|...... | ||
..\...................................\............|............|.-...\.../..../....\.\....................... | ||
.........-............/.....-..\..../.......-......./.../.\|.........../........\.......\........\............ | ||
.|.\..........................|....|.........../................../../-......|/../.........\......./...\...... | ||
........./|....|..|.-....................||..|.............................................|.................. | ||
..................|.|............/....|.............\...-....-\.....|....../../............................... | ||
..............|...........\.....-.......\..........................|.........-./\.............|........-...... | ||
...........................|......\..|.......................................................\.|.............. | ||
/..\.........................../........./......\...\...........\.........\./............/......../....../.\.. | ||
.\...-\....-..................-..............-.......|....\.....-...-|.|.......-.........../......\........... | ||
...|../-......../....\.-..........\............./.-........|...................................|.............. | ||
..\...\.................|............././...\|../....\.............-....../........-\.................\......| | ||
...\...........-.............|......../...................\...\..........................\...............-.... | ||
....................\......\................................................//......................././...... | ||
....-.|......|......../..-..|..|.............../.........................\.........-......................./.. | ||
...|............./......./..../..-..............................-......../..-....\...........-.-....|../...... | ||
.........-......................|............................/......\.........................\......|....|... | ||
........../...........\................//....-................-.............\............................../.. | ||
........\......./..........|-............./\............../.....\......-....................................\\ | ||
....\|....../........./-..........................-.../........./|...|-.\./.................|....|-.\......... | ||
-|............................|\.............................................................../.........-.... | ||
................\/....|............-......\/.\.-...|.................................../................./\.\. | ||
.....-....\............|............................../.........../....\...........................\..../-|... | ||
.............................\...................-...\..........|/.........../........\.....\....\..........|. | ||
...................\......./...../............./..\./.....-.|..........|...../-.....--..-..........|.......... | ||
...\..............-.-....-..|............../......................-.........................-..-............|. | ||
....|......./-................-................\...........|......../.................|/..............\....... | ||
........./..............\.....\..........\..|..|-........-..........\./.............\............|............ | ||
........\..................\........-..../.....-.\...../.-....................-................|..........-... | ||
.........................-...|......-...\....|......\.......\.........-....\.|\......\.........\.............. | ||
.........\...................-......................|.....-...............|...../......-.....\.|.......|..-... | ||
../.......-.......................|............|/.\........-...................|............/......|.-........ | ||
........-../..........|................/........-.\...-..|......./.....-..\.............../..........\....\..- | ||
............./../...-...................../.............................\...|.............../........-........ | ||
...................../...................|..........|........................||-..................\../........ | ||
.....|...........-...............-.....\.....\..../................\.....././................................. | ||
...........|.|...........|...........\............\......-......................-.........|................\.. | ||
......../............|................../.-.........../......|.\...............-..........\............\....-. | ||
..../\.................../....................-......................-..........\............./............./. | ||
........../.........-.\.-/-................................................................................... | ||
...../..../..-..................................|...................................|..............|...|...... | ||
..............\/......\...|\......................-................|../..-..................-...-.|..|....\... | ||
.....................|.\-...............................|......-.........\.......-.............-.....-........ | ||
............/........../....|../...................-....-...|./.....-.|.......|..|.........../.............../ | ||
.........../..../.................-..../....\................./...-........................................... | ||
..................................\..............|.........-....../..|........\...-....\.......-..|.........-. | ||
..../.|.....\............./....../........\................./....|.........................-..\-.............. | ||
....|........................|..........|........|.........../....-.\\-............/.....\.......|............ | ||
|.......-.....|.....................|.\...-....\..........-................................................../ | ||
.................................|............././........\........../..\......\/...................\./...|... | ||
.....-.............|..........\.............\.../..........\.......-|.........\.......\......-.../......|..... | ||
.......................\....................|.....|......-....................|...-...../................\.|.. | ||
.|.|..\...../...|............................./......................|................................/....... | ||
........\.....................-...............|...................-.\.....-.......|....../..........\./....... | ||
....//......../.-..../......................\..-...../.........|......................|...|.....|\............ | ||
...\-/..../-....../......................|........../....-...................-.../......|.\...........\/...... | ||
.............\..\.-................|.............|.............\..-......./..-....\.../........../.||\........ | ||
..........\...\./\|...\..................................................|.....-.\.-....\..\.................. | ||
......\...|....-.......-......\......|..............\..................\\...............................|..... | ||
......|....|.................\...|-.....|......./...|......../...../.............|...\.........|............\. | ||
.....|....-.\../-..........\............................../......./...-.......|.......|..............-.\...... | ||
...........................|...\/............................./.../..........\............|................... | ||
......-..........\./..........-.....................-...........................................\......|.-.... | ||
....-.../-.......|......|....../../............/..-......../.\..\|.-/.........-.../........\......\....../.... | ||
|...............\.............|......................|-..............\......................|......../.-....|. | ||
../././................|.....\..\../.\.|...........\.............-.....\.\..............-....\................ | ||
..................../............../...-........./-...-.........|.........../.............\...............-... | ||
.............................\.........\...-.............\......../....../.-..............................-... | ||
................/.|..../........\................./....\.......\........../..-.|...-.........\.............-.. | ||
.|../.|.................................................-.../...............................................|. | ||
..............................................|........../......................................-............. | ||
..............|...-....|......-.....|...-..........-.........-......-.\.........-...........-........|.......\ | ||
.....-......./...-..........|...\/...../.-.................-......\..........\....\...................../..-.. | ||
...../............./..........-...................\......................-......./../.................../|.... | ||
........|.............\...\--..-.../.......................................-............../..\............/.-. | ||
...............|-.............../-.........\.....................\..-.......................-........./\...... | ||
.........\.....\.............../....\.../.|.....................-............................................. | ||
........./...............|.../........../././.../-./-..............//..|.........|...|.....-.......\.........\ | ||
..........|.........\.\..............-..\.\.\..|-..............\.....|........................................ | ||
.....................-....-./../....../.....\-..............-....|......\........\...|................-....... | ||
/..............\.............|............\.\...\|.../.........-........../.....\...../|...................... | ||
.............-....-........................................|.........../....................|......-/......... | ||
....../......-..../.....-..............././.....|...|.../.....|......\........\|...-..-........\\....../\..... | ||
.....\......................................./..|......./\....|..|............./.............................. | ||
../..................\...|....../................\....\.../.........\..|........./......\/......|/............ |
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,181 @@ | ||
use anyhow::Result; | ||
use itertools::Itertools; | ||
use std::{collections::HashSet, fs, path::Path}; | ||
|
||
pub fn part1(input_path: &Path) -> Result<i64> { | ||
let input = fs::read_to_string(input_path)?; | ||
let grid = parse_input(&input); | ||
Ok(count_energized_tiles( | ||
&grid, | ||
QueueItem::new(0, 0, Direction::Right), | ||
)) | ||
} | ||
|
||
pub fn part2(input_path: &Path) -> Result<i64> { | ||
let input = fs::read_to_string(input_path)?; | ||
let grid = parse_input(&input); | ||
let mut configurations = vec![]; | ||
for row_idx in 0..grid.len() { | ||
configurations.push(QueueItem::new(row_idx as isize, 0, Direction::Right)); | ||
configurations.push(QueueItem::new( | ||
row_idx as isize, | ||
grid[0].len() as isize - 1, | ||
Direction::Left, | ||
)); | ||
} | ||
for col_idx in 0..grid[0].len() { | ||
configurations.push(QueueItem::new(0, col_idx as isize, Direction::Down)); | ||
configurations.push(QueueItem::new( | ||
grid.len() as isize - 1, | ||
col_idx as isize, | ||
Direction::Up, | ||
)); | ||
} | ||
Ok(configurations | ||
.into_iter() | ||
.map(|configuration| count_energized_tiles(&grid, configuration)) | ||
.max() | ||
.unwrap()) | ||
} | ||
|
||
fn parse_input(s: &str) -> Vec<Vec<Cell>> { | ||
s.lines() | ||
.map(|row| { | ||
row.chars() | ||
.map(|c| match c { | ||
'.' => Cell::Empty, | ||
'/' => Cell::MirrorLU, | ||
'\\' => Cell::MirrorRU, | ||
'-' => Cell::SplitterH, | ||
'|' => Cell::SplitterV, | ||
_ => panic!(), | ||
}) | ||
.collect() | ||
}) | ||
.collect() | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Hash)] | ||
enum Cell { | ||
Empty, | ||
MirrorLU, | ||
MirrorRU, | ||
SplitterH, | ||
SplitterV, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Hash, Clone)] | ||
enum Direction { | ||
Up, | ||
Down, | ||
Left, | ||
Right, | ||
} | ||
|
||
#[derive(Debug, PartialEq, Eq, Hash)] | ||
struct QueueItem { | ||
row_idx: isize, | ||
col_idx: isize, | ||
direction: Direction, | ||
} | ||
|
||
impl QueueItem { | ||
fn new(row_idx: isize, col_idx: isize, direction: Direction) -> QueueItem { | ||
QueueItem { | ||
row_idx, | ||
col_idx, | ||
direction, | ||
} | ||
} | ||
|
||
fn move_(&self, direction: Direction) -> QueueItem { | ||
match direction { | ||
Direction::Up => QueueItem::new(self.row_idx - 1, self.col_idx, direction), | ||
Direction::Down => QueueItem::new(self.row_idx + 1, self.col_idx, direction), | ||
Direction::Left => QueueItem::new(self.row_idx, self.col_idx - 1, direction), | ||
Direction::Right => QueueItem::new(self.row_idx, self.col_idx + 1, direction), | ||
} | ||
} | ||
} | ||
|
||
fn count_energized_tiles(grid: &Vec<Vec<Cell>>, configuration: QueueItem) -> i64 { | ||
let mut visited = HashSet::new(); | ||
let mut q = vec![configuration]; | ||
while let Some(item) = q.pop() { | ||
if visited.contains(&item) { | ||
continue; | ||
} | ||
if item.row_idx < 0 | ||
|| item.row_idx as usize == grid.len() | ||
|| item.col_idx < 0 | ||
|| item.col_idx as usize == grid[0].len() | ||
{ | ||
continue; | ||
} | ||
let cell = &grid[item.row_idx as usize][item.col_idx as usize]; | ||
let next_queue_items = match cell { | ||
Cell::Empty => { | ||
vec![item.move_(item.direction.clone())] | ||
} | ||
Cell::MirrorLU => match item.direction { | ||
Direction::Up => vec![item.move_(Direction::Right)], | ||
Direction::Down => vec![item.move_(Direction::Left)], | ||
Direction::Left => vec![item.move_(Direction::Down)], | ||
Direction::Right => vec![item.move_(Direction::Up)], | ||
}, | ||
Cell::MirrorRU => match item.direction { | ||
Direction::Up => vec![item.move_(Direction::Left)], | ||
Direction::Down => vec![item.move_(Direction::Right)], | ||
Direction::Left => vec![item.move_(Direction::Up)], | ||
Direction::Right => vec![item.move_(Direction::Down)], | ||
}, | ||
Cell::SplitterH => match item.direction { | ||
Direction::Up | Direction::Down => { | ||
vec![item.move_(Direction::Left), item.move_(Direction::Right)] | ||
} | ||
Direction::Left | Direction::Right => { | ||
vec![item.move_(item.direction.clone())] | ||
} | ||
}, | ||
Cell::SplitterV => match item.direction { | ||
Direction::Up | Direction::Down => { | ||
vec![item.move_(item.direction.clone())] | ||
} | ||
Direction::Left | Direction::Right => { | ||
vec![item.move_(Direction::Up), item.move_(Direction::Down)] | ||
} | ||
}, | ||
}; | ||
q.extend(next_queue_items); | ||
visited.insert(item); | ||
} | ||
visited | ||
.iter() | ||
.map(|item| (item.row_idx as usize, item.col_idx as usize)) | ||
.unique() | ||
.count() as i64 | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use pretty_assertions::assert_eq; | ||
|
||
#[test] | ||
fn test_part1() { | ||
let input_path = Path::new("./src/day16/sample"); | ||
assert_eq!(part1(input_path).unwrap(), 46); | ||
|
||
let input_path = Path::new("./src/day16/input"); | ||
assert_eq!(part1(input_path).unwrap(), 7870); | ||
} | ||
|
||
#[test] | ||
fn test_part2() { | ||
let input_path = Path::new("./src/day16/sample"); | ||
assert_eq!(part2(input_path).unwrap(), 51); | ||
|
||
let input_path = Path::new("./src/day16/input"); | ||
assert_eq!(part2(input_path).unwrap(), 8143); | ||
} | ||
} |
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,10 @@ | ||
.|...\.... | ||
|.-.\..... | ||
.....|-... | ||
........|. | ||
.......... | ||
.........\ | ||
..../.\\.. | ||
.-.-/..|.. | ||
.|....-|.\ | ||
..//.|.... |
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 |
---|---|---|
|
@@ -13,4 +13,5 @@ pub mod day12; | |
pub mod day13; | ||
pub mod day14; | ||
pub mod day15; | ||
pub mod day16; | ||
mod utils; |
Oops, something went wrong.