Exemplo n.º 1
0
def main():
    lines = get_file_lines('day2.in')
    result = get_bathroom_code(lines)
    print(result)

    #part 2
    result = get_new_bathroom_code(lines)
    print(result)
Exemplo n.º 2
0
def main():
    inp = list(map(int, get_file_lines("day1.in")))
    N = 2020
    result = product_of_sum_to(inp, N)
    print(result)

    #part 2
    result = product_of_three_sum_to(inp, N)
    print(result)
Exemplo n.º 3
0
def main():
    inp = [line.strip() for line in get_file_lines('day5.in')]
    result = highest_seat_id(inp)
    print(result)
    #print(get_seat_id("FFFBBBFRRR"))
    #print(get_seat_id("BBFFBBFRLL"))

    #part 2
    result = get_my_seat_id(inp)
    print(result)
Exemplo n.º 4
0
def main():
    file_lines = get_file_lines("day7.in")
    graph = create_graph(file_lines)
    #print(graph)
    result = how_many_contain_gold(graph)
    print(result)

    #part 2
    result = how_many_in_gold(graph)
    print(result)
Exemplo n.º 5
0
def main():
    lines = [line.strip() for line in get_file_lines("day6.in")]
    result = get_num_yes_for_groups(lines)
    print(result)
    result = all_yes_for_groups(lines)
    print(result)
Exemplo n.º 6
0
from file_lines import get_file_lines

lines = get_file_lines('day9.in')

inp = ''.join(lines)


def part_2(s, start, end):
    result = 0
    while start < end:
        if s[start] != '(':
            result += 1
            start += 1
        else:
            first_num = []
            start += 1
            while inp[start] != 'x':
                first_num.append(inp[start])
                start += 1
            start += 1
            second_num = []
            while inp[start] != ')':
                second_num.append(inp[start])
                start += 1
            first_num = int(''.join(first_num))
            second_num = int(''.join(second_num))
            start += 1
            result += second_num * part_2(s, start, start + first_num)
            start += first_num
    return result
Exemplo n.º 7
0
from file_lines import get_file_lines

lines = get_file_lines('day16.in')

fields = {}
my_ticket = None
nearby_tickets = []

section = 0
for line in lines:
    if line == '':
        section += 1
        continue
    elif line in ('your ticket:', 'nearby tickets:'):
        continue
    if section==0:
        field, rest = line.split(': ')
        ranges = rest.split(' or ')
        r1 = ranges[0].split('-')
        r1 = range(int(r1[0]), int(r1[1])+1)
        r2 = ranges[1].split('-')
        r2 = range(int(r2[0]), int(r2[1])+1)
        fields[field] = [r1, r2]
    elif section==1:
        my_ticket = list(map(int, line.split(',')))
    else:
        nearby_tickets.append(list(map(int, line.split(','))))

#print(fields)
result = 0
Exemplo n.º 8
0
def main():
    line = get_file_lines('day1.in')[0]
    result = get_length_of_path(line)
    print(result)
Exemplo n.º 9
0
def main():
    lines = get_file_lines('day8.in')
    result = get_result(lines)
    print(result)
    result = get_resultb(lines)
    print(result)
Exemplo n.º 10
0
from file_lines import get_file_lines

lines = get_file_lines('day8.in')


def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)


rows, cols = 6, 50
screen = [[0 for i in range(cols)] for j in range(rows)]

for line in lines:
    instr = line.split()
    if instr[0] == 'rect':
        wide, tall = map(int, instr[1].split('x'))
        for i in range(tall):
            for j in range(wide):
                screen[i][j] = 1
    elif instr[1] == 'row':
        row = int(instr[2][2:])
        by = int(instr[4]) % cols
        screen[row] = screen[row][-by:] + screen[row][:-by]
    else:
        col = int(instr[2][2:])
        by = int(instr[4]) % rows
        for i in range(by):
            tmp = screen[rows - 1][col]
            for j in range(rows - 1, 0, -1):
                screen[j][col] = screen[j - 1][col]
            screen[0][col] = tmp
Exemplo n.º 11
0
from file_lines import get_file_lines

