# A Mere Bagatelle

## The challenge

Given a bagatelle board like the one below:

|                    |
|    /\            / |
|      \            /|
|  \      \ /\    \  |
|   \   /       \    |
|\     /   /  \      |
|   \            / \ |
|    \               |
|  \    / \ /\    \  |
|\     /   /  \     /|
|                /   |
|    /\         /  / |
|              /     |
|\     /   /  /      |
|                    |
----------------------


your task is to turn a list of 'throws' into a score.

The board will always be 20 characters (plus 2 edges) wide, but of variable height. Each 'throw' is a number representing where the ball falls into the board, starting at 0 for the far left up to 19 for the far right. Each ball will fall straight down unless it encounters a / or a \. Encountering a / shifts the ball one column to the left, and a \ shifts the ball one column to the right. After being shifted, the ball continues straight down as before. The score for a throw is dependant on where the ball hits the bottom of the board.

The scores for each finishing position are as follows:

|                    |
----------------------
01234567899876543210


and are the same for all boards. The first and last rows will always be empty. The ball is guaranteed to reach the bottom regardless of where it is dropped from, so there will be no boards where it can get trapped by \/,\| or |/ combinations.

## Input

Input will be on STDIN, and will consist of the board followed by a blank line followed by a space separated list of numbers representing the throws.

## Output

Ouput should be the total score for the given game.

## A couple of examples

Input

|                    |
| \                  |
|  \                 |
|   \                |
|    \               |
|     \              |
|      \             |
|       \            |
|        \           |
|         \          |
|          \         |
|           \        |
|            \       |
|             \      |
|              \     |
|               \    |
|                \   |
|                 \  |
|                    |
|                    |
----------------------

1 4 2 19 0 2 4 3 17 13 16


Output

9


Input

|                    |
| \                / |
|  \              /  |
|   \            /   |
|    \          /    |
|     \        /     |
|      \      /      |
|       \    /       |
|        \  /        |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
|                    |
----------------------

15 10 3 8 18 19 0 6 7 10


Output

72


Input

|                    |
| ////////////////// |
| \\\\\\\\\\\\\\\\\\ |
| ////////////////// |
| \\\\\\\\\\\\\\\\\\ |
| ////////////////// |
| \\\\\\\\\\\\\\\\\\ |
| ////////////////// |
| \\\\\\\\\\\\\\\\\\ |
| ////////////////// |
| \\\\\\\\\\\\\\\\\\ |
|                    |
----------------------

1 4 18 17 14 2 0 19 15


Output

18


## Test scripts

I've used some test scripts written by Joey and Ventero (I hope they don't mind...) to create some tests for this task:-

Usage: ./test [your program and its arguments]

This is codegolf - shortest solution wins.

• You could have my generating script for them as well :-). I eventually wanted to make that public but as long as it still needs tweaking for many tasks I didn't want to do that. – Joey Aug 29 '11 at 17:11
• @Joey A generating script would be a useful tool to have available. – Gareth Aug 29 '11 at 17:42
• Both sets of test should be fixed now. – Gareth Aug 29 '11 at 18:45

n/{},)\);{1>:x,,{.x=31%4%(+}%}%[10,.-1%+]+0@[~]{2${=}/+}/\;  I was so tempted to write a solution which works by redefining the symbols /, \, and space, but it's actually quite expensive (especially once you can no longer use the original \). 31%4%( is nicked from Keith Randall's solution and maps the ASCII codes for space, /, and \ to 0, -1, 1 respectively. (See edit history). • Surprisingly long for GolfScript. :-) How did you get it to work with the test script though? All I got was errors when I tried ./test ruby golfscript.rb peter.gs? I threw some tests at it as piped input instead. – Gareth Aug 29 '11 at 19:44 • ./test-bagatelle.sh which golfscript.rb bagatelle2.gs – Peter Taylor Aug 29 '11 at 20:08 ## Python 2, 147 132 130 chars import sys s=0 l=list(sys.stdin) for t in l[-1].split(): p=int(t)+1 for r in l[:-3]:p-=" /".find(r[p]) s+=min(p-1,20-p) print s • Maybe s+=min(p,19-p)? – Keith Randall Sep 2 '11 at 3:07 • for r in l[:-3]:p-=" /".find(r[p]) should save a few chars – gnibbler Sep 2 '11 at 3:55 • Thank you two. Especially gnibbler's trick is truly awesome - but it cannot hurt to know a shorter tri function, too ;) – cemper93 Sep 2 '11 at 11:23 • @gnibbler That's a genius trick. – Gareth Sep 2 '11 at 12:03 • 19-p+1 == 20-p – Lowjacker Sep 2 '11 at 21:29 ## Python, 165 159 chars import sys A=list(sys.stdin) C=range(10) C+=C[::-1] for L in A[-4::-1]:C=[C[i+ord(L[i+1])%31%4-1]for i in range(20)] print sum(C[int(x)]for x in A[-1].split())  It starts with a row of scores and works its way from the bottom up, computing what the scores would be for balls starting at each row. • list(sys.stdin) instead of readlines() – gnibbler Sep 1 '11 at 9:11 ## Ruby, 1231171151079998 97 *b,_,_,n=*$<

• you can use i-='\ /'... instead to save one more char – gnibbler Sep 6 '11 at 5:36