Skip to content

Commit 25fad16

Browse files
2024: Visualize day 15
1 parent 4309488 commit 25fad16

File tree

4 files changed

+395
-2
lines changed

4 files changed

+395
-2
lines changed

2024/day15/Cargo.lock

Lines changed: 293 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

2024/day15/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22
name = "day15"
33
version = "0.1.0"
44
edition = "2021"
5+
6+
[dependencies]
7+
crossterm = "0.28.1"

2024/day15/src/main.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::fs;
1+
use screen::Screen;
2+
use std::{env, fs};
3+
4+
mod screen;
25

36
fn is_movable_vertical(grid: &[u8], w: usize, b: (usize, usize), y: usize, dy: isize) -> bool {
47
let ny = y.checked_add_signed(dy).unwrap();
@@ -92,6 +95,7 @@ fn run_instructions(
9295
mut grid: Vec<u8>,
9396
w: usize,
9497
h: usize,
98+
screen: &mut Option<Screen>,
9599
) -> usize {
96100
for instr in instructions {
97101
match instr {
@@ -136,6 +140,12 @@ fn run_instructions(
136140

137141
_ => panic!("Unknown instruction: {}", instr),
138142
}
143+
144+
if let Some(ref mut screen) = screen {
145+
grid[pos.1 * w + pos.0] = b'@';
146+
screen.update(&grid);
147+
grid[pos.1 * w + pos.0] = b'.';
148+
}
139149
}
140150

141151
let mut total = 0;
@@ -152,6 +162,9 @@ fn run_instructions(
152162
}
153163

154164
fn main() {
165+
// should the grid be visualized on the terminal?
166+
let visualize = env::var("AOC_DAY15_VISUALIZE").is_ok();
167+
155168
for part1 in [true, false] {
156169
let input = fs::read_to_string("input.txt").expect("Could not read file");
157170
let (grid, instructions) = input.split_once("\n\n").unwrap();
@@ -197,6 +210,12 @@ fn main() {
197210
grid = wider_grid;
198211
}
199212

213+
let mut screen = if visualize {
214+
Some(Screen::new(width, height))
215+
} else {
216+
None
217+
};
218+
200219
// find robot
201220
let mut pos = (0, 0);
202221
'outer: for y in 0..height {
@@ -209,7 +228,12 @@ fn main() {
209228
}
210229
grid[pos.1 * width + pos.0] = b'.';
211230

212-
let total = run_instructions(pos, instructions, grid, width, height);
231+
let total = run_instructions(pos, instructions, grid, width, height, &mut screen);
232+
233+
if let Some(mut screen) = screen {
234+
screen.finish();
235+
}
236+
213237
println!("{}", total);
214238
}
215239
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy