Skip to content

Commit

Permalink
Bring back svg visualizations
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Nov 11, 2024
1 parent 45d4bf8 commit 4b4ce2a
Show file tree
Hide file tree
Showing 17 changed files with 559 additions and 237 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ check-simd:
cargo +nightly test --features simd

check-site:
cd crates/wasm && npx prettier --write . && npx eslint . --ext .js && npx prettier --check .
cd crates/wasm && npx prettier --write . && npx eslint . && npx prettier --check .

site-compute-wasm:
cd crates/wasm && \
Expand All @@ -51,7 +51,7 @@ site-compute-wasm:
site-renderer-wasm:
cd crates/wasm && \
RUSTFLAGS="-C target-feature=$(WASM_TARGET_FEATURES)" \
cargo +nightly build $(WASM_BUILD_PROFILE) --target wasm32-unknown-unknown --features visualization && \
cargo build $(WASM_BUILD_PROFILE) --target wasm32-unknown-unknown --features visualization && \
$(WASM_BINDGEN) --out-dir site/show/generated ../../target/wasm32-unknown-unknown/$(WASM_DIR)/advent_of_code_wasm.wasm && \
cd site/show/generated && \
$(WASM_OPT) -o advent_of_code_wasm_bg.wasm advent_of_code_wasm_bg.wasm
Expand Down
3 changes: 2 additions & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ name = "advent_of_code"
count-allocations = ["allocation-counter"]
debug-output = []
simd = []
visualization = []
visualization = ["svgplot"]
webgpu-compute = ["bytemuck", "pollster", "wgpu"]

[dependencies]
allocation-counter = { version = "0", optional = true }
bytemuck = { version = "1", optional = true }
pollster = { version = "0", optional = true }
svgplot = { version = "2022.0.66", path = "../svgplot", optional = true }
wgpu = { version = "0", optional = true }

[dev-dependencies]
Expand Down
8 changes: 5 additions & 3 deletions crates/core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

use std::cell::RefCell;

pub type ResultType = String;

#[derive(Copy, Clone)]
pub enum Part {
One,
Expand All @@ -13,7 +15,7 @@ pub struct Input<'a> {
pub part: Part,
pub text: &'a str,
#[cfg(feature = "visualization")]
pub visualization: RefCell<crate::visualization::Visualization>,
pub visualization: RefCell<String>,
}

#[allow(single_use_lifetimes)]
Expand Down Expand Up @@ -42,7 +44,7 @@ impl<'a> Input<'a> {
part: Part::One,
text,
#[cfg(feature = "visualization")]
visualization: RefCell::new(crate::visualization::Visualization::default()),
visualization: RefCell::new("".to_string()),
}
}

Expand All @@ -53,7 +55,7 @@ impl<'a> Input<'a> {
part: Part::Two,
text,
#[cfg(feature = "visualization")]
visualization: RefCell::new(crate::visualization::Visualization::default()),
visualization: RefCell::new("".to_string()),
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ mod common;
#[cfg_attr(test, macro_use)]
mod input;
mod mod_exp;
#[cfg(feature = "visualization")]
mod visualization;
mod year2015;
mod year2016;
mod year2017;
Expand All @@ -42,7 +40,7 @@ mod year2022;
mod year2023;

#[cfg(feature = "visualization")]
pub type ResultType = visualization::Visualization;
pub type ResultType = String;

#[cfg(not(feature = "visualization"))]
pub type ResultType = String;
Expand Down Expand Up @@ -89,7 +87,7 @@ pub fn solve(year: u16, day: u8, part: u8, input: &str) -> Result<ResultType, St
}

#[cfg(feature = "visualization")]
let visualization = std::cell::RefCell::new(visualization::Visualization::default());
let visualization = std::cell::RefCell::new("".to_string());

