reference from [https://cs.carleton.edu/placement/problem.pdf] (ty valkyrie_pilot)
every_variant for the enum reflection
Last active
June 21, 2025 13:19
-
-
Save timelessnesses/0204109329ca902f2e066115ba99cc5e to your computer and use it in GitHub Desktop.
stupid problem
This file contains hidden or 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
| [package] | |
| name = "this-aint-connect-four" | |
| version = "0.1.0" | |
| edition = "2024" | |
| [dependencies] | |
| every_variant = "0.4.5" |
This file contains hidden or 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
| use every_variant::EveryVariant; | |
| use std::str::FromStr; | |
| #[derive(Debug, Clone)] | |
| struct Grid { | |
| width: usize, | |
| data: Box<[i128]>, | |
| } | |
| impl Grid { | |
| #[inline(always)] | |
| fn get(&self, x: isize, y: isize) -> Option<i128> { | |
| if x < 0 || y < 0 { | |
| return None; | |
| } | |
| let (x, y) = (x as usize, y as usize); | |
| self.data.get(y * self.width + x).copied() | |
| } | |
| #[inline(always)] | |
| fn dims(&self) -> (usize, usize) { | |
| (self.width, self.data.len() / self.width) | |
| } | |
| } | |
| impl FromStr for Grid { | |
| type Err = String; | |
| fn from_str(s: &str) -> Result<Self, Self::Err> { | |
| let mut grid = Vec::new(); | |
| let mut width = 0; | |
| for line in s.lines().filter(|l| !l.trim().is_empty()) { | |
| let mut row = line | |
| .split_whitespace() | |
| .map(|v| v.parse::<i128>().map_err(|e| e.to_string())) | |
| .collect::<Result<Vec<_>, _>>()?; | |
| width = width.max(row.len()); | |
| grid.append(&mut row); | |
| } | |
| Ok(Self { | |
| width, | |
| data: grid.into(), | |
| }) | |
| } | |
| } | |
| #[derive(Debug, Clone, Copy, EveryVariant)] | |
| enum Scan { | |
| Horizontal, | |
| Vertical, | |
| DownDiagonal, | |
| UpDiagonal, | |
| } | |
| impl Scan { | |
| #[inline(always)] | |
| fn offsets(&self) -> [(isize, isize); 4] { | |
| match self { | |
| Scan::Horizontal => [(0, 0), (1, 0), (2, 0), (3, 0)], | |
| Scan::Vertical => [(0, 0), (0, 1), (0, 2), (0, 3)], | |
| Scan::DownDiagonal => [(0, 0), (1, 1), (2, 2), (3, 3)], | |
| Scan::UpDiagonal => [(0, 0), (1, -1), (2, -2), (3, -3)], | |
| } | |
| } | |
| } | |
| fn main() { | |
| let grid_path = std::env::args() | |
| .nth(1) | |
| .expect("Please provide a path to a grid"); | |
| let text = std::fs::read_to_string(grid_path).expect("Could not read grid"); | |
| let grid = Grid::from_str(&text).expect("Could not parse grid"); | |
| let max_product = max_prod(&grid); | |
| println!("Max product: {}", max_product); | |
| } | |
| fn max_prod(grid: &Grid) -> i128 { | |
| let (width, height) = grid.dims(); | |
| let mut max_product = 0; | |
| for scan in Scan::every_variant() { | |
| for y in 0..height { | |
| for x in 0..width { | |
| let mut vals = Vec::with_capacity(4); | |
| for &(dx, dy) in &scan.offsets() { | |
| let xi = x as isize + dx; | |
| let yi = y as isize + dy; | |
| match grid.get(xi, yi) { | |
| Some(v) => vals.push(v), | |
| None => break, | |
| } | |
| } | |
| if vals.len() == 4 { | |
| let prod = vals.iter().product(); | |
| max_product = max_product.max(prod); | |
| } | |
| } | |
| } | |
| } | |
| max_product | |
| } | |
| #[cfg(test)] | |
| mod test { | |
| use super::*; | |
| #[test] | |
| fn test_grid() { | |
| let grid = Grid::from_str( | |
| r" | |
| 674 20 305 -921 912 779 25 525 331 181 660 -123 -620 -88 269 | |
| 650 640 613 575 617 -883 -202 159 -201 874 184 -893 -863 -655 550 | |
| 30 888 649 959 -339 668 471 519 538 -45 905 -427 350 607 -275 | |
| -140 362 -397 230 -638 -658 -499 -74 710 840 -856 -553 726 14 547 | |
| 753 288 -318 -432 -302 584 94 47 752 -901 -384 -14 -209 -435 443 | |
| -662 -943 59 -601 -650 934 -509 757 -323 -679 57 -780 -924 -853 207 | |
| 823 -582 21 168 729 1 839 -873 334 151 252 919 733 -481 -565 | |
| -385 562 -661 298 -646 -834 592 -753 277 97 -179 64 993 -168 421 | |
| -540 -803 917 -781 989 -789 -158 243 775 -394 -130 -19 655 318 420 | |
| 44 -18 112 -533 -886 699 -884 -607 -904 -17 971 -224 -956 134 110 | |
| ", | |
| ) | |
| .unwrap(); | |
| let res = max_prod(&grid); | |
| assert_eq!(res, 568_764_139_559) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment