示例#1
0
def run_websites(debug=False, overwrite=False, refine=None, synonly=False, service=False, apps=None):
  results = RunResults('websites', overwrite)
  
  sites = get_lines(WEBSITE_FILE, comment='#')
  pidx = 0
  pols = os.listdir(POLICY_DIR)
  plen = len(pols)
  for site in sites:
    # Limit to the given sites names, if provided.
    if apps is not None and site not in apps: continue

    # %%% Generalize
    polrel = pols[pidx]
    poldesc = polrel[:-7]
    policies = {poldesc: [os.path.join(POLICY_DIR, polrel)]}
    pidx = (pidx + 1) % plen
    
    url = 'http://' + site
    res = run_website(url, policies, debug=debug, overwrite=overwrite, refine=refine, synonly=synonly, service=service)

    # Track successful results
    results.add(res)

    # Space the output.
    sys.stderr.write('\n')

  results.printSummary()
示例#2
0
def run_targetpages(debug=False, overwrite=False, refine=None, synonly=False, service=False, apps=None):
  results = RunResults('targetpages', overwrite)
  
  sites = get_lines(TARGETPAGE_FILE, comment='#')
  polnet = os.path.join(POLICY_DIR, 'network-isolation.policy')
  policies = {'network-isolation': [polnet]}
  for site in sites:
    # Limit to the given sites names, if provided.
    if apps is not None and site not in apps: continue

    # Extract the application name from the URL.
    app = None
    paramidx = site.find("?payload=")
    if paramidx > -1:
      plidx = paramidx + 9
      endidx = site.find("&", plidx)
      if endidx == -1: endidx = len(site)
      app = site[plidx:endidx]
      warn("Appname: %s" % app)
    
    url = 'http://' + site
    res = run_website(url, policies, debug=debug, overwrite=overwrite, refine=refine, synonly=synonly, service=service, appname=app)

    # Track successful results
    results.add(res)

    # Space the output.
    sys.stderr.write('\n')

  results.printSummary()
 def __init__(self):
     self.project_dir = Path.absolute(Path(__file__).parent)
     self.host_os = platform.system()
     with open("config.yaml") as f:
         self.conf = yaml.safe_load(f)
     self.proxies = get_lines(self.conf["proxy"]["file"])
     self.headless = self.conf["headless"]
def kv_counts_from_folder(file_name):
  kv_counts = defaultdict(lambda : defaultdict(list))
  for line in util.get_lines(folder_name):
    counts = []
    for i, part in enumerate(line.rstrip().split(",")):
      counts.append(TYPES[i](part))
    if(counts[START] == 0):
      # Some matches have a start_time of 0, this is probably a bug at
      # Valve's end, currently we just ignore those values
      print("FOUND A ZERO VALUE, (line: " + line.rstrip() + " continuing")
      continue
    kv_counts[counts[FIELD]][counts[VALUE]].append((counts[START] / 1000, counts[COUNT]))
  return kv_counts
def get_corr(folder_name):
  correlations = []
  for line in util.get_lines(folder_name):
    parts = line.rstrip().split(",")
    if(parts[2] != "NaN"):
      if(len(parts) == 4):
        correlations.append((dota_values.get_hero_name(int(parts[0]), True),
                             dota_values.get_hero_name(int(parts[1]), True),
                             float(parts[2]), int(parts[3])))
      else:
        correlations.append((dota_values.get_hero_name(int(parts[0]), True),
                             dota_values.get_hero_name(int(parts[1]), True),
                             float(parts[2])))
  return correlations
                driver = self.init_driver()
                self.install_ext(driver)
                while True:
                    try:
                        self.vote(driver, username, vote_url)
                        break
                    except UnexpectedAlertPresentException:
                        # Captcha Error
                        out(f"Retrying to vote for {username}")
                        continue


if __name__ == "__main__":
    bot = Votebot()

    usernames = get_lines(
        bot.conf["username_file"])  # Users to get the voting reward for
    vote_urls = get_lines(
        bot.conf["vote_url_file"]
    )  # URL to the vote page of a server on minecraft-server.eu

    bot.run(usernames, vote_urls)

    if bot.conf["use_timer"] == "True":
        while True:
            # calculate a randomized time for the next execution
            time_till_next_day = datetime.combine(
                datetime.now().date() + timedelta(days=1),
                datetime.strptime("0000", "%H%M").time()) - datetime.now()

            delay = time_till_next_day + timedelta(hours=random.randint(2, 23))
            out(f"Next execution in: {delay}")
