Skip to content

Instantly share code, notes, and snippets.

@hgwood
Last active November 27, 2019 11:57
Show Gist options
  • Save hgwood/0469e856bc9d6580128c41258013be5a to your computer and use it in GitHub Desktop.
Save hgwood/0469e856bc9d6580128c41258013be5a to your computer and use it in GitHub Desktop.
Code for the Battle Dev event on 2019-11-26
# This is a revised, cleaned up version of the code for exercise 3
ncables, nreservations = map(int, input().split())
reservations = [tuple(map(int, input().split())) for _ in range(nreservations)]
reservations = [(i, start, end) for (i, (start, end)) in enumerate(reservations)]
assignments = [0 for _ in reservations]
available_cables = set(range(1, ncables + 1))
aborted = False
for now in range(2501):
starts_now = [i for i, start, end in reservations if start == now]
ends_now = [i for i, start, end in reservations if now == end]
for i in ends_now:
available_cables.add(assignments[i])
if len(starts_now) > len(available_cables):
aborted = True
break
for i in starts_now:
assignments[i] = available_cables.pop()
if aborted:
print("pas possible")
else:
print(*assignments)
# This is a revised, cleaned up version of the code for exercise 4
nstones, npowders, max_capacity = map(int, input().split())
stones = [tuple(map(int, input().split())) for _ in range(nstones)]
powders = [tuple(map(int, input().split())) for _ in range(npowders)]
stones = [(value / weight, weight, True) for value, weight in stones]
powders = [(value, weight, False) for value, weight in powders]
looted_value = 0
remaining_capacity = max_capacity
remaining_items = sorted(stones + powders, key=lambda item: item[0])
while len(remaining_items) > 0 and remaining_capacity > 0:
value, weight, is_stone = remaining_items.pop()
if remaining_capacity >= weight:
remaining_capacity_after = remaining_capacity - weight
looted_value_after = looted_value + value * weight
looted_weight = weight
elif not is_stone:
looted_value_after = looted_value + value * remaining_capacity
remaining_capacity_after = 0
looted_weight = remaining_capacity
if not is_stone and len(remaining_items) > 0:
next_value, next_weight, next_is_stone = remaining_items[-1]
if next_is_stone:
cannot_loot_both_current_and_next = next_weight > remaining_capacity_after
could_loot_next_if_drop_current = next_weight <= remaining_capacity
next_is_more_valuable = next_value * next_weight > value * looted_weight
if cannot_loot_both_current_and_next and could_loot_next_if_drop_current and next_is_more_valuable:
looted_value_after = looted_value
remaining_capacity_after = remaining_capacity
looted_value = looted_value_after
remaining_capacity = remaining_capacity_after
print(looted_value)
# This is a revised, cleaned up version of the code for exercise 1
npeople = int(input())
people = [(name, int(stick_length)) for name, stick_length in [input().split() for _ in range(npeople)]]
name, _ = min(people, key=lambda person: person[1])
print(name)
# This is a revised, cleaned up version of the code for exercise 2
planks = [int(input()) for _ in range(4)]
smallest_plank = min(planks)
throw_away = sum(plank - smallest_plank for plank in planks)
print(throw_away)
# This is as far as I got for exercise 5, including what I wrote after the end of the time limit
import sys
from random import shuffle
letters = 'ABCDEFGHIJKLMN'
graph = [
('A', 'F'),
('A', 'J'),
('A', 'M'),
('B', 'C'),
('B', 'K'),
('B', 'H'),
('C', 'D'),
('C', 'G'),
('D', 'G'),
('D', 'L'),
('E', 'G'),
('E', 'M'),
('F', 'K'),
('F', 'I'),
('H', 'I'),
('H', 'J'),
('I', 'L'),
('J', 'N'),
('K', 'L'),
('M', 'N')
]
letter = input()
edges = [tuple(input().split()) for _ in range(21)]
nodes = set()
for a, b in edges:
nodes.add(a)
nodes.add(b)
nodes = sorted(nodes)
nodesd = dict((j, i) for i, j in enumerate(nodes))
def generate(k):
result = []
for _ in range(k):
r = list(range(len(letters)))
shuffle(r)
result.append(r)
return result
def transpose(i):
print(i)
return [(letters[i[nodesd[a]]], letters[i[nodesd[b]]]) for a, b in edges]
def sort_edges(i):
return sorted((a, b) if a < b else (b, a) for a, b in i)
def evaluate(i):
score = 0
for (a, b), (x, y) in zip(graph, i):
if a == x and b == y:
score += 1
return score
def score(i):
return evaluate(sort_edges(transpose(i)))
def reproduce(i1, i2):
return i1[:len(letters)//2] + i2[len(letters)//2:]
def next_gen(gen):
take = 50
best = sorted(gen, key=score)[:take]
offspring = [reproduce(gen[i], gen[i+1]) for i in range(take - 1)]
return best + offspring
g = generate(100)
for _ in range(100):
g = next_gen(g)
gg=max(g, key=score)
print(score(g))
# This file contains the code I submitted
# Exercise 1
n = int(input())
m = min([(name, int(stick)) for name, stick in [input().split() for _ in range(n)]], key=lambda x: x[1])
print(m[0])
# Exercise 2
xs = [int(input()) for _ in range(4)]
big = min(xs)
print(sum(x - big for x in xs))
# Exercise 3
import sys
n, m = map(int, input().split())
ds = [(i, start, end) for (i, (start, end)) in enumerate([tuple(map(int, input().split())) for _ in range(m)])]
#print(ds)
rs = [0 for _ in ds]
cs = list(range(1, n + 1))
taken_ds = []
for t in range(2501):
starts_now = [i for i, start, end in ds if start == t]
ends_now = [i for i, start, end in ds if t == end]
something_happens = len(starts_now) > 0 or len(ends_now) > 0
if something_happens:
print(t, file=sys.stderr)
print("starts_now", starts_now, file=sys.stderr)
print("ends_now", ends_now, file=sys.stderr)
print("cables", cs, file=sys.stderr)
print("reservations before", rs, file=sys.stderr)
for i in ends_now:
print(i, "returns", rs[i], file=sys.stderr)
cs.append(rs[i])
if len(starts_now) > len(cs):
print("pas possible")
break
for i in starts_now:
take = cs.pop()
print(i, "takes", take, file=sys.stderr)
rs[i] = take
if something_happens:
print("reservations after", rs, file=sys.stderr)
print(*rs)
# Exercise 4
import sys
n, m, c = map(int, input().split())
xs = [tuple(map(int, input().split())) for _ in range(n)]
ys = [tuple(map(int, input().split())) for _ in range(m)]
xs2 = [(v / p, p, True) for v, p in xs]
ys2 = [(v, p, False) for v, p in ys]
total_v = 0
free_c = c
zs = sorted(xs2 + ys2, key=lambda x: x[0])
print(c, file=sys.stderr)
print(zs, file=sys.stderr)
while len(zs) > 0 and free_c > 0:
v, p, i = zs.pop()
if free_c >= p:
simulated_c = free_c - p
simulated_v = total_v + v * p
taken = p
elif not i:
simulated_v = total_v + v * free_c
simulated_c = 0
taken = free_c
if not i and len(zs) > 0:
nv, np, ni = zs[-1]
if ni and np > simulated_c and np <= free_c:
if nv * np > v * taken:
simulated_v = total_v
simulated_c = free_c
print("cancelling because next item is stone", (nv, np), "=", nv * np, "better than current", (v, taken), "=", v * taken, file=sys.stderr)
total_v = simulated_v
free_c = simulated_c
print(total_v, file=sys.stderr)
print(total_v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment