mirror of
https://github.com/Oxbian/CodingGame.git
synced 2025-07-07 04:14:27 +02:00
Adding new challenges solutions
This commit is contained in:
33
Python/Medium_Challenges/conway_sequence.py
Normal file
33
Python/Medium_Challenges/conway_sequence.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
r = int(input()) #Number of the origin
|
||||||
|
l = int(input()) #Line we want to found
|
||||||
|
#print(r,l, file=sys.stderr, flush=True)
|
||||||
|
|
||||||
|
def conway(n,tour):
|
||||||
|
if (tour == l-1):
|
||||||
|
print(n)
|
||||||
|
return
|
||||||
|
|
||||||
|
tab = n.split()
|
||||||
|
n = ""
|
||||||
|
number = tab[0]
|
||||||
|
occurrency = 1
|
||||||
|
|
||||||
|
for i in range(1, len(tab)):
|
||||||
|
if (number == tab[i]):
|
||||||
|
occurrency += 1
|
||||||
|
elif (number != " "):
|
||||||
|
if (len(n) > 1):
|
||||||
|
n += " " + str(occurrency) +" " + str(number)
|
||||||
|
else:
|
||||||
|
n += str(occurrency) +" " + str(number)
|
||||||
|
number = tab[i]
|
||||||
|
occurrency = 1
|
||||||
|
if (len(n) > 1):
|
||||||
|
n += " " + str(occurrency) +" " + str(number)
|
||||||
|
else:
|
||||||
|
n += str(occurrency) + " " + str(number)
|
||||||
|
conway(n, tour+1)
|
||||||
|
|
||||||
|
conway(str(r),0)
|
66
Python/Medium_Challenges/death_first_search_1.py
Normal file
66
Python/Medium_Challenges/death_first_search_1.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import sys
|
||||||
|
import math
|
||||||
|
|
||||||
|
class Node:
|
||||||
|
def __init__(self, _index, _isPasserelle, _linkedNodes):
|
||||||
|
self.index = _index
|
||||||
|
self.isPasserelle = _isPasserelle
|
||||||
|
self.linkedNodes = _linkedNodes
|
||||||
|
|
||||||
|
def addLink(self, newNode):
|
||||||
|
self.linkedNodes.append(newNode)
|
||||||
|
|
||||||
|
def removeLink (self, index):
|
||||||
|
for i in range(len(self.linkedNodes)-1):
|
||||||
|
if self.linkedNodes[i].index == index:
|
||||||
|
self.linkedNodes.pop(i)
|
||||||
|
|
||||||
|
def showInfo(self):
|
||||||
|
print(f"Node {self.index}, est une passerelle ? {self.isPasserelle}", file=sys.stderr, flush=True)
|
||||||
|
|
||||||
|
def showFull(self):
|
||||||
|
print(f"\nInformation du noeud:\nNode {self.index}, est une passerelle ? {self.isPasserelle}", file=sys.stderr, flush=True)
|
||||||
|
for node in self.linkedNodes:
|
||||||
|
node.showInfo()
|
||||||
|
|
||||||
|
def foundClosestPass(nodeList, passId, si):
|
||||||
|
for i in passId:
|
||||||
|
for j in range(len(nodeList[i].linkedNodes)):
|
||||||
|
if nodeList[i].linkedNodes[j].index == si:
|
||||||
|
return i
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def foundNodeToCut(nodeList, si):
|
||||||
|
passerelle = foundClosestPass(nodeList, passId, si)
|
||||||
|
#nodeList[passerelle].showFull()
|
||||||
|
for i in range(len(nodeList[passerelle].linkedNodes)):
|
||||||
|
if nodeList[passerelle].linkedNodes[i].index == si:
|
||||||
|
print(nodeList[passerelle].index, nodeList[si].index)
|
||||||
|
return
|
||||||
|
print(nodeList[passerelle].index, nodeList[passerelle].linkedNodes[0].index)
|
||||||
|
|
||||||
|
nodeList = []
|
||||||
|
passId = 0
|
||||||
|
n, l, e = [int(i) for i in input().split()]
|
||||||
|
for i in range(n):
|
||||||
|
nodeList.append(Node(i, False, [])) #Initialisation des noeuds
|
||||||
|
|
||||||
|
for i in range(l):
|
||||||
|
# n1: N1 and N2 defines a link between these nodes
|
||||||
|
n1, n2 = [int(j) for j in input().split()]
|
||||||
|
nodeList[n1].addLink(nodeList[n2])
|
||||||
|
nodeList[n2].addLink(nodeList[n1])
|
||||||
|
|
||||||
|
passId = [int(input()) for i in range(e)]
|
||||||
|
for i in passId:
|
||||||
|
nodeList[i].isPasserelle = True
|
||||||
|
|
||||||
|
for node in nodeList:
|
||||||
|
node.showFull()
|
||||||
|
|
||||||
|
|
||||||
|
# game loop
|
||||||
|
while True:
|
||||||
|
si = int(input()) # The index of the node on which the Bobnet agent is positioned this turn
|
||||||
|
print(si, file = sys.stderr, flush = True)
|
||||||
|
foundNodeToCut(nodeList ,si)
|
120
Python/Medium_Challenges/mars_lander_2.py
Normal file
120
Python/Medium_Challenges/mars_lander_2.py
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
import sys
|
||||||
|
import math
|
||||||
|
|
||||||
|
def distance_pos(pos1, pos2):
|
||||||
|
return math.sqrt((pos1[0] - pos2[0])**2 + (pos1[1] - pos2[1])**2)
|
||||||
|
|
||||||
|
def distance_point (pt1, pt2):
|
||||||
|
return pt1 - pt2
|
||||||
|
|
||||||
|
def target_dir(x, land_x):
|
||||||
|
return 1 if (x > land_x) else -1
|
||||||
|
|
||||||
|
def check_direction_speed(h_speed):
|
||||||
|
return -1 if (h_speed < 0) else 1
|
||||||
|
|
||||||
|
surface_n = int(input()) # the number of points used to draw the surface of Mars.
|
||||||
|
prev_x, prev_y, land_x, land_y = 0, 0, 1, 1
|
||||||
|
for i in range(surface_n):
|
||||||
|
prev_x, prev_y = land_x, land_y
|
||||||
|
land_x, land_y = [int(j) for j in input().split()]
|
||||||
|
if land_y == prev_y:
|
||||||
|
landing_x = (prev_x, land_x)
|
||||||
|
landing_y = land_y
|
||||||
|
|
||||||
|
target = ((landing_x[0] + landing_x[1]) // 2, landing_y)
|
||||||
|
print("Target:", target, file=sys.stderr)
|
||||||
|
|
||||||
|
#Constants
|
||||||
|
g = 3.711
|
||||||
|
pi = math.pi
|
||||||
|
start_dist_x = -1
|
||||||
|
start_h_speed = -1
|
||||||
|
start_v_speed = -1
|
||||||
|
starting_calc = True
|
||||||
|
counter_start_motion = False # Used to indicate whether we need to counter the starting velocity
|
||||||
|
landing = False # Indicates whether landing phase is live
|
||||||
|
movement = False # Indicates whether movement phase is live
|
||||||
|
|
||||||
|
# game loop
|
||||||
|
while True:
|
||||||
|
# h_speed: the horizontal speed (in m/s), can be negative.
|
||||||
|
# v_speed: the vertical speed (in m/s), can be negative.
|
||||||
|
# fuel: the quantity of remaining fuel in liters.
|
||||||
|
# rotate: the rotation angle in degrees (-90 to 90).
|
||||||
|
# acc: the thrust power (0 to 4).
|
||||||
|
x, y, h_speed, v_speed, fuel, rotate, acc = [int(i) for i in input().split()]
|
||||||
|
|
||||||
|
dist_x = distance_point(x, target[0])
|
||||||
|
acc_h = acc * math.sin(rotate * (pi / 180)) #Accélération horizontale = current accélération * sin(rotate in rad)
|
||||||
|
acc_v = (acc * math.cos(rotate * (pi / 180))) - g #Accélération verticale = current accélération * cos(rotate in rad) - g
|
||||||
|
|
||||||
|
# Starting calculations
|
||||||
|
if starting_calc:
|
||||||
|
# Starting values
|
||||||
|
start_dist_x = abs(distance_point(x, target[0]))
|
||||||
|
start_h_speed = h_speed
|
||||||
|
start_v_speed = v_speed
|
||||||
|
# Target values
|
||||||
|
tar_acc_h = (start_h_speed ** 2) / (2 * start_dist_x) # Horizontal acc when taking into account starting speed
|
||||||
|
try:
|
||||||
|
tar_time = (2 * start_dist_x) // (start_h_speed)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if abs(tar_acc_h) < 2 and start_h_speed != 0:
|
||||||
|
print(0, 4)
|
||||||
|
continue
|
||||||
|
starting_calc = False # We're done with the calculations
|
||||||
|
if start_h_speed != 0:
|
||||||
|
counter_start_motion = True
|
||||||
|
else:
|
||||||
|
movement = True
|
||||||
|
turn = (x + target[0]) // 2
|
||||||
|
# 3 stages (movement, stabilize, landing)
|
||||||
|
# Check what the starting velocity is (can be not_moving or moving)
|
||||||
|
# You will want to initiate movement phase or stabilize phase depending on the start
|
||||||
|
if counter_start_motion is True:
|
||||||
|
thrust = 4 # Fixing one variable
|
||||||
|
direction = check_direction_speed(start_h_speed) # Counters motion
|
||||||
|
rotate_to = direction * (int(math.asin(tar_acc_h / thrust) * 180 / pi) + 2)
|
||||||
|
if v_speed > 0:
|
||||||
|
thrust = 2
|
||||||
|
if abs(h_speed) < 2:
|
||||||
|
rotate_to = 0
|
||||||
|
counter_start_motion = False # We have stabilized
|
||||||
|
# Now we have to check whether we're clear to land
|
||||||
|
# Or maybe we need to move more towards the zone
|
||||||
|
if landing_x[0] < x < landing_x[1]: # We're in the zone
|
||||||
|
landing = True
|
||||||
|
else: # We overshot the zone or we hit the brakes too fast
|
||||||
|
movement = True
|
||||||
|
turn = (x + target[0]) // 2
|
||||||
|
# Now comes the movement
|
||||||
|
if movement is True:
|
||||||
|
thrust = 4
|
||||||
|
if x < turn:
|
||||||
|
rotate_to = -30
|
||||||
|
elif x >= turn:
|
||||||
|
rotate_to = 30
|
||||||
|
|
||||||
|
if landing_x[0] < x < landing_x[1] and abs(h_speed) < 2:
|
||||||
|
movement = False
|
||||||
|
landing = True
|
||||||
|
mov_dir = -1
|
||||||
|
rotate_to = 0
|
||||||
|
|
||||||
|
# We can try doing the landing procedure now
|
||||||
|
if landing is True:
|
||||||
|
if abs(v_speed) < 36:
|
||||||
|
thrust = 2
|
||||||
|
else:
|
||||||
|
thrust = 4
|
||||||
|
|
||||||
|
# Control board
|
||||||
|
print('Current Target is {}'.format(target), file=sys.stderr)
|
||||||
|
print('Starting distance to land is', start_dist_x, file=sys.stderr)
|
||||||
|
print('Horizontal acceleration is {}'.format(acc_h), file=sys.stderr)
|
||||||
|
print('Vertical acceleration is {}'.format(acc_v), file=sys.stderr)
|
||||||
|
print('Distance to flat surface is ', dist_x, file=sys.stderr)
|
||||||
|
|
||||||
|
print(rotate_to, thrust)
|
22
Python/Medium_Challenges/network-cabling.py
Normal file
22
Python/Medium_Challenges/network-cabling.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
x_min = 2**30
|
||||||
|
x_max = -2**30
|
||||||
|
|
||||||
|
y_list = []
|
||||||
|
nb_buildings = int(input())
|
||||||
|
for i in range(nb_buildings):
|
||||||
|
x, y = map(int, input().split())
|
||||||
|
y_list.append(y)
|
||||||
|
print(x,y, file=sys.stderr, flush=True)
|
||||||
|
if (x > x_max) : x_max = x
|
||||||
|
if (x < x_min) : x_min = x
|
||||||
|
|
||||||
|
cable_length = x_max - x_min
|
||||||
|
y_list.sort()
|
||||||
|
y_median : int = y_list[nb_buildings // 2]
|
||||||
|
|
||||||
|
for y in y_list:
|
||||||
|
cable_length += abs(y_median - y)
|
||||||
|
|
||||||
|
print(cable_length)
|
@ -33,10 +33,11 @@ __Be careful, a lot of my solutions aren't optimized because I didn't took time
|
|||||||
|Shadows of the knigth - Chapter 1 | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/C/Medium_Challenges/shadow_of_knight_1.c) | |
|
|Shadows of the knigth - Chapter 1 | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/C/Medium_Challenges/shadow_of_knight_1.c) | |
|
||||||
|Stock exchange losses| [x](https://github.com/ARKAGEDON/CodingGame/blob/main/C/Medium_Challenges/stock_exchange_losses.c) | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/stock_exchange_losses.py) |
|
|Stock exchange losses| [x](https://github.com/ARKAGEDON/CodingGame/blob/main/C/Medium_Challenges/stock_exchange_losses.c) | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/stock_exchange_losses.py) |
|
||||||
|Winamax battle | []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/winamax_battle.py) |
|
|Winamax battle | []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/winamax_battle.py) |
|
||||||
|Don't panic - Chapter 1 | []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/don't_panic_1.py) |
|
|Don't panic - Chapter 1 | []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/don't_panic_1.py) |
|
||||||
|
|Death First Search - Chapter 1| []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/death_first_search_1.py) |
|
||||||
## Hard challenges
|
|Mars Lander - Chapter 2| []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/mars_lander_2.py) |
|
||||||
--------
|
|Conway sequence| []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/conway_sequence.py) |
|
||||||
|
|Network Cabling| []() | [x](https://github.com/ARKAGEDON/CodingGame/blob/main/Python/Medium_Challenges/network_cabling.py) |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user