File size: 27,933 Bytes
79615ae
 
0e39b7f
79615ae
0e39b7f
79615ae
 
 
 
 
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
 
79615ae
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
 
 
 
79615ae
 
 
0e39b7f
 
 
 
 
 
 
 
 
79615ae
0e39b7f
 
 
 
79615ae
 
 
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
79615ae
0e39b7f
 
 
 
79615ae
 
 
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
0e39b7f
79615ae
0e39b7f
 
 
79615ae
 
 
0e39b7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79615ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
from __future__ import annotations

import math
import re
from math import comb, factorial, perm
from typing import Optional

from models import SolverResult


# ----------------------------
# Core helpers
# ----------------------------

def _clean(text: str) -> str:
    t = (text or "").strip()
    t = t.replace("×", "x")
    t = t.replace("–", "-").replace("—", "-")
    t = t.replace("“", '"').replace("”", '"')
    t = t.replace("’", "'")
    t = re.sub(r"\s+", " ", t)
    return t


def _lower(text: str) -> str:
    return _clean(text).lower()


def _safe_comb(n: int, r: int) -> Optional[int]:
    if n < 0 or r < 0 or r > n:
        return None
    return comb(n, r)


def _safe_perm(n: int, r: int) -> Optional[int]:
    if n < 0 or r < 0 or r > n:
        return None
    return perm(n, r)


def _fact(n: int) -> Optional[int]:
    if n < 0:
        return None
    return factorial(n)


def _make_result(topic: str, answer: int, steps: list[str]) -> SolverResult:
    # Keep numeric answer internal / structured.
    # The outer reply layer can decide whether to reveal it.
    return SolverResult(
        domain="quant",
        solved=True,
        topic=topic,
        answer_value=str(answer),
        internal_answer=str(answer),
        steps=steps,
    )


def _numbers(text: str) -> list[int]:
    return [int(x) for x in re.findall(r"\d+", text)]


def _has_any(text: str, words: list[str]) -> bool:
    return any(w in text for w in words)


def _word_frequency_from_quoted_or_caps(raw: str) -> Optional[dict[str, int]]:
    """
    Tries to detect repeated-letter arrangement prompts:
    - letters of "BALLOON"
    - word MISSISSIPPI
    - arrange the letters in BOOKKEEPER
    """
    # quoted word
    m = re.search(r'letters?\s+of\s+"([A-Za-z]+)"', raw, re.I)
    if not m:
        m = re.search(r"word\s+([A-Za-z]+)", raw, re.I)
    if not m:
        m = re.search(r'arrange\s+the\s+letters?\s+(?:in|of)\s+"?([A-Za-z]+)"?', raw, re.I)

    if not m:
        return None

    word = m.group(1).strip()
    if not word.isalpha() or len(word) < 2:
        return None

    freq: dict[str, int] = {}
    for ch in word.upper():
        freq[ch] = freq.get(ch, 0) + 1
    return freq


def _multiset_permutations(freq: dict[str, int]) -> int:
    total = sum(freq.values())
    out = factorial(total)
    for c in freq.values():
        out //= factorial(c)
    return out


def _extract_n_r_from_choose_style(lower: str) -> Optional[tuple[int, int]]:
    patterns = [
        r"(?:choose|select|pick|form|make)\s+(\d+)\s+(?:from|out of)\s+(\d+)",
        r"(?:choose|select|pick)\s+(\d+)\s+of\s+(\d+)",
        r"(?:committee|team|group|delegation|panel)\s+of\s+(\d+).*?(?:from|out of)\s+(\d+)",
        r"(\d+)\s+(?:chosen|selected|picked)\s+from\s+(\d+)",
        r"how many combinations.*?(\d+).*?(?:from|out of)\s+(\d+)",
        r"n\s*=\s*(\d+)\s*,?\s*r\s*=\s*(\d+)",  # only if combo cues present elsewhere
    ]

    for pat in patterns:
        m = re.search(pat, lower)
        if m:
            a, b = int(m.group(1)), int(m.group(2))
            # for n=,r= pattern we want n,r order
            if "n=" in pat:
                return a, b
            return b, a
    return None