let input = Input {
part: if part == 1 { Part::One } else { Part::Two },
Expand Down
3 changes: 0 additions & 3 deletions crates/core/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ fn main() -> Result<(), String> {
match solve_raw(year, day, part, input.as_ref()) {
Ok(result) => {
if repeat == 1 {
#[cfg(feature = "visualization")]
println!("{result:?}");
#[cfg(not(feature = "visualization"))]
println!("{result}");
}
}
Expand Down
143 changes: 0 additions & 143 deletions crates/core/src/visualization.rs

This file was deleted.

4 changes: 4 additions & 0 deletions crates/core/src/year2022.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ pub mod day20;
pub mod day21;
pub mod day22;
pub mod day23;
#[cfg(feature = "visualization")]
pub mod day23_renderer;
#[cfg(feature = "simd")]
pub mod day23_simd;
#[cfg(feature = "webgpu-compute")]
pub mod day23_webgpu;
pub mod day24;
#[cfg(feature = "visualization")]
pub mod day24_renderer;
pub mod day25;
39 changes: 11 additions & 28 deletions crates/core/src/year2022/day12.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::VecDeque;

#[cfg(feature = "visualization")]
use crate::visualization::{Visualization, VisualizationEvent, VisualizationEventWithTime};
use svgplot::{Coordinate, SvgColor, SvgImage, SvgPath, SvgScript, SvgShape, SvgStrokeLinecap};

use crate::input::Input;

Expand Down Expand Up @@ -85,24 +85,14 @@ pub fn solve(input: &Input) -> Result<u32, String> {
let (start_pos, destination_pos, mut graph) = Graph::parse(input.text)?;

#[cfg(feature = "visualization")]
let mut visualization = Visualization::default();
//let mut svg = SvgImage::new().view_box((0, 0, graph.width as i64, graph.height as i64));
let mut svg = SvgImage::new().view_box((0, 0, graph.width as i64, graph.height as i64));
#[cfg(feature = "visualization")]
let mut current_render_step = 0;
#[cfg(feature = "visualization")]
visualization.add_event(VisualizationEventWithTime {
key_frame: 0,
event: VisualizationEvent::OrthographicCamera {
left: 0.,
right: graph.width as f32,
top: 0.,
bottom: graph.height as f32,
},
});
//#[cfg(feature = "visualization")]
//let mut path_render_script = String::from("const pathsPerStep = ['");

/*
let mut circles_render_script = String::from("const circlesPerStep = ['");
#[cfg(feature = "visualization")]
let mut path_render_script = String::from("const pathsPerStep = ['");

#[cfg(feature = "visualization")]
{
for draw_height in 0..26 {
Expand Down Expand Up @@ -157,7 +147,6 @@ pub fn solve(input: &Input) -> Result<u32, String> {
)),
);
}
*/

let mut to_visit = VecDeque::with_capacity(64);
graph.mark_visited(destination_pos.0, destination_pos.1);
Expand All @@ -173,7 +162,6 @@ pub fn solve(input: &Input) -> Result<u32, String> {
graph.height_at(new_pos.0, new_pos.1) == 0
};

/*
#[cfg(feature = "visualization")]
{
if new_cost != current_render_step {
Expand All @@ -197,10 +185,8 @@ pub fn solve(input: &Input) -> Result<u32, String> {
.data_string(),
);
}
*/

if at_goal {
/*
#[cfg(feature = "visualization")]
{
let visited_path_id = svg.add_with_id(
Expand All @@ -221,15 +207,12 @@ pub fn solve(input: &Input) -> Result<u32, String> {
svg.add(SvgScript::new(format!(
"{circles_render_script}{path_render_script}"
)));
input
.visualization
.replace(crate::input::Visualization::Svg(
svg.data_attribute("steps".to_string(), format!("{new_cost}"))
.data_attribute("step-duration".to_string(), format!("{}", 100))
.to_svg_string(),
));
input.visualization.replace(
svg.data_attribute("steps".to_string(), format!("{new_cost}"))
.data_attribute("step-duration".to_string(), format!("{}", 100))
.to_svg_string(),
);
}
*/
return Ok(new_cost);
}
to_visit.push_back((new_cost, new_pos));
Expand Down
11 changes: 6 additions & 5 deletions crates/core/src/year2022/day23.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#[cfg(feature = "visualization")]
#[cfg(not(feature = "simd"))]
#[cfg(not(feature = "webgpu-compute"))]
use crate::visualization::Visualization;

#[cfg(not(feature = "simd"))]
#[cfg(not(feature = "webgpu-compute"))]
use crate::input::Input;

#[cfg(not(feature = "simd"))]
#[cfg(not(feature = "webgpu-compute"))]
#[cfg(not(feature = "visualization"))]
pub fn solve(input: &Input) -> Result<usize, String> {
use crate::common::map_windows::MapWindowsIterator;
use crate::common::u256::U256;
Expand Down Expand Up @@ -212,6 +208,7 @@ pub fn solve(input: &Input) -> Result<usize, String> {
self.bit_rows.iter().map(|x| x.count_ones() as usize).sum()
}
}

let mut grid = ElfGrid::parse(input.text)?;

if input.is_part_one() {
Expand All @@ -222,12 +219,16 @@ pub fn solve(input: &Input) -> Result<usize, String> {
.ok_or_else(|| "No solution found in 10,000 rounds".to_string())
}
}

#[cfg(feature = "simd")]
pub use super::day23_simd::solve;

#[cfg(feature = "webgpu-compute")]
pub use super::day23_webgpu::solve;

#[cfg(feature = "visualization")]
pub use super::day23_renderer::solve;

#[test]
pub fn tests() {
use crate::input::Input;
Expand Down
Loading

1 comment on commit 4b4ce2a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@                     Benchmark Difference                     @@
#      Name   Old (instructions)   New (instructions)   Change (%)
   2023_1_1            1,484,899            1,484,935            0
   2023_1_2            1,457,481            1,457,517            0
   2023_2_1              565,161              565,197            0
   2023_2_2              538,286              538,322            0
   2023_3_1              831,295              831,331            0
   2023_3_2              627,076              627,112            0
   2023_4_1              911,480              911,516            0
   2023_4_2              922,696              922,732            0
   2023_5_1              638,287              638,323            0
   2023_5_2              773,128              773,164            0
   2023_6_1                3,681                3,681            0
   2023_6_2                2,650                2,650            0
   2023_7_1              661,071              661,071            0
   2023_7_2              657,242              657,242            0
   2023_8_1            1,174,656            1,174,404            0
   2023_8_2            2,580,220            2,580,100            0
   2023_9_1              799,584              799,584            0
   2023_9_2              811,311              811,311            0
  2023_10_1            2,950,343            2,950,343            0
  2023_10_2            3,513,748            3,513,748            0
  2023_11_1            1,741,772            1,741,772            0
  2023_11_2            1,741,796            1,741,796            0
  2023_12_1            4,374,265            4,374,265            0
  2023_12_2           68,540,590           68,540,590            0
  2023_13_1              583,257              583,257            0
  2023_13_2              584,313              584,313            0
  2023_14_1              839,811              839,811            0
  2023_14_2          289,510,489          289,510,489            0
  2023_15_1              807,770              807,770            0
  2023_15_2            1,087,799            1,087,799            0
  2023_16_1            1,074,620            1,074,620            0
  2023_16_2          220,133,110          220,133,110            0
  2023_17_1          216,844,218          216,844,218            0
  2023_17_2          666,065,701          666,065,701            0
  2023_18_1              368,072              368,072            0
  2023_18_2              438,362              438,362            0
  2023_19_1            3,006,203            3,006,227            0
  2023_19_2            1,918,527            1,918,551            0
  2023_20_1            4,637,904            4,637,892            0
  2023_20_2           18,235,316           18,235,304            0
  2023_21_1            3,163,573            3,137,774            0
  2023_21_2            4,586,040            4,545,446            0
  2023_22_1           10,749,685           10,749,685            0
  2023_22_2          291,836,472          291,836,472            0
  2023_23_1            1,693,574            1,693,599            0
  2023_23_2        1,129,554,234        1,129,554,259            0
  2023_24_1            3,666,384            3,666,368            0
  2023_24_2              982,488              982,472            0
  2023_25_1            2,376,206            2,376,206            0
Benchmark Instructions (count) Instructions (%)
2023_23_2 1,129,554,259 38.0
2023_17_2 666,065,701 22.4
2023_22_2 291,836,472 9.8
2023_14_2 289,510,489 9.7
2023_16_2 220,133,110 7.4
2023_17_1 216,844,218 7.3
2023_12_2 68,540,590 2.3
2023_20_2 18,235,304 0.6
2023_22_1 10,749,685 0.4
2023_20_1 4,637,892 0.2
2023_21_2 4,545,446 0.2
2023_12_1 4,374,265 0.1
2023_24_1 3,666,368 0.1
2023_10_2 3,513,748 0.1
2023_21_1 3,137,774 0.1
2023_19_1 3,006,227 0.1
2023_10_1 2,950,343 0.1
2023_8_2 2,580,100 0.1
2023_25_1 2,376,206 0.1
2023_19_2 1,918,551 0.1
2023_11_2 1,741,796 0.1
2023_11_1 1,741,772 0.1
2023_23_1 1,693,599 0.1
2023_1_1 1,484,935 0.0
2023_1_2 1,457,517 0.0
2023_8_1 1,174,404 0.0
2023_15_2 1,087,799 0.0
2023_16_1 1,074,620 0.0
2023_24_2 982,472 0.0
2023_4_2 922,732 0.0
2023_4_1 911,516 0.0
2023_14_1 839,811 0.0
2023_3_1 831,331 0.0
2023_9_2 811,311 0.0
2023_15_1 807,770 0.0
2023_9_1 799,584 0.0
2023_5_2 773,164 0.0
2023_7_1 661,071 0.0
2023_7_2 657,242 0.0
2023_5_1 638,323 0.0
2023_3_2 627,112 0.0
2023_13_2 584,313 0.0
2023_13_1 583,257 0.0
2023_2_1 565,197 0.0
2023_2_2 538,322 0.0
2023_18_2 438,362 0.0
2023_18_1 368,072 0.0
2023_6_1 3,681 0.0
2023_6_2 2,650 0.0

Please sign in to comment.