From 5238435d5a46b3f775bd5a9830ed7726ad5e58cd Mon Sep 17 00:00:00 2001 From: astynax Date: Thu, 4 Jun 2026 08:27:46 +0400 Subject: [PATCH 1/2] fix(84_Super_Star_Trek/rust): non-integer torpedo courses --- 84_Super_Star_Trek/rust/src/commands.rs | 38 ++++++++++++++++++++----- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index c1c4bc8d4..52f8d59cc 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -529,15 +529,39 @@ fn find_torpedo_path(start_sector: Pos, course: f32) -> Vec { let mut last_sector = start_sector; let mut path = Vec::new(); + let mut nx = last_sector.0 as f32; + let mut ny = last_sector.1 as f32; + loop { - let nx = (last_sector.0 as f32 + dx) as i8; - let ny = (last_sector.1 as f32 + dy) as i8; - if nx < 0 || ny < 0 || nx >= 8 || ny >= 8 { + nx += dx; + ny += dy; + if nx < 0.0 || ny < 0.0 || nx >= 8.0 || ny >= 8.0 { break; } - last_sector = Pos(nx as u8, ny as u8); - path.push(last_sector); + let step = Pos(nx as u8, ny as u8); + if step != last_sector { + last_sector = step; + path.push(last_sector); + } } - + path -} \ No newline at end of file +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_find_torpedo_path() { + let path = find_torpedo_path(Pos(0, 0), 7.5); + assert_eq!( + *path.last().unwrap(), + Pos(7, 3), + ); + assert_eq!( + path, + vec!(Pos(1, 0), Pos(2, 1), Pos(3, 1), Pos(4, 2), Pos(5, 2), Pos(6, 3), Pos(7, 3)) + ); + } +} From 4da5ce3ae6581fc972226c1a6ecf6f7ac2e1d77f Mon Sep 17 00:00:00 2001 From: astynax Date: Fri, 5 Jun 2026 10:28:00 +0400 Subject: [PATCH 2/2] round the torpedo coords properly, add a fuzzy test --- 84_Super_Star_Trek/rust/src/commands.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/84_Super_Star_Trek/rust/src/commands.rs b/84_Super_Star_Trek/rust/src/commands.rs index 52f8d59cc..599327cac 100644 --- a/84_Super_Star_Trek/rust/src/commands.rs +++ b/84_Super_Star_Trek/rust/src/commands.rs @@ -538,7 +538,7 @@ fn find_torpedo_path(start_sector: Pos, course: f32) -> Vec { if nx < 0.0 || ny < 0.0 || nx >= 8.0 || ny >= 8.0 { break; } - let step = Pos(nx as u8, ny as u8); + let step = Pos(nx.round() as u8, ny.round() as u8); if step != last_sector { last_sector = step; path.push(last_sector); @@ -554,7 +554,7 @@ mod tests { #[test] fn test_find_torpedo_path() { - let path = find_torpedo_path(Pos(0, 0), 7.5); + let path = find_torpedo_path(Pos(0, 0), 7.45); assert_eq!( *path.last().unwrap(), Pos(7, 3), @@ -563,5 +563,22 @@ mod tests { path, vec!(Pos(1, 0), Pos(2, 1), Pos(3, 1), Pos(4, 2), Pos(5, 2), Pos(6, 3), Pos(7, 3)) ); + + assert_eq!( + find_torpedo_path(Pos(4, 7), 4.5), + vec!(Pos(4, 6), Pos(3, 5), Pos(3, 4), Pos(2, 3), Pos(2, 2), Pos(1, 1), Pos(1, 0)) + ); + + let enterprise = Pos(3, 3); + for y in 0..=7 { + for x in 0..=7 { + let pos = Pos(y, x); + if pos != enterprise { + let course = enterprise.direction(pos); + let path = find_torpedo_path(enterprise, course); + assert!(path.contains(&pos)); + }; + }; + }; } }