Exemple #1
0
import funs

# lines = funs.lines_from_file("7.intest")
# lines = funs.lines_from_file("7.intest2")
lines = funs.lines_from_file("7.in")

a1 = 0
a2 = 0
bags = {}
for line in lines:
    cont = line.split(' contain ')
    inner_bags = []
    if 'no other bags' not in cont[1]:
        inner_bags = list(
            map(lambda y: [y[1] + y[2], int(y[0])],
                [x.split(' ') for x in cont[1].split(', ')]))
    bags[''.join(cont[0].split(' ')[:2])] = inner_bags

found = set()
search = {'shinygold'}
while len(search) > 0:
    new_search = set()
    for k, v in bags.items():
        for s in search:
            for vx in v:
                if s in vx:
                    new_search.add(k)
    found.update(new_search)
    search = new_search

a1 = len(found)
Exemple #2
0
from functools import reduce
import funs
import copy

# m = funs.lines_from_file("11.intest")
m = funs.lines_from_file("11.in")
# m.append('')  # Add empty line if needed

adj = [[-1, -1], [-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1]]

m = list(map(list, m))


def add(a, b):
    return [a[0] + b[0], a[1] + b[1]]


def inside(m, pos):
    return 0 <= pos[0] < len(m) and 0 <= pos[1] < len(m[0])


def neighbors(m, pos):
    return filter(lambda y: inside(m, y), map(lambda x: add(x, pos), adj))


def neighs(m, pos, d):
    res = []
    curr = pos
    while True:
        curr = add(curr, d)
        if inside(m, curr):
Exemple #3
0
import funs

ls = funs.lines_from_file("15.intest")
# ls = funs.lines_from_file("15.in")

nrs = list(map(int, ls[0].split(',')))

a1 = 0
a2 = 0


def next_nr(nrs):
    last = nrs[-1]
    if last in nrs[:-1]:
        l = len(nrs) - 1
        for i in range(1, l + 1):
            if nrs[l - i] == last:
                return i
    else:
        return 0


size = 2020

while len(nrs) < size:
    nrs.append(next_nr(nrs))

a1 = nrs[-1]

print('\nRes 1:', a1)
Exemple #4
0
import funs

# ls = funs.lines_from_file("20.intest")
ls = funs.lines_from_file("20.in")
ls.append('')  # Add empty line if needed


def flip(tile):
    n = tile[0][::-1]
    e = tile[3][::-1]
    s = tile[2][::-1]
    w = tile[1][::-1]
    return [n, e, s, w]


def rot(tile):  # rotate 90 deg clockwise
    return [tile[3]] + tile[0:3]


def fix_tile(tile):
    fixed = []
    n = tile[0]
    s = tile[len(tile) - 1][::-1]
    e = ''
    w = ''
    for i in range(len(tile)):
        w += tile[i][0]
        e += tile[i][-1]
    w = w[::-1]
    fixed.append([n, e, s, w])
    fixed.append(rot(fixed[0]))
Exemple #5
0
from functools import *
from itertools import *
from computer import *
import numpy as np
import funs
import copy

# ls = funs.lines_from_file("13.intest")
ls = funs.lines_from_file("13.in")
# ls.append('')  # Add empty line if needed

start = int(ls[0])
buses = ls[1].split(',')

times = []
tt = []

for i in range(len(buses)):
    times.append(0)
    b = buses[i]
    if b == 'x':
        continue
    t = int(b)
    while t < start:
        t += int(b)
    times[i] = t

sched = list(filter(lambda x: x != 0, sorted(times)))
# print(sched)
ix = times.index(sched[0])
# print(ix, times)
Exemple #6
0
#! /usr/bin/python3

import funs
import functools


def slope_trees(dx, dy):
    x, y, cnt = (0, 0, 0)
    while y < len(arr):
        if arr[y][x] == '#':
            cnt = cnt + 1
        y = y + dy
        x = (x + dx) % len(arr[0])
    return cnt


arr = list(funs.lines_from_file('3.in'))

print(slope_trees(3, 1))

slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]
print(
    functools.reduce(lambda a, b: a * b, map(lambda s: slope_trees(*s),
                                             slopes)))
Exemple #7
0
#! /usr/bin/python3

import funs


def parse_line(l):
    minmax, ch, pwd = l.split()
    lo, hi = [int(x) for x in minmax.split('-')]
    return lo, hi, ch[0], pwd


def pwd_valid(cmin, cmax, ch, pwd):
    return cmin <= pwd.count(ch) <= cmax


def pwd_valid2(pos1, pos2, ch, pwd):
    return (pwd[pos1 - 1] == ch) != (pwd[pos2 - 1] == ch)


def count_valid(val_fun, rule_lst):
    return sum(map(lambda r: val_fun(*r), rule_lst))


# lines = funs.read_lines_from_file('2.intest')
lines = funs.lines_from_file('2.in')
rules = list(map(parse_line, lines))

print(count_valid(pwd_valid, rules))
print(count_valid(pwd_valid2, rules))