EaglePB2's Competitive Programming Writeups
  • Home Page
  • Why Python?
  • Why 10^9 + 7?
  • General Formatting Title
  • 🇲🇾CodeNection
    • 2021
      • Closed Cateogry
        • Attend Talks
        • Distant Relatives
        • Concert
        • Mamak
        • Fair Contest
      • Open Preliminary Round
        • f(Aibohphobia)^-1
        • Did they cheat?
        • Semester Breaks
      • Open Final Round
        • Ways 1
        • Circular Campus
        • A joke
        • 🥰Last year when life was better
        • Thank You Pizza
    • 2023
      • Test Round
        • Codey and Alphabetical Scoring
        • Codey and Binary Guesser
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Hide-and-Seek
        • Codey and Math
        • Codey and Textbooks
        • Codey and Money
        • Codey and Team Selection
        • Codey and Painted Tree
        • Codey and Number Grid
        • Codey and Crimes
      • Final Round
        • Codey and CodeNection 2
        • Codey and Connection
        • Codey and Schedule
        • Codey and School Supplies
        • Codey and Zombies
        • Codey and Sightseeing
        • Codey and Apples
        • Codey and Facto
        • Codey and Zoey
    • 2024
      • Test Round
        • Codey and Sunday
        • Codey and Takoyaki
      • Preliminary Round
        • Codey and CodeNection
        • Codey and Pebbles
        • Codey and Spam
        • Codey and Coins
        • Codey and Rectangles
        • Codey and Manuscript
        • Codey and Recipes
        • Codey and Toy Kingdom
        • Codey and Peaks
      • Final Round
        • Codey and Exit
        • Codey and Gardening
        • Codey and Symbol
        • Codey and Rectangles 2
        • Codey and Jutsu
        • Codey and Toy Kingdom 2
        • Codey and Speeches
  • ABaKoDa
    • 2023
      • Round 1
        • Problem Letters
        • Problem Statistics
        • Rankings Order
        • Rankings Search
      • Round 2
        • Abakoda Letters
        • Borrowed Words
        • Kensorship
        • Duel Languages
  • Meta Coding Competitions
    • 2011
      • Qualification Round
        • Double Squares
        • Peg Game
        • Studious Student
      • Round 1A
        • Diversity Number
        • Turn on the Lights
        • Wine Tasting
      • Round 1B
        • Chess 2
        • Diminishing Circle
        • Slot Machine Hacker
      • Round 1C
        • N-Factorful
        • Polynomial Factoring
        • Risky Slide
      • Round 2
        • Bonus Assignments
        • Scott's New Trick
        • Studious Student II
      • Final Round
        • Alien Game
        • Party Time
        • Safest Place
  • EaglePB2's Special
    • Hong Kong Identity card
    • Cycle Prefix Averages
    • Word Squares
Powered by GitBook
On this page
  • Question
  • Input Format
  • Constraints
  • Output Format
  • Sample Inputs:
  1. CodeNection
  2. 2024
  3. Final Round

Codey and Exit

https://www.hackerrank.com/contests/codenection-2024-final-round-closed-category/challenges/cn24-2/problem

Question

Codey is in its bedroom, standing at point (a,b)(a,b)(a,b) on a 2D grid, and the door is located at some point (x,y)(x, y)(x,y). Codey has four possible moves:

  • Move up -> Moves from (a,b)(a, b)(a,b) to (a,b+1)(a, b+1)(a,b+1)

  • Move down -> Moves from (a,b)(a, b)(a,b) to (a,b−1)(a, b-1)(a,b−1)

  • Move left -> Moves from (a,b)(a, b)(a,b) to (a−1,b)(a-1, b)(a−1,b)

  • Move right -> Moves from (a,b)(a, b)(a,b) to (a+1,b)(a+1, b)(a+1,b)

Codey needs your help to find the minimum number of moves required to reach the exit.

Input Format

The first line contains two integers a and b, which represents the coordinate of Codey.

The second line contains two integers x and y, which represents the coordinate of the door.

Constraints

1≤a,b,x,y≤1051 \le a, b, x, y \le 10^51≤a,b,x,y≤105

Output Format

Output the minimum number of moves required for Codey to reach (x,y)(x, y)(x,y) from (a,b)(a, b)(a,b)

Sample Inputs:

Input

1 1
2 2

Output

2

Explanation

Codey can take following steps to exit:

Therefore, the minimum number of moves required is 2.


Solution - Ignore Question Please

The whole question is set up to confuse you, let me explain it into simple words:

Given start coordinates and end coordinates, you can only walk horizontal and vertical, find the minimum steps to reach from start to end.

By using simple math and logic, we know that the minimum steps given the restriction above is the sum of the x-steps + y-steps.

The way to find those 2 steps count (x-axis, y-axis) is destination coordinates - start coordinates. Simple enough.

One thing had to reminder; don't forget to add absolute symbols (In python, it is abs()). Since you may have to walk left and down to reach the destination, therefore should drop the negative symbol before adding together to get the minimum distance.

Here's my solution:

x1, y1 = map(int, input().strip().split())
x2, y2 = map(int, input().strip().split())

print((abs(x2-x1) + abs(y2-y1)))
PreviousFinal RoundNextCodey and Gardening

Last updated 2 months ago

🇲🇾