PYTHON 3 · CCC JUNIOR · LESSON 7 PRACTICE

Two Real CCC Problems — Step by Step, From the Logic Up

Companion to Lesson 7: 2D Grids. For each problem we do the same four moves a strong competitor makes: 1 · translate the story into grid language, 2 · find the counting logic, 3 · walk the official sample one step at a time, 4 · turn the logic into code. Then it is your turn.

How to work in class: at every animation step, predict before you press Step. Say out loud what should change — then check. Predicting is where the learning happens; the animation is only the referee.

Problem 1 · Snail Path — CCC 2026 J4

A snail crawls on an infinite grid. Each square it touches becomes slimy. Given its movements, count how many times it enters a square that is already slimy. (Full statement: CEMC 2026 Junior problem set, linked at the bottom.)

Step 1 · Translate the story into grid language

Words in the problemMeaning in code
a squareone position (r, c) — row, column (Lesson 7!)
a movement, e.g. S2a direction letter + how many squares: go South, 2 separate single steps
N / S / E / Wthe Lesson 7 direction table: N = (−1, 0), S = (+1, 0), W = (0, −1), E = (0, +1)
“slimy”this (r, c) is already in our visited collection
“enters a slimy square”after one single step, the new (r, c) was already visited before this step
the answer, Ta counter: +1 for every entry into an already-slimy square — the same square can score many times
The one logic decision that decides the whole problem: a movement like E20 is not one jump. It is 20 entries, and every single one must be checked — the snail might cross its own trail in the middle of a movement, not just at the end. So the inner logic is always: expand every movement into single squares, then ask one yes/no question per square: “have I been here before?”
Detective work — the sample tells you a hidden rule: does the starting square count as slimy? The problem never says so in words. Test it on Sample 1 below: if the start is not slimy, the count comes out 3 — but the official answer is 4. If the start is slimy from the beginning, the count is exactly 4. ✔ So the starting square goes into visited before any movement. Lesson: when a rule is unclear, let the sample decide.

Step 2 · The logic plan (before any code)

  1. Start at r = 0, c = 0. Put the start in visited. Set T = 0.
  2. For each movement, split it into a letter and a number: "S2" → "S" and 2. (Careful: the number can have two digits — 15, 20 — so take everything after the first character.)
  3. Turn the letter into (dr, dc) using the direction table.
  4. Repeat that many times: take one step (r += dr; c += dc), then ask: is (r, c) in visited? If yes, T += 1. Either way, make sure (r, c) is in visited now.
  5. After all movements, print T.
Why a set and not a grid? The grid is infinite and the snail can wander far away (Sample 2 travels 15 + 20 squares) — you cannot build a list big enough, and positions can be “negative” (north/west of the start). A set of (r, c) pairs stores only the squares actually visited, wherever they are. This is the Lesson 7 “visited trail” idea, without the tray.

Step 3 · Walk the official Sample 1 — one square at a time

Sample Input 1 3 S2 N2 S3 Official Output 4
Animation — 🐌 = the snail · green ≈ = slimy · orange ①②… = a counted re-entry
Code — the logic plan, translated line by line
M = int(input())
dirs = {"N": (-1, 0), "S": (1, 0),
        "E": (0, 1),  "W": (0, -1)}

r, c = 0, 0
visited = {(0, 0)}      # start is slimy!
T = 0

for _ in range(M):
    move = input().strip()
    dr, dc = dirs[move[0]]
    steps = int(move[1:])   # may be 2 digits
    for _ in range(steps):  # ONE square
        r += dr
        c += dc
        if (r, c) in visited:
            T += 1          # slimy entry!
        visited.add((r, c))

print(T)
Check yourself against Sample 2: input 3 / S2 / W15 / N20 → official output 0. The path visits 38 distinct squares (the start + 37 steps) but never re-enters one — so a big trail can still score 0. T counts re-entries, not slimy squares. Same numbers, different question: entries (37 after the start) − first-time entries (37) = 0 re-entries.

Step 4 · Your turn — predict first, then reveal

Try 1 · Out and back
Input: 2 / E2 / W2. Walk it on squared paper. What is T?
Show answerThe snail visits (0,1), (0,2) — both new. Coming back W2: (0,1) is slimy → T = 1; (0,0) is the start, also slimy → T = 2. Answer: 2. Every step of the way home re-enters slime.
Try 2 · A small square
Input: 4 / E1 / S1 / W1 / N1 — the snail walks a tiny loop and comes home. What is T?
Show answer(0,1), (1,1), (1,0) are all new. The last step N1 re-enters (0,0) — the starting square, slimy from the beginning → T = 1. Answer: 1. If you answered 0, you forgot the detective rule from the sample: the start counts.
Try 3 · Explain it
In Sample 2 the snail ends with 38 slimy squares but the answer is 0. Explain in one sentence why both can be true.
Show answerBecause T does not count slimy squares — it counts entries into squares that were already slimy, and in Sample 2 every entry was into a brand-new square, so there was nothing to count.
Three traps that catch real contestants: (1) Jumping — moving 20 squares at once and only checking the landing square; crossings in the middle are missed. (2) One-digit parsing — reading W15 as “W, 1” drops 14 squares of path. (3) Flipped north — in (r, c) language, north is row −1 (up the page = smaller row), exactly as in Lesson 7 §4. Mixing this up mirrors the whole path — harmless for the count in some cases, fatal in others.

Problem 2 · Trianglane — CCC 2023 J4/S1

A laneway is made of two rows of equilateral triangular tiles, each side 1 metre. Some tiles are painted black (wet, shown as 1); the rest are white (dry, 0). Warning tape goes around the perimeter of all wet areas. How many metres of tape are needed? (Full statement: CEMC 2023 Junior problem set, linked at the bottom.)

