Problem list
Log in
#r03623 · CCC 2026 Junior

Snail Path

Unrated 2.0 s 256 MB

A snail is crawling across an infinite grid of equally sized squares. It can crawl horizontally (east and west) or vertically (north and south), but it cannot crawl diagonally.

As the snail crawls, it leaves a trail of slime which makes the squares of the grid it touches slimy.

For example, after the snail below crawls east $3$ squares, there will be $4$ slimy squares as shown.

Given the movements taken by the snail, your job is to determine how many times the snail enters a slimy square.

Technical Notes

You might receive an unpredictable error message if your program uses too much memory.

Python 3 submissions for this problem will be evaluated using the standard interpreter python3 (Version 3.10.12) instead of pypy3 7.3.9 (Version 3.8.13).

Input

The first line of input contains a positive integer, $M$, representing the number of movements taken by the snail.

The next $M$ lines will specify the snail's movements, in order. Each movement will contain an uppercase directional letter (N, E, S, or W), followed by a positive integer less than or equal to 20 representing the number of squares the snail crawls in that direction.

Output

Output the non-negative integer, $T$, which is the number of times the snail enters a slimy square.

Example 1

Input
3
S2
N2
S3
Output
4

The diagram shows the snail's path. Whenever the snail enters a slimy square, a numbered circle is placed along the path.

Notice that a single slimy square can be entered multiple times.

Example 2

Input
3
S2
W15
N20
Output
0

The snail's path will consist of (38) slimy squares. However, the snail never returns to a square after leaving it, so the snail never enters a slimy square.

Source
CCC 2026 Junior
Appears in
Not part of a ladder, contest or set yet.
Accepted 0 Submissions 0 Acceptance 0% Solvers 0

Discussion

Threads about this problem. Spoilers are collapsed; wrap hints in ||double bars||.

No discussion yet. Start a thread if something here needs explaining.

Recent submissions Open with filters
Code