def _extract_n_r_from_perm_style(lower: str) -> Optional[tuple[int, int]]:
    patterns = [
        r"(?:arrange|order|rank|assign|seat)\s+(\d+)\s+(?:from|out of)\s+(\d+)",
        r"(?:choose|select|pick)\s+(\d+)\s+(?:from|out of)\s+(\d+)\s+(?:and|then)\s+(?:arrange|order|rank)",
        r"permutations?\s+of\s+(\d+)\s+(?:from|out of)\s+(\d+)",
        r"(?:how many ways|number of ways).*?(?:arrange|order|rank).*?(\d+).*?(?:from|out of)\s+(\d+)",
        r"(\d+)\s+(?:positions|places|slots).*?(?:filled|chosen).*?(?:from|out of)\s+(\d+)",
    ]

    for pat in patterns:
        m = re.search(pat, lower)
        if m:
            r, n = int(m.group(1)), int(m.group(2))
            return n, r
    return None


def _extract_total_distinct_arrangement_n(lower: str) -> Optional[int]:
    patterns = [
        r"(?:arrange|order|permute|seat)\s+(\d+)\s+(?:different|distinct)?\s*(?:objects|items|people|books|letters|marbles|students|digits)?",
        r"permutations?\s+of\s+(\d+)\s+(?:different|distinct)?",
        r"arrangements?\s+of\s+(\d+)\s+(?:different|distinct)?",
    ]
    for pat in patterns:
        m = re.search(pat, lower)
        if m:
            return int(m.group(1))
    return None


def _extract_circular_n(lower: str) -> Optional[int]:
    patterns = [
        r"(?:around|in)\s+a\s+circle.*?(\d+)",
        r"circular arrangements?.*?(\d+)",
        r"seat\s+(\d+).*?(?:around|in)\s+a\s+(?:round\s+)?table",
        r"arrange\s+(\d+).*?(?:around|in)\s+a\s+circle",
    ]
    for pat in patterns:
        m = re.search(pat, lower)
        if m:
            return int(m.group(1))
    return None


def _extract_required_excluded_committee(lower: str) -> Optional[tuple[int, int, int, int]]:
    """
    Returns (n_total, r_choose, required_count, excluded_count)
    for patterns like:
    - committee of 4 from 10 if A must be included
    - choose 3 from 8 if 2 specific people are excluded
    """
    base = _extract_n_r_from_choose_style(lower)
    if not base:
        return None

    n, r = base
    required = 0
    excluded = 0

    # specific people forced in
    req_patterns = [
        r"(?:must|has to|have to|should)\s+be\s+included",
        r"including\s+\w+",
        r"include\s+\w+",
        r"with\s+\w+\s+included",
    ]
    exc_patterns = [
        r"(?:must|has to|have to)\s+not\s+be\s+included",
        r"excluding\s+\w+",
        r"exclude\s+\w+",
        r"without\s+\w+",
    ]

    if any(re.search(p, lower) for p in req_patterns):
        required = 1
    if any(re.search(p, lower) for p in exc_patterns):
        excluded = 1

    m = re.search(r"(\d+)\s+specific\s+(?:people|members|students|persons)\s+(?:must|have to)\s+be\s+included", lower)
    if m:
        required = int(m.group(1))

    m = re.search(r"(\d+)\s+specific\s+(?:people|members|students|persons)\s+(?:must|have to)\s+be\s+excluded", lower)
    if m:
        excluded = int(m.group(1))

    if required == 0 and excluded == 0:
        return None

    return n, r, required, excluded


def _extract_group_selection(lower: str) -> Optional[tuple[int, int, int, int, str]]:
    """
    Handles common grouped committee/team cases:
    - 3 men and 2 women from 7 men and 5 women
    - at least 2 women from 6 men and 4 women for a committee of 4
    Returns:
      (a_total, b_total, choose_total, threshold, mode)
    mode in {"exact_a", "at_least_a"}
    Here group A is first-mentioned category.
    """
    # exact split: x men and y women from M men and W women
    m = re.search(
        r"(\d+)\s+\w+\s+and\s+(\d+)\s+\w+.*?(?:from|out of)\s+(\d+)\s+\w+\s+and\s+(\d+)\s+\w+",
        lower,
    )
    if m:
        a_choose = int(m.group(1))
        b_choose = int(m.group(2))
        a_total = int(m.group(3))
        b_total = int(m.group(4))
        # pack choose_total as first component sum, threshold as a_choose,
        # mode exact_a means choose threshold from first group and remainder from second
        return a_total, b_total, a_choose + b_choose, a_choose, "exact_a"

    # at least k from first group
    m = re.search(
        r"at least\s+(\d+)\s+\w+.*?(?:committee|team|group|delegation|selection)\s+of\s+(\d+).*?(?:from|out of)\s+(\d+)\s+\w+\s+and\s+(\d+)\s+\w+",
        lower,
    )
    if m:
        threshold = int(m.group(1))
        choose_total = int(m.group(2))
        a_total = int(m.group(3))
        b_total = int(m.group(4))
        return a_total, b_total, choose_total, threshold, "at_least_a"

    return None