Step 1 · Translate the story into counting language

Words in the problemMeaning in code / maths
the lanewaya grid with 2 rows and C columns — but the cells are triangles, not squares
a tile, 1 or 0grid[r][c] is 1 (wet) or 0 (dry) — input gives the numbers separated by spaces
tape around a wet areathe perimeter: count the tile sides that touch something dry (or the outside)
two wet tiles sharing a sidethat side is inside the wet area — it gets no tape
tiles alternate directionfirst tile points up ▲, next points down ▼, and the second row is the opposite way round — this decides which tiles can share a side at all
The counting trick — “start generous, then fix the over-count”: if no wet tiles touched, each wet triangle would need 3 metres (3 sides), so the answer would be 3 × B where B = number of wet tiles. Every time two wet tiles share a side, that side was counted twice (once by each tile) but needs zero tape — so subtract 2. The whole problem becomes: tape = 3B − 2 × (number of shared sides between wet tiles). Now we just have to count shared sides carefully — that is a Lesson 7 neighbour problem in disguise.
Where can a shared side hide? Exactly three places: (1) side-by-side in the top row, (2) side-by-side in the bottom row, (3) between the rows in the same column. Places 1–2 are easy: any two neighbouring columns that are both wet share a slanted side. Place 3 has the twist — look at the animation’s lane: in even columns (0, 2, 4, …) the top ▲ and bottom ▼ meet along a full flat side, but in odd columns the two triangles meet only at a point — and a point is not a side, so it removes no tape. Vertical pairs count only in even columns.

Step 2 · The logic plan (before any code)

  1. Read C, then read the two rows as lists of integers.
  2. Count B = the total number of 1s in both rows. Start with tape = 3 × B.
  3. Scan each row left → right: whenever two side-by-side tiles are both 1, that is one shared side — tape -= 2. (Look only at pairs (0,1), (1,2), (2,3), … — never count a pair twice.)
  4. For the between-rows pairs, check only even columns 0, 2, 4, …: if both tiles in that column are 1, tape -= 2.
  5. Print tape.

Step 3 · Walk the official Sample 2 — count, then fix

Sample Input 2 7 0 0 1 1 0 1 0 0 0 1 0 1 0 0 Official Output 11 Sample Input 1 (try it yourself below) 5 1 0 1 0 1 0 0 0 0 0 → Output 9
Animation — dark = wet (1) · the number under each tile is its value
Code — the logic plan, translated line by line
C = int(input())
row0 = [int(x) for x in input().split()]
row1 = [int(x) for x in input().split()]
rows = [row0, row1]

B = 0
for r in range(2):
    for c in range(C):
        B += rows[r][c]      # count the 1s
tape = 3 * B                 # start generous

# shared sides inside each row
for r in range(2):
    for c in range(C - 1):
        if rows[r][c] == 1 and rows[r][c + 1] == 1:
            tape -= 2

# shared sides between the rows:
# EVEN columns only!
for c in range(0, C, 2):
    if row0[c] == 1 and row1[c] == 1:
        tape -= 2

print(tape)
Cross-check the answer a second way (the official explanation does this): the wet tiles form three separate areas. The big area (top row columns 2–3 + bottom row column 2) has perimeter 5; each single wet tile has perimeter 3. Total 5 + 3 + 3 = 11 ✔. Two different routes to the same number is how you know the logic is right — contest veterans build this habit on every problem.
Input warning: Trianglane’s rows arrive as 0 0 1 1 0 1 0 — numbers with spaces. That is why the code uses input().split() and int(x). It is not a string grid like Lesson 7 §2 — list(input()) here would give you spaces as cells!

Step 4 · Your turn — predict first, then reveal

Try 1 · The official Sample 1
C = 5, top row 1 0 1 0 1, bottom row all 0. Run the logic plan by hand: what are B, the shared sides, and the tape?
Show answerB = 3 → start 9. Side-by-side pairs: the wet tiles are separated by dry ones, so 0 shared sides. Between rows: the bottom row is all dry, so 0. Tape = 9 − 0 = 9 ✔ (the official output).
Try 2 · One shared side
C = 3, top row 1 1 0, bottom row 0 0 0. Predict the tape, then draw the two triangles and check your number against the picture.
Show answerB = 2 → start 6. The two wet tiles are side-by-side in the top row: 1 shared side → −2. Tape = 4. Draw it: two triangles glued along one side make a diamond shape with exactly 4 outer sides ✔.
Try 3 · The even-column trap
C = 3, top row 0 1 0, bottom row 0 1 0 — two wet tiles, one directly above the other in column 1. Is the tape 4 or 6?
Show answer6. Column 1 is odd: the two triangles touch only at a point, so they share no side and nothing is subtracted: 3 × 2 − 0 = 6. (Move the same pair to column 0 or 2 — an even column — and the answer becomes 4.) This one rule is the difference between a full-score and a wrong answer on this problem.
Try 4 · Explain the “−2”
A classmate says: “A shared side needs no tape, so subtract 1, not 2.” Use Sample 2’s big wet area (3 tiles, 2 shared sides, true perimeter 5) to show why they are wrong.
Show answerWith −1 per shared side: 3 × 3 − 2 × 1 = 7 — but the real perimeter is 5. The shared side was counted twice in 3B (once as a side of each tile) and needs zero tape, so the full correction is 2 per shared side: 9 − 2 × 2 = 5 ✔.
Three traps that catch real contestants: (1) Counting vertical pairs in every column — odd columns only touch at a point; Try 3 above is exactly this trap. (2) Counting a pair twice — scanning (2,3) and later (3,2) as if they were different pairs; the loop for c in range(C - 1) looking only at the right neighbour counts each pair exactly once. (3) Reading the input like a string grid — the spaces are part of the input format, not cells.