diff --git a/679_24_game/Cargo.lock b/679_24_game/Cargo.lock new file mode 100644 index 0000000..9955c4c --- /dev/null +++ b/679_24_game/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "twenty_four_game" +version = "0.1.0" diff --git a/679_24_game/Cargo.toml b/679_24_game/Cargo.toml new file mode 100644 index 0000000..7822df8 --- /dev/null +++ b/679_24_game/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "twenty_four_game" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/679_24_game/src/main.rs b/679_24_game/src/main.rs new file mode 100644 index 0000000..2336fd6 --- /dev/null +++ b/679_24_game/src/main.rs @@ -0,0 +1,65 @@ +fn main() { + println!("{}", Solution::judge_point24(vec![1, 5, 9, 1])); +} + +struct Solution; + +impl Solution { + pub fn judge_point24(cards: Vec) -> bool { + let mut stack: Vec> = vec![cards.iter().map(|&v| v as f64).collect()]; + + while let Some(cards) = stack.pop() { + println!("{:?}", cards); + + if cards.len() == 1 { + if (cards.first().unwrap() - 24f64).abs() < 1e-6 { + return true; + } else { + continue; + } + } + + for i in 0..(cards.len() - 1) { + for j in (i + 1)..cards.len() { + let a = cards[i]; + let b = cards[j]; + + let mut left_cards = cards.clone(); + left_cards.remove(j); + left_cards.remove(i); + println!("{i} {j} {:?}", left_cards); + + let mut new_cards = left_cards.clone(); + new_cards.push(a + b); + stack.push(new_cards); + + let mut new_cards = left_cards.clone(); + new_cards.push(a * b); + stack.push(new_cards); + + let mut new_cards = left_cards.clone(); + new_cards.push(a - b); + stack.push(new_cards); + + let mut new_cards = left_cards.clone(); + new_cards.push(b - a); + stack.push(new_cards); + + if b.abs() > 1e-6 { + let mut new_cards = left_cards.clone(); + new_cards.push(a / b); + stack.push(new_cards); + } + + if a.abs() > 1e-6 { + let mut new_cards = left_cards.clone(); + new_cards.push(b / a); + stack.push(new_cards); + } + } + } + } + + false + } +}