Last active
March 27, 2020 09:17
-
-
Save abidkhan484/dcde3443423b5cec230811e99f3e5818 to your computer and use it in GitHub Desktop.
Advent Of Code: 2019; Problem Statement: https://adventofcode.com/2019/day/3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/bin/python | |
| # Too Much Time Consuming For Big Size Input | |
| # Because of for 31st line condition | |
| # As List takes O(n) time with "in" operator | |
| # Dictionary can be used instead of "direction_list" List | |
| # As Dictionary takes O(1) time to check | |
| # Another Solution is here: https://github.com/abidkhan484/adventOfCode/commit/2590ca911faf78b5a60612ebb3b065d0b66f2fef | |
| def get_direction_list(input_data): | |
| input_data = input_data.split(',') | |
| directions = {'U' : 1, 'D' : -1, 'R' : 1, 'L' : -1} | |
| direction_list = [] | |
| x = y = 0 | |
| for item in input_data: | |
| direction = item[0] | |
| distance = int(item[1:]) | |
| x_axis = y_axis = 0 | |
| if (direction == 'U'): | |
| y_axis = 1 | |
| elif (direction == 'D'): | |
| y_axis = -1 | |
| elif (direction == 'L'): | |
| x_axis = -1 | |
| elif (direction == 'R'): | |
| x_axis = 1 | |
| for _ in range(distance): | |
| x, y = x_axis + x, y_axis + y | |
| if (x,y) not in direction_list: | |
| direction_list.append( (x,y) ) | |
| return direction_list | |
| def get_intersetion_points(first_direction_list, second_direction_list): | |
| return list(set(first_direction_list) & set(second_direction_list)) | |
| def get_minimum_distance(intersections): | |
| return min([abs(item[0]) + abs(item[1]) for item in intersections]) | |
| input1 = 'R8,U5,L5,D3' | |
| input2 = 'U7,R6,D4,L4' | |
| first_direction_list = get_direction_list(input1) | |
| second_direction_list = get_direction_list(input2) | |
| intersections = get_intersetion_points(first_direction_list, second_direction_list) | |
| minimum_distance = get_minimum_distance(intersections) | |
| print(minimum_distance) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment