The city of Poughkeepsie celebrates Halloween every year. Geographically, the city is a rectangle that is 30 miles long and 45 miles wide. On a map, it looks like a grid, with its east-west roads horizontally dividing the city into 1-mile-long rectangles and its north-south roads vertically dividing the city likewise. There is one house at each road intersection.

The city's residents tend to be more generous with their candy as one moves toward the center of the rectangle. The resident at the lower-left corner of the city - whom we'll call Bob - gives out 10 candies per child. If a child starts at Bob's house and proceeds eastward, each resident they come across will give them 5 more candies than the last one (In other words, Bob's nearest eastern neighbor will give 15 candies. If a child walks another mile to the next eastern neighbor, they get another 20 candies.) After they reach the horizontal center street, that number decreases by 5. If that same child instead proceeds northward of Bob's house - which is located at (0,0) - each resident they meet will give them 10 more candies than the previous resident. After they reach the relevant center street, each resident they meet will give 10 less.

The number of candies given out by residents who do not live on the edges of the city is the sum of the number of candies given out by the two neighbors who live on the nearest borders of the city. So, a resident who lives at (44,29) will give out the number of candies given by the resident at (45,29) + the number of candies given by the resident at (30,44).

Important note: The only way to access each house is by walking along roads or adjacent paths. One is not allowed to, for example, bee-line from Bob's house to (1,1) - s/he must first either walk along the y-axis for 1 mile, turn right and walk for another mile; or walk along the x-axis for 1 mile, turn left and walk for another mile. Also, once a child has received candy from a house, they will receive no more candy from that same house.

Objective: Your program is to figure out how much candy a child can receive if s/he starts at Bob's house and is to walk no more than a given distance "d".

Scoring Programs will be scored based on the size (in bytes) of the source code.

  • 1
    \$\begingroup\$ Also, you have not specified how much candy is received from the north and east borders. \$\endgroup\$
    – Zgarb
    Oct 21, 2015 at 19:32
  • \$\begingroup\$ @Zgarb: Each resident gives as much candy as does the resident who shares a street with, but lives on the opposite border from, them. So, the resident at (45,5) gives the same amount of candy as the resident at (0,5) \$\endgroup\$
    – moonman239
    Oct 21, 2015 at 19:52
  • \$\begingroup\$ This doesn't take into account how terrible the traffic/roads are in Poughkeepsie. ;) (Assuming you're talking about NY). \$\endgroup\$ Oct 21, 2015 at 19:52
  • \$\begingroup\$ It's a grid? google.com/maps/place/Poughkeepsie,+NY/… \$\endgroup\$
    – user46167
    Oct 21, 2015 at 20:21
  • 1
    \$\begingroup\$ @ev3commander If you end up at (2,2) you get the number of candies given by the resident at (2,0) + the number of candies given by the resident at (0,2). There is one - and only one - house per intersection. \$\endgroup\$
    – moonman239
    Oct 22, 2015 at 15:49

1 Answer 1


Jelly, 58 bytes


Try it online!

Very slow (brute force), TIO can handle up to 8. Test cases in \$[0,8]\$: Try it online!

  • \$\begingroup\$ Note: I've tested cases until \$4\$ by hand. \$\endgroup\$ Jul 28, 2018 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.