Skip to content

Commit 12d2d26

Browse files
committed
Day 16 puzzle 2
1 parent 5b07690 commit 12d2d26

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
| 12 | [src/solutions/day12.rs](src/solutions/day12.rs) | ⭐️⭐️ |
2424
| 13 | [src/solutions/day13.rs](src/solutions/day13.rs) | ⭐️⭐️ |
2525
| 14 | [src/solutions/day14.rs](src/solutions/day14.rs) | ⭐️⭐️ |
26-
<!-- | 15 | [src/solutions/day15.rs](src/solutions/day15.rs) | ⭐️⭐️ | -->
27-
<!-- | 16 | [src/solutions/day16.rs](src/solutions/day16.rs) | ⭐️⭐️ | -->
26+
| 15 | [src/solutions/day15.rs](src/solutions/day15.rs) | ⭐️⭐️ |
27+
| 16 | [src/solutions/day16.rs](src/solutions/day16.rs) | ⭐️⭐️ |
2828
<!-- | 17 | [src/solutions/day17.rs](src/solutions/day17.rs) | ⭐️⭐️ | -->
2929
<!-- | 18 | [src/solutions/day18.rs](src/solutions/day18.rs) | ⭐️⭐️ | -->
3030
<!-- | 19 | [src/solutions/day19.rs](src/solutions/day19.rs) | ⭐️⭐️ | -->

src/solutions/day16.rs

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::data::load;
2-
use std::collections::{HashMap, HashSet};
2+
use std::{
3+
cmp,
4+
collections::{HashMap, HashSet},
5+
};
36
use thiserror::Error;
47

58
#[derive(Error, Debug, PartialEq, Eq)]
@@ -70,25 +73,30 @@ impl Beam {
7073
fn new(loc: Coord, dir: Direction) -> Self {
7174
Beam { loc, dir }
7275
}
76+
}
7377

78+
impl Beam {
7479
fn move_up(&self) -> Self {
7580
let mut b = *self;
7681
b.dir = Direction::Up;
7782
b.loc.r -= 1;
7883
b
7984
}
85+
8086
fn move_down(&self) -> Self {
8187
let mut b = *self;
8288
b.dir = Direction::Down;
8389
b.loc.r += 1;
8490
b
8591
}
92+
8693
fn move_left(&self) -> Self {
8794
let mut b = *self;
8895
b.dir = Direction::Left;
8996
b.loc.c -= 1;
9097
b
9198
}
99+
92100
fn move_right(&self) -> Self {
93101
let mut b = *self;
94102
b.dir = Direction::Right;
@@ -143,16 +151,43 @@ fn move_beam(beam: &Beam, grid: &HashMap<Coord, CaveObject>, beam_tracker: &mut
143151
}
144152
}
145153

146-
pub fn puzzle_1(input: &str) -> Result<usize, PuzzleErr> {
147-
let grid = parse_input(input)?;
154+
fn count_energized_tiles(starting_beam: Beam, grid: &HashMap<Coord, CaveObject>) -> usize {
148155
let mut energized_coords = HashSet::new();
149-
let beam = Beam::new(Coord { r: 0, c: 0 }, Direction::Right);
150-
move_beam(&beam, &grid, &mut energized_coords);
151-
Ok(energized_coords
156+
move_beam(&starting_beam, grid, &mut energized_coords);
157+
energized_coords
152158
.iter()
153159
.map(|b| b.loc)
154160
.collect::<HashSet<_>>()
155-
.len())
161+
.len()
162+
}
163+
164+
pub fn puzzle_1(input: &str) -> Result<usize, PuzzleErr> {
165+
let grid = parse_input(input)?;
166+
let beam = Beam::new(Coord { r: 0, c: 0 }, Direction::Right);
167+
Ok(count_energized_tiles(beam, &grid))
168+
}
169+
170+
pub fn puzzle_2(input: &str) -> Result<usize, PuzzleErr> {
171+
let grid = parse_input(input)?;
172+
let mut max = 0;
173+
let height = grid.keys().map(|c| c.r).max().unwrap();
174+
let width = grid.keys().map(|c| c.c).max().unwrap();
175+
176+
for r in 0..=height {
177+
for (c, dir) in [(0, Direction::Right), (width, Direction::Left)] {
178+
let beam = Beam::new(Coord { r, c }, dir);
179+
max = cmp::max(max, count_energized_tiles(beam, &grid));
180+
}
181+
}
182+
183+
for c in 0..=width {
184+
for (r, dir) in [(0, Direction::Down), (height, Direction::Up)] {
185+
let beam = Beam::new(Coord { r, c }, dir);
186+
max = cmp::max(max, count_energized_tiles(beam, &grid));
187+
}
188+
}
189+
190+
Ok(max)
156191
}
157192

158193
pub fn main(data_dir: &str) {
@@ -168,10 +203,10 @@ pub fn main(data_dir: &str) {
168203
assert_eq!(answer_1, Ok(6921));
169204

170205
// Puzzle 2.
171-
// let answer_2 = puzzle_2(&data);
172-
// match answer_2 {
173-
// Ok(x) => println!(" Puzzle 2: {}", x),
174-
// Err(e) => panic!("No solution to puzzle 2: {}", e),
175-
// }
176-
// assert_eq!(answer_2, Ok(30449))
206+
let answer_2 = puzzle_2(&data);
207+
match answer_2 {
208+
Ok(x) => println!(" Puzzle 2: {}", x),
209+
Err(e) => panic!("No solution to puzzle 2: {}", e),
210+
}
211+
assert_eq!(answer_2, Ok(7594))
177212
}

tests/test_day16.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use aoc_2023::solutions::day16::puzzle_1;
1+
use aoc_2023::solutions::day16::{puzzle_1, puzzle_2};
22

33
const EXAMPLE_INPUT_1: &str = r"
44
.|...\....
@@ -18,3 +18,9 @@ fn puzzle_1_example_1() {
1818
let _ = env_logger::try_init();
1919
assert_eq!(puzzle_1(self::EXAMPLE_INPUT_1), Ok(46));
2020
}
21+
22+
#[test]
23+
fn puzzle_2_example_1() {
24+
let _ = env_logger::try_init();
25+
assert_eq!(puzzle_2(self::EXAMPLE_INPUT_1), Ok(51));
26+
}

0 commit comments

Comments
 (0)