inp = list(map(int, get_file_lines('day15.in')[0].split(',')))

def nth_number(inp, n=2020):
    ages = {}
    for i,e in enumerate(inp[:-1]):
        ages[e] = i
    i = len(inp)
    last_num = inp[-1]
    while i < n:
        if last_num in ages:
            new_num = i-1-ages[last_num]
            ages[last_num] = i-1
            last_num = new_num
        else:
            ages[last_num] = i-1
            last_num = 0
        i += 1
    return last_num

result = nth_number(inp)
print(result)

result = nth_number(inp, 30000000)
print(result)
Exemplo n.º 12
0
from file_lines import get_file_lines

seat_map = [list(s) for s in get_file_lines('day11.in')]
original_seat_map = [[x for x in row] for row in seat_map]


def num_adjacent(seat_map, i, j):
    result = 0
    for x in range(max(0, i - 1), min(len(seat_map), i + 2)):
        for y in range(max(0, j - 1), min(len(seat_map[i]), j + 2)):
            if x == i and y == j:
                continue
            result += seat_map[x][y] == '#'
    return result


def simulate_round(seat_map):
    new_seat_map = []
    any_changes = False
    for i, row in enumerate(seat_map):
        new_row = []
        for j, cell in enumerate(row):
            if cell == 'L' and num_adjacent(seat_map, i, j) == 0:
                any_changes = True
                new_row.append('#')
            elif cell == '#' and num_adjacent(seat_map, i, j) >= 4:
                any_changes = True
                new_row.append('L')
            else:
                new_row.append(cell)
        new_seat_map.append(new_row)
Exemplo n.º 13
0
from file_lines import get_file_lines

lines = get_file_lines('day7.in')


def is_valid_ip(ip):
    l4 = []
    has_abba = False
    is_square = False
    for c in ip:
        if c in ('[', ']'):
            if c == '[':
                is_square = True
            else:
                is_square = False
            l4 = []
            continue
        l4.append(c)
        if len(l4) > 4:
            l4.pop(0)
        if len(l4
               ) == 4 and l4[0] != l4[1] and l4[0] == l4[3] and l4[1] == l4[2]:
            if is_square:
                return False
            has_abba = True
    return has_abba


def supports_ssl(ip):
    l3 = []
    patterns = set()
Exemplo n.º 14
0
def main():
    inp = [line.strip() for line in get_file_lines('day4.in')]
    result = num_valid_passports(get_passports(inp))
    print(result)
Exemplo n.º 15
0
from file_lines import get_file_lines
from collections import Counter

lines = get_file_lines('day4.in')


def shift_cipher(s, shift):
    result = []
    for c in s:
        if c == '-':
            result.append(' ')
            continue
        val = ord(c) - ord('a')
        val = (val + shift) % 26
        result.append(chr(ord('a') + val))
    return ''.join(result)


result = 0
for line in lines:
    parts = line.split('-')
    name = ''.join(parts[:-1])
    line_without_id_sum = '-'.join(parts[:-1])
    sector_id, checksum = parts[-1].split('[')
    sector_id, checksum = int(sector_id), checksum[:-1]
    msg = shift_cipher(line_without_id_sum, sector_id)
    if 'pole' in msg and 'north' in msg:
        print(msg, sector_id)
    c = Counter(name)
    sorted_c_keys = sorted(c.keys(), key=lambda x: (-c[x], x))
    if ''.join(sorted_c_keys[:5]) == checksum:
Exemplo n.º 16
0
from file_lines import get_file_lines

lines = get_file_lines('day3.in')


def is_valid_triangle(a, b, c):
    return a + b > c and a + c > b and b + c > a


grid = [list(map(int, line.split())) for line in lines]

num_valid_triangles = sum(is_valid_triangle(a, b, c) for a, b, c in grid)
print(num_valid_triangles)

new_num_valid_triangles = 0
for i in range(0, len(grid), 3):
    for j in range(len(grid[i])):
        new_num_valid_triangles += is_valid_triangle(grid[i][j],
                                                     grid[i + 1][j],
                                                     grid[i + 2][j])
print(new_num_valid_triangles)