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 problem | Meaning in code |
|---|---|
| a square | one position (r, c) — row, column (Lesson 7!) |
a movement, e.g. S2 | a direction letter + how many squares: go South, 2 separate single steps |
N / S / E / W | the 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, T | a counter: +1 for every entry into an already-slimy square — the same square can score many times |
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?”visited before any movement. Lesson: when a rule is unclear, let the sample decide.Step 2 · The logic plan (before any code)
- Start at
r = 0, c = 0. Put the start invisited. SetT = 0. - For each movement, split it into a letter and a number:
"S2" → "S"and2. (Careful: the number can have two digits — 15, 20 — so take everything after the first character.) - Turn the letter into
(dr, dc)using the direction table. - Repeat that many times: take one step (
r += dr; c += dc), then ask: is(r, c)invisited? If yes,T += 1. Either way, make sure(r, c)is invisitednow. - After all movements, print
T.
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
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)
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
Input:
2 / E2 / W2. Walk it on squared paper. What is T?
Show answer
The 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.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.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 answer
BecauseT 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.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 problem | Meaning in code / maths |
|---|---|
| the laneway | a grid with 2 rows and C columns — but the cells are triangles, not squares |
a tile, 1 or 0 | grid[r][c] is 1 (wet) or 0 (dry) — input gives the numbers separated by spaces |
| tape around a wet area | the perimeter: count the tile sides that touch something dry (or the outside) |
| two wet tiles sharing a side | that side is inside the wet area — it gets no tape |
| tiles alternate direction | first 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 |
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.Step 2 · The logic plan (before any code)
- Read
C, then read the two rows as lists of integers. - Count
B= the total number of 1s in both rows. Start withtape = 3 × B. - 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.) - For the between-rows pairs, check only even columns 0, 2, 4, …: if both tiles in that column are 1,
tape -= 2. - Print
tape.
Step 3 · Walk the official Sample 2 — count, then fix
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)
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
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 answer
B = 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).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 answer
B = 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 ✔.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 answer
6. 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.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 answer
With −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 ✔.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.