def _extract_adjacent_block_n_k(lower: str) -> Optional[tuple[int, int]]:
    """
    Tries to detect:
    - among n distinct objects, k specific objects must be together
    - A and B must sit together among n people
    Returns (n_total_distinct, block_size)
    """
    # count named items joined by and
    m = re.search(
        r"(\d+)\s+(?:distinct|different)?\s*(?:people|students|books|letters|objects|items|marbles).*?(?:must|have to)\s+be\s+together",
        lower,
    )
    if m:
        n = int(m.group(1))
        # if text explicitly lists "a and b", treat as 2
        pair = re.search(r"\b\w+\s+and\s+\w+\b.*?(?:must|have to)\s+be\s+together", lower)
        if pair:
            return n, 2

    # more direct "2 specific people must sit together among 7"
    m = re.search(
        r"(\d+)\s+specific\s+(?:people|students|books|letters|objects|items).*?(?:must|have to)\s+be\s+together.*?(?:among|from|out of)\s+(\d+)",
        lower,
    )
    if m:
        k = int(m.group(1))
        n = int(m.group(2))
        return n, k

    # "A and B together among 6 people"
    m = re.search(
        r"\b\w+\s+and\s+\w+\b.*?(?:together|next to each other|adjacent).*?(?:among|out of|from)\s+(\d+)",
        lower,
    )
    if m:
        n = int(m.group(1))
        return n, 2

    # fallback: if exactly one overall n and pair-together cue exists
    nums = _numbers(lower)
    if len(nums) == 1 and _has_any(lower, ["together", "next to each other", "adjacent"]):
        return nums[0], 2

    return None


def _extract_not_together_pair(lower: str) -> Optional[int]:
    if not _has_any(lower, ["not together", "not adjacent", "not next to each other", "cannot sit together"]):
        return None
    nums = _numbers(lower)
    if nums:
        return nums[-1] if len(nums) == 1 else max(nums)
    return None


def _extract_relative_order_n(lower: str) -> Optional[int]:
    """
    Detect simple relative-order cases:
    - A left of B
    - A before B
    - A ahead of B
    among n distinct objects/people
    """
    if not _has_any(lower, ["left of", "before", "ahead of", "to the left of"]):
        return None
    nums = _numbers(lower)
    if nums:
        return nums[-1] if len(nums) == 1 else max(nums)
    return None


def _extract_stars_bars(lower: str) -> Optional[tuple[int, int, bool]]:
    """
    Detect nonnegative / positive integer solution counts:
    - number of nonnegative integer solutions to x+y+z=10
    - positive integer solutions to a+b+c=12
    Returns (n_sum, k_vars, positive_required)
    """
    m = re.search(
        r"(nonnegative|positive)\s+integer\s+solutions?.*?to\s+([a-z](?:\s*\+\s*[a-z])+)\s*=\s*(\d+)",
        lower,
    )
    if not m:
        return None

    positivity = m.group(1)
    vars_expr = m.group(2)
    total = int(m.group(3))
    vars_count = len(re.findall(r"[a-z]", vars_expr))
    return total, vars_count, positivity == "positive"


