Example #1
0
def Day08():
    instructions = []

    for instruction in aoc.input(8):
        instructions.append(instruction[:-1])

    # part 1
    print(f'part 1: {RunInstructions(instructions)}')

    # part 2
    for i in range(len(instructions)):
        temp = copy.deepcopy(instructions)

        it = temp[i].split(' ')
        if it[0] == 'nop':
            it[0] = 'jmp'
        elif it[0] == 'jmp':
            it[0] = 'nop'
        else:
            continue
        temp[i] = f'{it[0]} {it[1]}'

        output = RunInstructions(temp)

        if output[0] == 0:
            print(f'part 2: {output}')
            break
Example #2
0
import aoc

ids = []

for line in aoc.input(5):
    seat = 0
    vert = list(map(lambda v: '1' if v == 'B' else '0', line[:7][::-1]))
    hori = list(map(lambda h: '1' if h == 'R' else '0', line[7:10][::-1]))

    for i in range(7):
        seat += int(vert[i]) * (2**i)
    seat *= 8

    for i in range(3):
        seat += int(hori[i]) * (2**i)

    if seat not in ids:
        ids.append(seat)

ids.sort()

print(f'part 1: {max(ids)}')

for i in range(1, len(ids)):
    if ids[i - 1] + 2 == ids[i]:
        print(f'part 2: {ids[i]-1}')
Example #3
0
import aoc
import re

rules = {}

for line in aoc.input(7):
    r_eval = lambda r, s: re.search(r, s).group(0)

    conditions = re.findall('\d+ \w+ \w+', line)
    current = r_eval('^\w+ \w+', line)
    rules[current] = {
        r_eval('\w+ \w+$', c): r_eval('^\d+', c)
        for c in conditions
    }

valid = []
get_iter_sum = lambda color: sum([i for i in get_containers(color)])


def get_containers(bag_color):
    for rule in rules:
        if bag_color in rules[rule] and rule not in valid:
            valid.append(rule)
            yield get_iter_sum(rule) + 1


def get_bag_count(bag_color):
    count = 0
    if len(rules[bag_color]) > 0:
        for key in rules[bag_color]:
            count += (get_bag_count(key) * int(rules[bag_color][key])) + int(
Example #4
0
import aoc
from copy import deepcopy

adapters = [int(adapter[:-1]) for adapter in aoc.input(10)]
adapters.sort()

ones = 0
threes = 1
prev = 0
for adapter in adapters:
    diff = adapter - prev
    if diff == 1:
        ones += 1
    if diff == 3:
        threes += 1
    prev = adapter
print(f'part 1: {ones * threes}')

count = []
count.append(0)

#print(adapters)

gn = 0
groups = {gn: []}

for i in range(0, len(adapters)):
    if (adapters[i - 1] + 3 == adapters[i]):
        gn += 1
        groups[gn] = []
    groups[gn].append(adapters[i])
Example #5
0
import aoc


count = 0
temp = []

for line in aoc.input(6):
  if line == '\n':
    count += len(temp)
    temp = []
  
  for letter in line:
    if letter not in temp and letter != '\n':
      temp.append(letter)
      
print(f'part 1: {count}')

count = 0
group = []
answers = {}

for line in aoc.input(6):
  if line == '\n':
    count += sum([1 for key in answers if answers[key] == len(group)])
    group = []
    answers = {}
  else:
    group.append(line)
  
    for answer in line:
      if answer not in answers:
Example #6
0
import aoc

instructions = [int(instruction[:-1]) for instruction in aoc.input(9)]
invalid_num = 0
pre_start = 0
pre_end = 25


def preamble_sum_exists(number):
    for i in range(pre_start, pre_end):
        for j in range(pre_start, pre_end):
            if i != j and instructions[i] + instructions[j] == number:
                return (0, number)
    return (-1, number)


for i in range(pre_end, len(instructions)):
    x = preamble_sum_exists(instructions[i])
    if x[0] == -1:
        invalid_num = x[1]
        break
    pre_start += 1
    pre_end += 1


def get_weakness(invalid):
    max_index = instructions.index(invalid)
    for i in range(0, max_index):
        temp = instructions[i]
        for j in range(i + 1, max_index):
            temp += instructions[j]
Example #7
0
import aoc
import re

input = [(i[0], int(re.search('\d+$', i).group(0))) for i in aoc.input(12)]
x = 0
y = 0
face = (1, 0)
dirs = [(0, -1), (1, 0), (0, 1), (-1, 0)]

for cmd, num in input:
    if cmd == 'N':
        y -= num
    elif cmd == 'S':
        y += num
    elif cmd == 'E':
        x += num
    elif cmd == 'W':
        x -= num
    elif cmd == 'F':
        x += (face[0] * num)
        y += (face[1] * num)
    else:
        num /= 90
        i = dirs.index(face)
        if cmd == 'L':
            face = dirs[int((i - num) % 4)]
        elif cmd == 'R':
            face = dirs[int((i + num) % 4)]

print(f'part 1: {x+y}')
Example #8
0
import aoc
from copy import deepcopy

input = ['.' + i[:-1] + '.' for i in aoc.input(11)]
input.insert(0, '.' * len(input[0]))
input.append('.' * len(input[0]))


def update_seating(temp, lst, row, col):
    occupied = 0
    iters = 0

    for r in range(-1, 2):
        for c in range(-1, 2):
            iters += 1
            if lst[row][col] == '#' and iters >= 5 and occupied == 0:
                return
            if r == 0 and c == 0:
                continue
            elif is_dir_occupied(lst, row, col, r, c):
                occupied += 1
            if occupied >= 5 and lst[row][col] == '#':
                r = list(temp[row])
                r[col] = 'L'
                temp[row] = ''.join(r)
                return

    if occupied == 0 and lst[row][col] == 'L':
        r = list(temp[row])
        r[col] = '#'
        temp[row] = ''.join(r)
Example #9
0
# current_time = 6
# earliest_time = int(data[0])

# buses = [int(i) for i in data[1].split(',') if i != 'x']

# while True:
#   bus = [b for b in buses if current_time % b == 0]

#   if len(bus) > 0 and current_time >= earliest_time:
#     result = bus[0] * abs(earliest_time - current_time)
#     print(f'part 1: {result}')
#     break
#   current_time += 1

# part 2
data = [i for i in aoc.input(13)]
arrivals = [1 if i == 'x' else int(i) for i in data[1].split(',')]
#buses = list(filter(lambda x: x != 1, arrivals))
buses = [
    arrivals[i] + i if arrivals[i] != 1 else 1 for i in range(len(arrivals))
]
buses.sort()

z = lambda x, y: x % y == 0

ids_multiplied = 1

for i in range(len(arrivals) - 1):
    ids_multiplied *= (arrivals[i] + i)

depth = 0