Esempio n. 1
0
import time
import aoc_data

data = aoc_data.load(13).rstrip(b"\n").split(b"\n")
data = [v.decode("utf-8") for v in data]
# data = ["939", "7,13,x,x,59,x,31,19"]

# part 1
et = int(data[0])  # earliest timestamp you could depart on a bus
bus_ids = [int(v) for v in data[1].split(",") if not v == "x"]


def get_earliest_bus(ts):
    d = []
    for v in bus_ids:
        if ts % v == 0:
            d.append(0)
        else:
            d.append(v - ts % v)
    return bus_ids[d.index(min(d))], min(d)


v1, v2 = get_earliest_bus(et)
print(v1 * v2)

start = time.time()
# part 2
# This can be solved by chinese remainder theorem
# https://en.wikipedia.org/wiki/Chinese_remainder_theorem

# For example, let's consider data = ["939", "7,13,x,x,59,x,31,19"]
Esempio n. 2
0
import time
import aoc_data

data = aoc_data.load(1)
data = [int(v) for v in data.split()]

start = time.time()
min_val = min(data)
# remove the elements whose sum will always be larger than 2020 with any other
# element
data = [v for v in data if v + min_val <= 2020]

N = len(data)
for i in range(N):
    j = i + 1
    for j in range(j, N):
        if data[i] + data[j] == 2020:
            print(i, j)
            print(data[i], data[j])
            print(data[i] * data[j])
            break

print("-------")
# part 2
niter = 0
N = len(data)
for i in range(N):
    j = i + 1
    for j in range(j, N):
        if data[i] + data[j] < 2020:
            k = j + 1