def _extract_digit_codes(lower: str) -> Optional[tuple[int, bool, bool]]:
    """
    Handles password/code/number formation style:
    Returns (length, repetition_allowed, starts_with_zero_allowed_guess)
    """
    if not _has_any(lower, ["digit number", "digits", "code", "password", "license plate", "pin"]):
        return None

    m = re.search(r"(\d+)[-\s]digit", lower)
    if not m:
        m = re.search(r"code\s+of\s+length\s+(\d+)", lower)
    if not m:
        return None

    length = int(m.group(1))
    repetition_allowed = _has_any(lower, ["repetition allowed", "can repeat", "may repeat", "with repetition"])
    starts_zero_allowed = not _has_any(lower, ["first digit cannot be 0", "leading zero not allowed", "cannot start with 0"])

    return length, repetition_allowed, starts_zero_allowed


def _extract_available_digits(lower: str) -> Optional[int]:
    m = re.search(r"from\s+(\d+)\s+(?:digits|numbers)", lower)
    if m:
        return int(m.group(1))
    # decimal digit default
    if _has_any(lower, ["digit number", "digits", "code", "password", "pin"]):
        return 10
    return None


# ----------------------------
# Main solver
# ----------------------------

def solve_combinatorics(text: str) -> Optional[SolverResult]:
    raw = _clean(text or "")
    lower = raw.lower()

    if not raw:
        return None

    combinatorics_cues = [
        "combination", "combinations", "permutation", "permutations",
        "arrange", "arrangement", "arrangements", "order", "ordered",
        "choose", "select", "pick", "committee", "team", "group",
        "delegation", "panel", "seat", "seating", "circular", "circle",
        "round table", "together", "adjacent", "left of", "before",
        "anagram", "letters of", "word", "integer solutions", "password",
        "license plate", "pin", "code", "nonnegative integer", "positive integer",
    ]
    if not _has_any(lower, combinatorics_cues):
        return None

    # ----------------------------------------
    # 1) Repeated-letter arrangements / anagrams
    # ----------------------------------------
    freq = _word_frequency_from_quoted_or_caps(raw)
    if freq and _has_any(lower, ["letters", "word", "arrange", "anagram", "distinct arrangements"]):
        result = _multiset_permutations(freq)
        return _make_result(
            topic="combinatorics",
            answer=result,
            steps=[
                "Treat this as an arrangement of letters with repetition.",
                "Start with the factorial of the total number of letters.",
                "Then divide by factorials of each repeated-letter count so identical rearrangements are not overcounted.",
                "Evaluate that expression at the end.",
            ],
        )

    # ----------------------------------------
    # 2) Circular arrangements
    # ----------------------------------------
    n_circle = _extract_circular_n(lower)
    if n_circle is not None and n_circle >= 1:
        result = 1 if n_circle == 1 else factorial(n_circle - 1)
        return _make_result(
            topic="combinatorics",
            answer=result,
            steps=[
                "For circular arrangements of distinct objects, rotations count as the same arrangement.",
                "Fix one object to remove rotational duplicates.",
                "Then arrange the remaining objects in the remaining positions.",
                "Evaluate the factorial expression at the end.",
            ],
        )

    # ----------------------------------------
    # 3) Integer-solution counting (stars and bars)
    # ----------------------------------------
    stars = _extract_stars_bars(lower)
    if stars:
        total, vars_count, positive_required = stars
        if positive_required:
            adjusted = total - vars_count
            if adjusted < 0:
                return _make_result(
                    topic="combinatorics",
                    answer=0,
                    steps=[
                        "For positive integer solutions, first give each variable 1.",
                        "That reduces the remaining amount to distribute.",
                        "If the remaining amount is negative, no valid solutions exist.",
                    ],
                )
            result = comb(adjusted + vars_count - 1, vars_count - 1)
            return _make_result(
                topic="combinatorics",
                answer=result,
                steps=[
                    "This is a stars-and-bars counting problem with positive integers.",
                    "Give each variable 1 first so the positivity condition is satisfied.",
                    "Then count the nonnegative distributions of the remaining total among the variables.",
                    "Evaluate the resulting combination.",
                ],
            )
        else:
            result = comb(total + vars_count - 1, vars_count - 1)
            return _make_result(
                topic="combinatorics",
                answer=result,
                steps=[
                    "This is a stars-and-bars counting problem with nonnegative integers.",
                    "Interpret the total as stars and the separators between variables as bars.",
                    "Count the placements of the bars among the stars.",
                    "Evaluate the resulting combination.",
                ],
            )

    # ----------------------------------------
    # 4) Exact grouped selections
    # ----------------------------------------
    grouped = _extract_group_selection(lower)
    if grouped:
        a_total, b_total, choose_total, threshold, mode = grouped
        if mode == "exact_a":
            a_choose = threshold
            b_choose = choose_total - a_choose
            left = _safe_comb(a_total, a_choose)
            right = _safe_comb(b_total, b_choose)
            if left is not None and right is not None:
                result = left * right
                return _make_result(
                    topic="combinations",
                    answer=result,
                    steps=[
                        "Split the selection by category.",
                        "Choose the required number from the first group.",
                        "Choose the remaining number from the second group.",
                        "Multiply the independent counts.",
                    ],
                )

        if mode == "at_least_a":
            total_count = 0
            valid = False
            for a_choose in range(threshold, choose_total + 1):
                b_choose = choose_total - a_choose
                left = _safe_comb(a_total, a_choose)
                right = _safe_comb(b_total, b_choose)
                if left is None or right is None:
                    continue
                total_count += left * right
                valid = True
            if valid:
                return _make_result(
                    topic="combinations",
                    answer=total_count,
                    steps=[
                        "Break the problem into valid cases based on how many can come from the first group.",
                        "For each valid case, choose from the first group and choose the remainder from the second group.",
                        "Add the case counts together.",
                    ],
                )

    # ----------------------------------------
    # 5) Committee / selection with required or excluded members
    # ----------------------------------------
    req_exc = _extract_required_excluded_committee(lower)
    if req_exc:
        n, r, required, excluded = req_exc

        # required members first
        if required > 0 and excluded == 0:
            remaining_n = n - required
            remaining_r = r - required
            result = _safe_comb(remaining_n, remaining_r)
            if result is not None:
                return _make_result(
                    topic="combinations",
                    answer=result,
                    steps=[
                        "Treat the required members as already chosen.",
                        "Reduce both the total pool and the number still to be selected.",
                        "Then count the remaining selection with combinations.",
                    ],
                )

        # excluded members first
        if excluded > 0 and required == 0:
            remaining_n = n - excluded
            result = _safe_comb(remaining_n, r)
            if result is not None:
                return _make_result(
                    topic="combinations",
                    answer=result,
                    steps=[
                        "Remove the excluded members from the pool first.",
                        "Then choose the full committee from the reduced pool.",
                    ],
                )

        # both required and excluded
        remaining_n = n - required - excluded
        remaining_r = r - required
        result = _safe_comb(remaining_n, remaining_r)
        if result is not None:
            return _make_result(
                topic="combinations",
                answer=result,
                steps=[
                    "Force the required members into the selection.",
                    "Remove the excluded members from the pool.",
                    "Then choose the remaining spots from the remaining eligible people.",
                ],
            )

    # ----------------------------------------
    # 6) Pair not together / not adjacent
    # ----------------------------------------
    n_not_together = _extract_not_together_pair(lower)
    if n_not_together is not None and n_not_together >= 2:
        total = factorial(n_not_together)
        together = 2 * factorial(n_not_together - 1)
        result = total - together
        return _make_result(
            topic="permutations",
            answer=result,
            steps=[
                "Count all unrestricted arrangements first.",
                "Then count the arrangements where the two specified objects stay together by treating them as one block.",
                "Because the two objects can switch places inside the block, include that internal ordering factor.",
                "Subtract the together-case count from the total.",
            ],
        )

    # ----------------------------------------
    # 7) Adjacent/together block arrangements
    # ----------------------------------------
    block = _extract_adjacent_block_n_k(lower)
    if block:
        n, k = block
        if n >= k >= 2:
            result = factorial(n - k + 1) * factorial(k)
            return _make_result(
                topic="permutations",
                answer=result,
                steps=[
                    "Treat the required adjacent objects as one block.",
                    "Count the arrangements of that block together with the remaining distinct objects.",
                    "Then multiply by the internal arrangements of the objects inside the block.",
                ],
            )

    # ----------------------------------------
    # 8) Relative order: A before B / left of B
    # ----------------------------------------
    n_order = _extract_relative_order_n(lower)
    if n_order is not None and n_order >= 2:
        result = factorial(n_order) // 2
        return _make_result(
            topic="permutations",
            answer=result,
            steps=[
                "Start from all arrangements of the distinct objects.",
                "For any arrangement, swapping the two named objects reverses their relative order.",
                "So exactly half of all arrangements satisfy the required order condition.",
            ],
        )

    # ----------------------------------------
    # 9) Exact combination selection nCr
    # ----------------------------------------
    choose_nr = _extract_n_r_from_choose_style(lower)
    if choose_nr and _has_any(lower, ["choose", "select", "pick", "committee", "team", "group", "delegation", "panel"]):
        n, r = choose_nr
        result = _safe_comb(n, r)
        if result is not None:
            return _make_result(
                topic="combinations",
                answer=result,
                steps=[
                    "This is a selection problem where order does not matter.",
                    "Use combinations: choose the required number from the total pool.",
                    "Set up the combination expression and evaluate it at the end.",
                ],
            )

    # ----------------------------------------
    # 10) Exact permutation / ordered selection nPr
    # ----------------------------------------
    perm_nr = _extract_n_r_from_perm_style(lower)
    if perm_nr:
        n, r = perm_nr
        result = _safe_perm(n, r)
        if result is not None:
            return _make_result(
                topic="permutations",
                answer=result,
                steps=[
                    "This is an ordered selection problem, so order matters.",
                    "Choose and arrange the required number of positions from the total available objects.",
                    "Set up the permutation expression and evaluate it at the end.",
                ],
            )

    # ----------------------------------------
    # 11) Full arrangement of n distinct objects
    # ----------------------------------------
    if _has_any(lower, ["arrange", "arrangement", "arrangements", "permutation", "permutations", "seat", "seating", "order"]):
        n_all = _extract_total_distinct_arrangement_n(lower)
        if n_all is not None:
            result = _fact(n_all)
            if result is not None:
                return _make_result(
                    topic="permutations",
                    answer=result,
                    steps=[
                        "All distinct objects are being arranged.",
                        "Fill positions one by one: first position, second position, and so on.",
                        "That leads to the factorial count for all distinct arrangements.",
                    ],
                )

    # ----------------------------------------
    # 12) Digits / codes / passwords
    # ----------------------------------------
    digit_case = _extract_digit_codes(lower)
    if digit_case:
        length, repetition_allowed, starts_zero_allowed = digit_case
        available = _extract_available_digits(lower) or 10

        if repetition_allowed:
            if starts_zero_allowed:
                result = available ** length
            else:
                result = (available - 1) * (available ** (length - 1))
            return _make_result(
                topic="combinatorics",
                answer=result,
                steps=[
                    "Treat each position independently.",
                    "Use the allowed number of choices for the first position, paying attention to any leading-zero restriction.",
                    "Then multiply by the allowed choices for each remaining position.",
                ],
            )

        # no repetition allowed
        if length > available:
            return _make_result(
                topic="combinatorics",
                answer=0,
                steps=[
                    "Without repetition, you cannot fill more positions than the number of available distinct digits.",
                    "So this setup has no valid arrangements.",
                ],
            )

        if starts_zero_allowed:
            result = perm(available, length)
        else:
            result = (available - 1) * perm(available - 1, length - 1)

        return _make_result(
            topic="combinatorics",
            answer=result,
            steps=[
                "This is an ordered arrangement of digits.",
                "Because repetition is not allowed, the number of choices decreases from position to position.",
                "Handle the first digit separately if leading zero is not allowed.",
                "Then multiply the sequential choices.",
            ],
        )

    # ----------------------------------------
    # 13) Direct nCr / nPr notation
    # ----------------------------------------
    m = re.search(r"(\d+)\s*c\s*(\d+)", lower)
    if m:
        n, r = int(m.group(1)), int(m.group(2))
        result = _safe_comb(n, r)
        if result is not None:
            return _make_result(
                topic="combinations",
                answer=result,
                steps=[
                    "Interpret the notation as a combination count.",
                    "Evaluate the combination expression carefully.",
                ],
            )

    m = re.search(r"(\d+)\s*p\s*(\d+)", lower)
    if m:
        n, r = int(m.group(1)), int(m.group(2))
        result = _safe_perm(n, r)
        if result is not None:
            return _make_result(
                topic="permutations",
                answer=result,
                steps=[
                    "Interpret the notation as a permutation count.",
                    "Evaluate the permutation expression carefully.",
                ],
            )

    return None