示例#7
0
import re

from util import get_lines

lines = get_lines(
    day=4,
    test=False,
    type_conv=lambda x: x.split(' '),
)

required_keys = {
    'byr',
    'iyr',
    'eyr',
    'hgt',
    'hcl',
    'ecl',
    'pid',
}

hcl_re = re.compile('^#[0-9a-f]{6}$')

rules = {
    'byr':
    lambda x: 1920 <= int(x) <= 2002,
    'iyr':
    lambda x: 2010 <= int(x) <= 2020,
    'eyr':
    lambda x: len(x) == 4 and 2020 <= int(x) <= 2030,
    'hgt':
    lambda x: (x.endswith('cm') and 150 <= int(x[:-2]) <= 193) or
示例#8
0
#
#     def valid(self, password):
#         min_, max_ = [int(p) for p in self.rule.split("-")]
#         return min_ <= password.count(self.char) <= max_


# Part 2
class PasswordRule:
    def __init__(self, rule, char):
        self.rule = rule
        self.char = char

    def valid(self, password):
        pos1, pos2 = [int(p) - 1 for p in self.rule.split("-")]
        return ((password[pos1] == self.char) +
                (password[pos2] == self.char)) == 1


lines = get_lines(
    day=2,
    test=False,
    type_conv=lambda x: [
        PasswordRule(*p.split(" ")) if i == 0 else p
        for i, p in enumerate(x.split(": "))
    ],
)

valid = sum([r.valid(p) for r, p in lines])

print(valid)
示例#9
0
            address += opcodes[opcode](
                memory,
                address)  # Will raise a KeyError if opcode is not implemented
        except StopIteration:
            break  # Program has halted
    return memory[0]


def part2(program):
    for noun in range(100):
        for verb in range(100):
            if run_machine(program, noun, verb) == 19690720:
                return 100 * noun + verb


if __name__ == '__main__':
    # Part 1 examples
    assert run_machine([1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50]) == 3500
    assert run_machine([1, 0, 0, 0, 99]) == 2
    assert run_machine([2, 3, 0, 3, 99]) == 2
    assert run_machine([2, 4, 4, 5, 99, 0]) == 2
    assert run_machine([1, 1, 1, 4, 99, 5, 6, 0, 99]) == 30

    formatted_input = [
        int(line) for line in get_lines('input/day02.txt')[0].split(',')
    ]

    # First step is to restore the gravity assist program to the "1202 program alarm" state
    print(run_machine(formatted_input, 12, 2))
    print(part2(formatted_input))
示例#10
0
    def can_contain(self, name):
        for count, bag in self.contains:
            if bag == name or bags[bag].can_contain(name):
                return True

        return False

    def bag_count(self):
        total = 0
        for count, bag in self.contains:
            total += count + (count * bags[bag].bag_count())

        return total


lines = get_lines(
    day=7,
    part=2,
    test=True,
    type_conv=lambda x: x.split(" contain "),
)

for line in lines:
    bags[line[0][:-1]] = Bag(*line)

# Part 1
print(sum([bag.can_contain("shiny gold bag") for bag in bags.values()]))

# Part 2
print(bags["shiny gold bag"].bag_count())
示例#11
0
from collections import defaultdict

from util import get_lines

lines = get_lines(
    day=10,
    test=False,
    type_conv=int,
)

lines = sorted(lines)
lines.append(max(lines) + 3)


def question_one(items):
    c, d = 0, defaultdict(int)

    for j in lines:
        d[j - c] += 1
        c = j

    print(c)


def question_two(items):
    cache = {0: 1}
    for item in items:
        cache[item] = (
            cache.get(item - 3, 0) +
            cache.get(item - 2, 0) +
            cache.get(item - 1, 0)
示例#12
0
from util import get_lines

lines = get_lines(
    day=3,
    test=False,
    type_conv=lambda x: list(x),
)

m = len(lines[0])

# Part 1
slopes = [(3, 1)]

# Part 2
slopes = [(1, 1), (3, 1), (5, 1), (7, 1), (1, 2)]

trees = [0 for _ in slopes]
for s, slope in enumerate(slopes):
    i, j = 0, 0
    x, y = slope
    while True:
        i = (i + x) % m
        j += y

        try:
            trees[s] += lines[j][i] == "#"
        except IndexError:
            break

total = 1
for t in trees:
示例#13
0
import re

from util import get_lines

lines = get_lines(
    day=5,
    test=False,
    type_conv=lambda x: (x[:7], x[7:]),
)


# Part 1
def split(r):
    return r[:int(len(r) / 2)], r[int(len(r) / 2):]


rows = [_ for _ in range(128)]
cols = [_ for _ in range(8)]

seats = []
for line in lines:
    low, high = split(rows)
    for r in line[0]:
        if r == "F":
            low, high = split(low)
        else:
            low, high = split(high)

        if high == [] or low == []:
            try:
                row = high[0]
示例#14
0
# -*- encoding=utf-8 -*-
__author__ = 'Peter'

import codecs, json, re, datetime
from collections import OrderedDict as dict
from dateutil import parser

import util
import nlpir
import numpy

screen_names = util.get_user_path_list('%s')
re_exp = re.compile(r'\[(\S+?)\]')
re_grp = re.compile(r'Weibo\/(\w+)\/')
neg_exp = util.get_lines('./Exps-Neg.lst')

# 第一人称复数 http://baike.baidu.com/view/1569657.htm
set_We = set(['我们', '大家', '咱们', '咱们', '俺们'])

# 第一人称单数
set_I = set(['我', '俺'])

textLen = []


def extractProfile(fname):
    f = dict()
    #f['Group'] = re.findall(re_grp,fname)[0]

    with codecs.open(fname, 'r', encoding='utf-8') as uf:
        u = json.load(uf)
示例#15
0
from util import get_lines

lines = get_lines(
    day=13,
    test=True,
    type_conv=str,
)

timestamp = int(lines[0])
routes = [int(x) if x != "x" else x for x in lines[1].split(",")]

def question_one(timestamp, routes):
    min_ = (0, 1000)

    for r in routes:
        if r == "x":
            continue

        wait = r - (timestamp % r)
        if wait < min_[1]:
            min_ = (r, wait)

    print(min_[0] * min_[1])


def question_two(routes):
    i = 0
    x = [r - j for j, r in enumerate(routes) if r != "x"]
    t =  1
    for r in x:
        t *= r
示例#16
0
from util import get_lines

lines = get_lines(
    day=12,
    test=False,
    type_conv=lambda x: (x[0], int(x[1:])),
)


def question_one(lines):
    current = [0, 0]
    dirs = ['E', 'S', 'W', 'N']
    movements = [(1, 0), (0, -1), (-1, 0), (0, 1)]
    facing = 'E'

    for action, amount in lines:
        if action == 'F':
            action = facing

        if action in dirs:
            i = dirs.index(action)
            move = movements[i]
            current[0] += move[0] * amount
            current[1] += move[1] * amount

        if action in ['L', 'R']:
            i = dirs.index(facing)
            i = i + (amount // 90) if action == 'R' else i - (amount // 90)
            i = i % 4
            facing = dirs[i]
示例#17
0
from util import get_lines

lines = get_lines(1, False, int)

# Part 1
for i in lines:
    if 2020 - i in lines:
        print(i * (2020 - i))

for i, item1 in enumerate(lines[:-3]):
    for j, item2 in enumerate(lines[i:-2]):
        for k, item3 in enumerate(lines[j:]):
            if item1 + item2 + item3 == 2020:
                print(item1 * item2 * item3)
示例#18
0
import re

from util import get_lines

cmd_re = re.compile(r"(acc|jmp|nop) ([+-]{1}\d*)")
lines = get_lines(
    day=8,
    test=False,
    type_conv=str,
)

orig_stack = []
for line in lines:
    m = cmd_re.match(line)
    orig_stack.append((m[1], int(m[2])))

# Part 2
for j in range(len(orig_stack)):
    cmd, x = orig_stack[j]

    stack = orig_stack[:]
    if cmd == "jmp":
        stack[j] = ("nop", x)
    elif cmd == "nop":
        stack[j] = ("jmp", x)
    else:
        continue

    # Part 1
    acc, i = 0, 0
    seen = set()
示例#19
0
import re

from util import get_lines

lines = get_lines(
    day=6,
    test=False,
    type_conv=set,
)

lines += [set()]

groups = []

# Part 1
group = set()
# Part 2
group = set('abcdefghijklmnopqrstuvwxyz')

for line in lines:
    if line != set():
        # Part 1
        # group |= line
        # Part 2
        group &= line

    else:
        groups.append(len(group))
        group = set('abcdefghijklmnopqrstuvwxyz')

print(groups)
示例#20
0
        return False  # No pair of digits

    return True


def part1(low, high):
    return sum(1 for i in range(low, high + 1) if meets_criteria(str(i)))


def part2(low, high):
    return sum(1 for i in range(low, high + 1)
               if meets_criteria(str(i), max_digit_group=2))


if __name__ == '__main__':
    # Part 1 examples
    assert meets_criteria('111111')
    assert not meets_criteria('223450')
    assert not meets_criteria('123789')
    # Part 2 examples
    assert meets_criteria('112233', max_digit_group=2)
    assert not meets_criteria('123444', max_digit_group=2)
    assert meets_criteria('111122', max_digit_group=2)

    formatted_input = [
        int(number) for number in get_lines('input/day04.txt')[0].split('-')
    ]

    print(part1(*formatted_input))
    print(part2(*formatted_input))
示例#21
0
import re
from collections import defaultdict

from util import get_lines

mem_re = re.compile(r"^mem\[(\d+)\] = (\d+)$")

lines = get_lines(
    day=14,
    test=False,
    part=2,
    type_conv=str,
)


class ValueMask:
    def __init__(self, mask):
        self.mask = {i: v for i, v in enumerate(mask) if v != "X"}

    def apply(self, value):
        binstr = list(format(value, '036b'))
        for i, v in self.mask.items():
            binstr[i] = v
        return int("".join(binstr), 2)


class AddressMask:
    def __init__(self, mask):
        self.one_mask = [i for i, v in enumerate(mask) if v == "1"]
        self.float_mask = [i for i, v in enumerate(mask) if v == "X"]
示例#22
0
                wires_path_length = (wire1_steps + l1(line1[0], intersection) +
                                     wire2_steps + l1(line2[0], intersection))
                min_wire_steps = min(min_wire_steps, wires_path_length)
            wire2_steps += l1(line2[0], line2[1])  # Add up the steps as we go
        wire1_steps += l1(line1[0], line1[1])  # Add up the steps as we go

    return min_wire_steps


if __name__ == '__main__':
    # Part 1 examples
    assert minimum_manhatten_distace(
        make_lines('R75,D30,R83,U83,L12,D49,R71,U7,L72'.split(',')),
        make_lines('U62,R66,U55,R34,D71,R55,D58,R83'.split(','))) == 159
    assert minimum_manhatten_distace(
        make_lines('R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51'.split(',')),
        make_lines('U98,R91,D20,R16,D67,R40,U7,R15,U6,R7'.split(','))) == 135
    # Part 2 examples
    assert minimum_wire_steps(
        make_lines('R75,D30,R83,U83,L12,D49,R71,U7,L72'.split(',')),
        make_lines('U62,R66,U55,R34,D71,R55,D58,R83'.split(','))) == 610
    assert minimum_wire_steps(
        make_lines('R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51'.split(',')),
        make_lines('U98,R91,D20,R16,D67,R40,U7,R15,U6,R7'.split(','))) == 410
    formatted_input = [
        make_lines(line.split(',')) for line in get_lines('input/day03.txt')
    ]

    print(minimum_manhatten_distace(*formatted_input))
    print(minimum_wire_steps(*formatted_input))
示例#23
0
    if not count_mass_of_fuel or fuel <= 0:
        return fuel

    return fuel + fuel_required(fuel, count_mass_of_fuel)


def part1(masses):
    return sum(fuel_required(mass) for mass in masses)


def part2(masses):
    return sum(fuel_required(mass, True) for mass in masses)


if __name__ == '__main__':
    # Part 1 examples
    assert fuel_required(12) == 2
    assert fuel_required(14) == 2
    assert fuel_required(1969) == 654
    assert fuel_required(100756) == 33583
    # Part 2 examples
    assert fuel_required(12, True) == 2
    assert fuel_required(1969, True) == 966
    assert fuel_required(100756, True) == 50346

    formatted_input = [int(line) for line in get_lines('input/day01.txt')]

    print(part1(formatted_input))
    print(part2(formatted_input))
示例#24
0
from collections import defaultdict

from util import get_lines

lines = get_lines(
    day=15,
    test=False,
    type_conv=lambda x: [int(_) for _ in x.split(",")],
)


def question(place, line):
    last = None
    seen = defaultdict(lambda: [None, None])

    for i in range(place):
        if i < len(line):
            current = line[i]
        else:
            l = seen[last]
            if l[0] is not None:
                current = l[1] - l[0]
            else:
                current = 0

        s = seen[current]
        seen[current] = s

        if s[1] is None:
            s[1] = i
        else: