Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

2.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69162d64f96574d9bb629efc Challenge 111: Ball Trajectory 29 challenge-111

--description--

Today's challenge is inspired by the video game Pong, which was released November 29, 1972.

Given a matrix (array of arrays) that includes the location of the ball (2), and the previous location of the ball (1), return the matrix indices for the next location of the ball.

  • The ball always moves in a straight line.
  • The movement direction is determined by how the ball moved from 1 to 2.
  • The edges of the matrix are considered walls. If the ball hits a:
    • top or bottom wall, it bounces by reversing its vertical direction.
    • left or right wall, it bounces by reversing its horizontal direction.
    • corner, it bounces by reversing both directions.

--hints--

get_next_location([[0,0,0,0], [0,0,0,0], [0,1,2,0], [0,0,0,0]]) should return [2, 3].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_location([[0,0,0,0], [0,0,0,0], [0,1,2,0], [0,0,0,0]]), [2, 3])`)
}})

get_next_location([[0,0,0,0], [0,0,1,0], [0,2,0,0], [0,0,0,0]]) should return [3, 0].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_location([[0,0,0,0], [0,0,1,0], [0,2,0,0], [0,0,0,0]]), [3, 0])`)
}})

get_next_location([[0,2,0,0], [1,0,0,0], [0,0,0,0], [0,0,0,0]]) should return [1, 2].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_location([[0,2,0,0], [1,0,0,0], [0,0,0,0], [0,0,0,0]]), [1, 2])`)
}})

get_next_location([[0,0,0,0], [0,0,0,0], [2,0,0,0], [0,1,0,0]]) should return [1, 1].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_location([[0,0,0,0], [0,0,0,0], [2,0,0,0], [0,1,0,0]]), [1, 1])`)
}})

get_next_location([[0,0,0,0], [0,0,0,0], [0,0,1,0], [0,0,0,2]]) should return [2, 2].

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_next_location([[0,0,0,0], [0,0,0,0], [0,0,1,0], [0,0,0,2]]), [2, 2])`)
}})

--seed--

--seed-contents--

def get_next_location(matrix):

    return matrix

--solutions--

def get_next_location(matrix):
    prev = None
    curr = None

    for r in range(len(matrix)):
        for c in range(len(matrix[0])):
            if matrix[r][c] == 1:
                prev = (r, c)
            if matrix[r][c] == 2:
                curr = (r, c)

    prev_row, prev_col = prev
    curr_row, curr_col = curr

    dir_x = curr_col - prev_col
    dir_y = curr_row - prev_row

    next_row = curr_row + dir_y
    next_col = curr_col + dir_x

    max_row = len(matrix) - 1
    max_col = len(matrix[0]) - 1

    if next_col < 0 or next_col > max_col:
        dir_x *= -1
    if next_row < 0 or next_row > max_row:
        dir_y *= -1

    next_row = curr_row + dir_y
    next_col = curr_col + dir_x

    return [next_row, next_col]