diff --git a/src/lib.rs b/src/lib.rs index a5b7e8f5..506a75b2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1494,6 +1494,7 @@ pub mod problem_2023_number_of_pairs_of_strings_with_concatenation_equal_to_targ pub mod problem_2024_maximize_the_confusion_of_an_exam; pub mod problem_2027_minimum_moves_to_convert_string; pub mod problem_2028_find_missing_observations; +pub mod problem_2029_stone_game_ix; pub mod problem_2032_two_out_of_three; pub mod problem_2033_minimum_operations_to_make_a_uni_value_grid; pub mod problem_2034_stock_price_fluctuation; diff --git a/src/problem_2029_stone_game_ix/mathematical.rs b/src/problem_2029_stone_game_ix/mathematical.rs new file mode 100644 index 00000000..7c9f2ab8 --- /dev/null +++ b/src/problem_2029_stone_game_ix/mathematical.rs @@ -0,0 +1,38 @@ +pub struct Solution; + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl Solution { + pub fn stone_game_ix(stones: Vec) -> bool { + let mut counts = [0_u32; 3]; + + for stone in stones { + counts[usize::from(stone as u16 % 3)] += 1; + } + + let [zero, one, two] = counts; + let (x, y) = if two < one { (two, one) } else { (one, two) }; + + if zero % 2 == 0 { + x != 0 + } else { + y - x > 2 + } + } +} + +// ------------------------------------------------------ snip ------------------------------------------------------ // + +impl super::Solution for Solution { + fn stone_game_ix(stones: Vec) -> bool { + Self::stone_game_ix(stones) + } +} + +#[cfg(test)] +mod tests { + #[test] + fn test_solution() { + super::super::tests::run::(); + } +} diff --git a/src/problem_2029_stone_game_ix/mod.rs b/src/problem_2029_stone_game_ix/mod.rs new file mode 100644 index 00000000..3a25f65c --- /dev/null +++ b/src/problem_2029_stone_game_ix/mod.rs @@ -0,0 +1,46 @@ +pub mod mathematical; + +pub trait Solution { + fn stone_game_ix(stones: Vec) -> bool; +} + +#[cfg(test)] +mod tests { + use super::Solution; + + pub fn run() { + let test_cases = [ + (&[2, 1] as &[_], true), + (&[2], false), + (&[5, 1, 2, 4, 3], false), + (&[2, 2, 3], false), + (&[2, 2, 2, 3], true), + ( + &[ + 325, 348, 389, 196, 236, 79, 219, 29, 119, 354, 497, 203, 55, 333, 403, 180, 205, 80, 208, 310, + 416, 335, 392, 409, 485, 329, 379, 277, 403, 370, 194, 428, 491, 488, 152, 316, 268, 364, 69, 61, + 266, 335, 236, 240, 148, 352, 141, 276, 400, 397, 279, 408, 81, 320, 294, 395, 418, 277, 369, 364, + 83, 460, 364, 282, 223, 36, 449, 31, 303, 485, 455, 33, 437, 413, 462, 225, 183, 500, 316, 141, + 164, 60, 406, 254, 132, 407, 105, 343, 70, 363, 19, 500, 211, 199, 155, 477, 424, 101, 347, 402, + 469, 323, 192, 210, 224, 352, 348, 500, 282, 82, 262, 437, 438, 173, 236, 375, 38, 458, 436, 261, + 461, 271, 155, 488, 446, 418, 403, 482, 474, 46, 344, 480, 390, 357, 303, 57, 298, 31, 402, 460, 2, + 212, 285, 243, 110, 359, 81, 147, 308, 190, 341, 417, 99, 277, 42, 109, 51, 49, 487, 443, 143, 150, + 428, 129, 287, 348, 500, 203, 183, 74, 112, 228, 166, 21, 10, 355, 174, 91, 493, 124, 457, 254, + 478, 407, 277, 122, 403, 251, 144, 404, 430, 396, 336, 433, 100, 497, 127, 122, 300, 145, 234, 423, + 9, 467, 442, 290, 275, 329, 105, 115, 415, 41, 125, 471, 421, 247, 182, 158, 311, 39, 284, 291, + 243, 455, 124, 7, 462, 232, 62, 78, 437, 220, 266, 358, 473, 479, 417, 477, 447, 34, 295, 244, 173, + 77, 403, 483, 460, 396, 102, 205, 123, 259, 212, 461, 203, 33, 493, 301, 136, 5, 64, 104, 157, 337, + 318, 102, 44, 39, 246, 97, 334, 85, 407, 442, 168, 203, 466, 211, 463, 271, 450, 166, 374, 368, + 186, 137, 436, 246, 41, 90, 272, 220, 4, 69, 430, 319, 242, 13, 402, 426, 276, 13, 450, 223, 340, + 83, 394, 10, 69, 422, 230, 473, 414, 121, 297, 161, 458, 396, 413, 164, 475, 83, 111, 71, 397, 11, + 148, 433, 302, 99, 26, 232, 177, 45, 161, 223, 37, 398, 29, 200, 255, 257, 44, 402, 329, 290, + ], + true, + ), + ]; + + for (stones, expected) in test_cases { + assert_eq!(S::stone_game_ix(stones.to_vec()), expected); + } + } +}