Exemplo n.º 1
0
import AoC_tools.aoc_tools as aoc

path = "C:/Users/Admin/SURFdrive/Code/advent_of_code/2021/2/input.txt"
data = aoc.lines2list(path)
data = aoc.split_list(data)
for d in data:
    d[1] = int(d[1])
print(data)

# forward position, depth
pos = 0
depth = 0

for d in data:
    if d[0] == 'forward':
        pos += d[1]
    elif d[0] == 'down':
        depth += d[1]
    elif d[0] == 'up':
        depth -= d[1]

print(pos, depth)
print(pos * depth)
assert pos * depth == 1488669
Exemplo n.º 2
0
import AoC_tools.aoc_tools as aoc

data = aoc.lines2list(
    "C:/Users/Admin/SURFdrive/Code/advent_of_code/2021/3/input.txt")

# initialize count for most common bits in each position
bits = [0] * len(data[0])

print(bits)
for line in data:
    for i in range(len(line)):
        bits[i] += int(line[i])

print(bits)

for b in range(len(bits)):
    if bits[b] > len(data) / 2:
        bits[b] = '1'
    elif bits[b] < len(data) / 2:
        bits[b] = '0'

gamma = int("".join(bits), 2)
epsilon = [str(1 - int(b)) for b in bits]
epsilon = int("".join(epsilon), 2)
result = gamma * epsilon

assert result == 741950
Exemplo n.º 3
0
import AoC_tools.aoc_tools as aoc

data = aoc.lines2list(
    path="C:/Users/Admin/SURFdrive/Code/advent_of_code/2021/5/testinput.txt")
data = aoc.split_list(data, sep=" -> ")
#print(data)
for i in range(len(data)):
    data[i] = [(int(data[i][0].split(',')[0]), int(data[i][0].split(',')[1])),
               (int(data[i][1].split(',')[0]), int(data[i][1].split(',')[1]))]
#print(data)

# selecteer horizontale en diagonale lijnen

lines = []
for i in range(len(data)):
    if data[i][0][0] == data[i][1][0] or data[i][0][1] == data[i][1][1]:
        lines.append(data[i])

#print(lines)

# expand lines


def expand(line):
    x1, y1 = line[0]
    x2, y2 = line[1]
    newline = []
    # increase in x
    if x2 - x1 > 0:
        for i in range((x2 - x1) + 1):
            newline.append((x1 + i, y1))
Exemplo n.º 4
0
import AoC_tools.aoc_tools as aoc

data = aoc.lines2list("input.txt")
output_values = [line.split(" | ")[1] for line in data]
print(output_values)

result = 0
for o in output_values:
    digits = o.split()
    for d in digits:
        if len(d) in [2, 3, 4, 7]:
            result += 1

assert result == 375
Exemplo n.º 5
0
import AoC_tools.aoc_tools as aoc

# part 1

measurements = aoc.lines2list('input.txt', numeric=True)

intervals = [(measurements[n] - measurements[n - 1])
             for n in range(1, len(measurements))]

count = 0
for i in intervals:
    if i > 0:
        count += 1

print(count)
assert (count == 1624)

# part 2
numbers = aoc.lines2list('input.txt', numeric=True)

measurements = [(numbers[n] + numbers[n + 1] + numbers[n + 2])
                for n in range(len(numbers) - 2)]

#aoc.lprint(measurements)

intervals = [(measurements[n] - measurements[n - 1])
             for n in range(1, len(measurements))]

count = 0
for i in intervals:
    if i > 0: