def all_teams(): all_public_teams = Team.query.filter_by(public_team=True) page = util.as_int(request.values.get('page'), on_fail=1) json_data = util.as_int(request.values.get('json'), on_fail=0) if json_data: teams_dict = {} for team in all_public_teams: team_dict = {} team_dict['name'] = team.name team_dict['tag'] = team.tag team_dict['flag'] = team.flag team_dict['logo'] = team.logo team_dict['players'] = filter(lambda x: bool(x), team.auths) team_dict['players_pref_names'] = filter(lambda x: bool(x), team.preferred_names) teams_dict[team.id] = team_dict return jsonify(teams_dict) else: # Render teams page teams = all_public_teams.paginate(page, 20) editable = g.user is not None and g.user.super_admin return render_template('teams.html', user=g.user, teams=teams, my_teams=editable, page=page, owner=None)
def teams_user(userid): user = User.query.get_or_404(userid) page = util.as_int(request.values.get('page'), on_fail=1) json_data = util.as_int(request.values.get('json'), on_fail=0) if json_data: teams_dict = {} for team in user.teams: team_dict = {} team_dict['name'] = team.name team_dict['tag'] = team.tag team_dict['flag'] = team.flag team_dict['logo'] = team.logo team_dict['players'] = filter(lambda x: bool(x), team.auths) teams_dict[team.id] = team_dict return jsonify(teams_dict) else: # Render teams page my_teams = (g.user is not None and userid == g.user.id) if g.user.admin: teams = Team.query.paginate(page, 20) else: teams = user.teams.paginate(page, 20) return render_template( 'teams.html', user=g.user, teams=teams, my_teams=my_teams, page=page, owner=user)
def matchesliga3(): page = util.as_int(request.values.get('page'), on_fail=1) matches = Match.query.order_by(-Match.id).filter_by( cancelled=False).join(Team, Match.team1_id==Team.id).filter_by( bracket='Liga 3').paginate(page, 20) return render_template('matchesliga3.html', user=g.user, matches=matches, my_matches=False, all_matches=True, page=page)
def matches_user(userid): user = User.query.get_or_404(userid) page = util.as_int(request.values.get('page'), on_fail=1) matches = user.matches.order_by(-Match.id).paginate(page, 20) is_owner = (g.user is not None) and (userid == g.user.id) return render_template('matches.html', user=g.user, matches=matches, my_matches=is_owner, all_matches=False, match_owner=user, page=page)
def match_map_update(matchid, mapnumber): match = Match.query.get_or_404(matchid) match_api_check(request, match) map_stats = match.map_stats.filter_by(map_number=mapnumber).first() if map_stats: t1 = as_int(request.values.get('team1score')) t2 = as_int(request.values.get('team2score')) if t1 != -1 and t2 != -1: map_stats.team1_score = t1 map_stats.team2_score = t2 db.session.commit() else: return 'Failed to find map stats object', 400 return 'Success'
def match_map_update(matchid, mapnumber): match = Match.query.get_or_404(matchid) match_api_check(request, match) map_stats = match.map_stats.filter_by(map_number=mapnumber).first() if map_stats: t1 = as_int(request.values.get('team1score')) t2 = as_int(request.values.get('team2score')) if t1 != -1 and t2 != -1: map_stats.team1_score = t1 map_stats.team2_score = t2 db.session.commit() else: return 'Failed to find map stats object', 400 return 'Success'
def matches_user(userid): user = User.query.get_or_404(userid) page = util.as_int(request.values.get('page'), on_fail=1) matches = user.matches.order_by(-Match.id).paginate(page, 20) is_owner = (g.user is not None) and (userid == g.user.id) return render_template('matches.html', user=g.user, matches=matches, my_matches=is_owner, all_matches=False, match_owner=user, page=page)
def seasons(): page = util.as_int(request.values.get('page'), on_fail=1) seasons = Season.query.order_by(-Season.id).paginate(page, 20) return render_template('seasons.html', user=g.user, seasons=seasons, my_seasons=False, all_seasons=True, page=page)
def matches(): page = util.as_int(request.values.get('page'), on_fail=1) matches = Match.query.order_by(-Match.id).filter_by( cancelled=False).paginate(page, 20) return render_template('matches.html', matches=matches, my_matches=False, all_matches=True, page=page)
def parse_rule(s: str) -> Tuple[int, Rule]: l = s.split(':') final = None rule_id = int(l[0]) ored = [s.strip() for s in l[1].split('|')] if len(ored) == 1 and ored[0][0] == '"': final = ored[0][1] anded = [] else: anded = [as_int(o.split(' ')) for o in ored] return rule_id, (anded, final)
def seasons_user(userid): user = User.query.get_or_404(userid) page = util.as_int(request.values.get('page'), on_fail=1) seasons = user.seasons.order_by(-Season.id).paginate(page, 20) is_owner = (g.user is not None) and (userid == g.user.id) app.logger.info('User is {}'.format(g.user)) return render_template('seasons.html', user=g.user, seasons=seasons, my_seasons=is_owner, all_matches=False, season_owner=user, page=page)
def season_matches(seasonid): season_info = Season.query.get_or_404(seasonid) page = util.as_int(request.values.get('page'), on_fail=1) matches = Match.query.order_by(-Match.id).filter_by( season_id=seasonid, cancelled=False).paginate(page, 20) return render_template('matches.html', user=g.user, matches=matches, season_matches=True, all_matches=False, page=page, season=season_info)
def part1(data: str, iterations: int) -> int: numbers = as_int(data.split(',')) indices = {n: i for i, n in enumerate(numbers[:-1])} last = numbers[-1] for it in range(len(numbers) - 1, iterations - 1): # print('Turn %d, spoke %d' % (it + 1, last)) if last in indices: previous = indices[last] indices[last] = it last = it - previous else: indices[last] = it last = 0 return last
def __init__(self, data: List[str]): self._fields: Dict[str, Set[int]] = {} i = 0 # fields for i in range(len(data)): line = data[i] if line == '': break m = re.match(self.FIELD_RE, line) if not m: raise RuntimeError self._fields[m.group(1)] = \ set(range(int(m.group(2)), int(m.group(3)) + 1)) | \ set(range(int(m.group(4)), int(m.group(5)) + 1)) # your ticket assert data[i + 1] == 'your ticket:' self._your: Ticket = Ticket(as_int(data[i + 2].split(','))) # nearby tickets assert data[i + 4] == 'nearby tickets:' self._nearby: List[Ticket] = [] for i in range(i + 5, len(data)): line = data[i] self._nearby.append(Ticket(as_int(line.split(','))))
def teams_user(userid): user = User.query.get_or_404(userid) page = util.as_int(request.values.get('page'), on_fail=1) json_data = util.as_int(request.values.get('json'), on_fail=0) if json_data: teams_dict = {} for team in user.teams: team_dict = {} team_dict['name'] = team.name team_dict['tag'] = team.tag team_dict['flag'] = team.flag team_dict['logo'] = team.logo team_dict['players'] = filter(lambda x: bool(x), team.auths) teams_dict[team.id] = team_dict return jsonify(teams_dict) else: # Render teams page my_teams = (g.user is not None and userid == g.user.id) teams = user.teams.paginate(page, 20) return render_template( 'teams.html', user=g.user, teams=teams, my_teams=my_teams, page=page, owner=user)
continue v = reverse_paths[k] if len(v) > 1: newly_processed = set() partition_by_src(k, reverse_paths, newly_processed) processed |= newly_processed cnt = count_paths(reverse_paths, max(newly_processed), min(newly_processed)) total *= cnt else: processed.add(k) return total if __name__ == "__main__": joltages = sorted(as_int(load('day10-test.txt'))) ratings = rating_for(joltages) assert 1 * ratings[1] + 3 * ratings[3] == 22 paths = paths_for(joltages) assert paths == 8 joltages = sorted(as_int(load('day10-test2.txt'))) ratings = rating_for(joltages) assert ratings[1] * ratings[3] == 220 paths = paths_for(joltages) assert paths == 19208 joltages = sorted(as_int(load('day10.txt'))) ratings = rating_for(joltages) assert ratings[1] * ratings[3] == 2244 print("Rating: %d" % (ratings[1] * ratings[3]))
class Team(db.Model): MAXPLAYERS = util.as_int(app.config['TEAM_MAXPLAYERS'], on_fail=7) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) name = db.Column(db.String(40)) tag = db.Column(db.String(40), default='') flag = db.Column(db.String(4), default='') logo = db.Column(db.String(10), default='') auths = db.Column(db.PickleType) public_team = db.Column(db.Boolean, index=True) preferred_names = db.Column(db.PickleType) @staticmethod def create(user, name, tag, flag, logo, auths, public_team=False, preferred_names=None): rv = Team() rv.user_id = user.id rv.set_data(name, tag, flag, logo, auths, (public_team and user.admin), preferred_names) db.session.add(rv) return rv def set_data(self, name, tag, flag, logo, auths, public_team, preferred_names=None): self.name = name self.tag = tag self.flag = flag.lower() if flag else '' self.logo = logo self.auths = auths self.public_team = public_team self.preferred_names = preferred_names def can_edit(self, user): if not user: return False if self.user_id == user.id: return True if user.super_admin: return True return False def get_players(self): results = [] for steam64 in self.auths: if steam64: name = get_steam_name(steam64) if not name: name = '' results.append((steam64, name)) return results def can_delete(self, user): if not self.can_edit(user): return False return self.get_recent_matches().count() == 0 def get_recent_matches(self, limit=10): if self.public_team: matches = Match.query.order_by(-Match.id).limit(100).from_self() else: owner = User.query.get_or_404(self.user_id) matches = owner.matches recent_matches = matches.filter( ((Match.team1_id == self.id) | (Match.team2_id == self.id)) & (Match.cancelled == False) & (Match.start_time != None) # noqa: E712 ).order_by(-Match.id).limit(5) if recent_matches is None: return [] else: return recent_matches def get_vs_match_result(self, match_id): other_team = None my_score = 0 other_team_score = 0 match = Match.query.get(match_id) if match.team1_id == self.id: my_score = match.team1_score other_team_score = match.team2_score other_team = Team.query.get(match.team2_id) else: my_score = match.team2_score other_team_score = match.team1_score other_team = Team.query.get(match.team1_id) # for a bo1 replace series score with the map score if match.max_maps == 1: mapstat = match.map_stats.first() if mapstat: if match.team1_id == self.id: my_score = mapstat.team1_score other_team_score = mapstat.team2_score else: my_score = mapstat.team2_score other_team_score = mapstat.team1_score if match.live(): return 'Live, {}:{} vs {}'.format(my_score, other_team_score, other_team.name) if my_score < other_team_score: return 'Lost {}:{} vs {}'.format(my_score, other_team_score, other_team.name) elif my_score > other_team_score: return 'Won {}:{} vs {}'.format(my_score, other_team_score, other_team.name) else: return 'Tied {}:{} vs {}'.format(other_team_score, my_score, other_team.name) def get_flag_html(self, scale=1.0): # flags are expected to be 32x21 width = int(round(32.0 * scale)) height = int(round(21.0 * scale)) html = '<img src="{}" width="{}" height="{}">' output = html.format(countries.get_flag_img_path(self.flag), width, height) return Markup(output) def get_logo_html(self, scale=1.0): if logos.has_logo(self.logo): width = int(round(32.0 * scale)) height = int(round(32.0 * scale)) html = ('<img src="{}" width="{}" height="{}">') return Markup( html.format(logos.get_logo_img(self.logo), width, height)) else: #app.logger.info("Looked for {} but found nothing.".format(self.logo)) return '' def get_url(self): return url_for('team.team', teamid=self.id) def get_name_url_html(self): return Markup('<a href="{}">{}</a>'.format(self.get_url(), self.name)) def get_logo_or_flag_html(self, scale=1.0, other_team=None): if logos.has_logo(self.logo): return self.get_logo_html(scale) else: return self.get_flag_html(scale) def __repr__(self): return 'Team(id={}, user_id={}, name={}, flag={}, logo={}, public={})'.format( self.id, self.user_id, self.name, self.flag, self.logo, self.public_team)
def teams_user(userid): user = User.query.get_or_404(userid) page = util.as_int(request.values.get('page'), on_fail=1) my_teams = (g.user is not None and userid == g.user.id) teams = user.teams.paginate(page, 20) return render_template('teams.html', user=g.user, teams=teams, my_teams=my_teams, page=page, owner=user)
def test_as_int(self): self.assertEqual(util.as_int('abcdef'), 0) self.assertEqual(util.as_int('3'), 3)
def match_map_update_player(matchid, mapnumber, steamid64): match = Match.query.get_or_404(matchid) api_key = request.values.get('key') if match.api_key != api_key: return 'Wrong API key', 400 map_stats = match.map_stats.filter_by(map_number=mapnumber).first() if map_stats: player_stats = PlayerStats.get_or_create(matchid, mapnumber, steamid64) if player_stats: player_stats.name = request.values.get('name') team = request.values.get('team') if team == 'team1': player_stats.team_id = match.team1_id elif team == 'team2': player_stats.team_id = match.team2_id player_stats.kills = as_int(request.values.get('kills')) player_stats.assists = as_int(request.values.get('assists')) player_stats.deaths = as_int(request.values.get('deaths')) player_stats.flashbang_assists = as_int( request.values.get('flashbang_assists')) player_stats.teamkills = as_int(request.values.get('teamkills')) player_stats.suicides = as_int(request.values.get('suicides')) player_stats.damage = as_int(request.values.get('damage')) player_stats.headshot_kills = as_int( request.values.get('headshot_kills')) player_stats.roundsplayed = as_int( request.values.get('roundsplayed')) player_stats.bomb_plants = as_int( request.values.get('bomb_plants')) player_stats.bomb_defuses = as_int( request.values.get('bomb_defuses')) player_stats.k1 = as_int(request.values.get('1kill_rounds')) player_stats.k2 = as_int(request.values.get('2kill_rounds')) player_stats.k3 = as_int(request.values.get('3kill_rounds')) player_stats.k4 = as_int(request.values.get('4kill_rounds')) player_stats.k5 = as_int(request.values.get('5kill_rounds')) player_stats.v1 = as_int(request.values.get('v1')) player_stats.v2 = as_int(request.values.get('v2')) player_stats.v3 = as_int(request.values.get('v3')) player_stats.v4 = as_int(request.values.get('v4')) player_stats.v5 = as_int(request.values.get('v5')) player_stats.firstkill_t = as_int( request.values.get('firstkill_t')) player_stats.firstkill_ct = as_int( request.values.get('firstkill_ct')) player_stats.firstdeath_t = as_int( request.values.get('firstdeath_t')) player_stats.firstdeath_ct = as_int( request.values.get('firstdeath_ct')) db.session.commit() else: return 'Failed to find map stats object', 404 return 'Success'
n1, x = bus[0] n2 = 1 for b in bus[1:]: n1, a1 = n1 * n2, x n2, a2 = b _, m1, m2 = gcd_extended(n1, n2) x = a1 * m2 * n2 + a2 * m1 * n1 x = x % (n1 * n2) return x if __name__ == "__main__": data = load('day13.txt') earliest = int(data[0]) bus_ids = as_int(list(filter(lambda b: b != 'x', data[1].split(',')))) part1(bus_ids) print('--- Part 2 ---') # part2 and part2b are TOO SLOW # part2c is wrong for the actual puzzle input :-/ assert part2_crt('17,x,13,19') == 3417 assert part2_crt('67,7,59,61') == 754018 assert part2_crt('67,x,7,59,61') == 779210 assert part2_crt('67,7,x,59,61') == 1261476 assert part2_crt('7,13,x,x,59,x,31,19') == 1068781 assert part2_crt('1789,37,47,1889') == 1202161486 t = part2_crt(data[1]) assert t == 225850756401039
def matches(): page = util.as_int(request.values.get('page'), on_fail=1) matches = Match.query.order_by(-Match.id).filter_by( cancelled=False).paginate(page, 20) return render_template('matches.html', user=g.user, matches=matches, my_matches=False, all_matches=True, page=page)
def match_map_update_player(matchid, mapnumber, steamid64): match = Match.query.get_or_404(matchid) api_key = request.values.get('key') if match.api_key != api_key: return 'Wrong API key', 400 map_stats = match.map_stats.filter_by(map_number=mapnumber).first() if map_stats: player_stats = PlayerStats.get_or_create(matchid, mapnumber, steamid64) if player_stats: player_stats.name = request.values.get('name') team = request.values.get('team') if team == 'team1': player_stats.team_id = match.team1_id elif team == 'team2': player_stats.team_id = match.team2_id player_stats.kills = as_int(request.values.get('kills')) player_stats.assists = as_int(request.values.get('assists')) player_stats.deaths = as_int(request.values.get('deaths')) player_stats.flashbang_assists = as_int( request.values.get('flashbang_assists')) player_stats.teamkills = as_int(request.values.get('teamkills')) player_stats.suicides = as_int(request.values.get('suicides')) player_stats.damage = as_int(request.values.get('damage')) player_stats.headshot_kills = as_int( request.values.get('headshot_kills')) player_stats.roundsplayed = as_int( request.values.get('roundsplayed')) player_stats.bomb_plants = as_int( request.values.get('bomb_plants')) player_stats.bomb_defuses = as_int( request.values.get('bomb_defuses')) player_stats.k1 = as_int(request.values.get('1kill_rounds')) player_stats.k2 = as_int(request.values.get('2kill_rounds')) player_stats.k3 = as_int(request.values.get('3kill_rounds')) player_stats.k4 = as_int(request.values.get('4kill_rounds')) player_stats.k5 = as_int(request.values.get('5kill_rounds')) player_stats.v1 = as_int(request.values.get('v1')) player_stats.v2 = as_int(request.values.get('v2')) player_stats.v3 = as_int(request.values.get('v3')) player_stats.v4 = as_int(request.values.get('v4')) player_stats.v5 = as_int(request.values.get('v5')) player_stats.firstkill_t = as_int( request.values.get('firstkill_t')) player_stats.firstkill_ct = as_int( request.values.get('firstkill_ct')) player_stats.firstdeath_t = as_int( request.values.get('firstdeath_t')) player_stats.firstdeath_ct = as_int( request.values.get('firstdeath_ct')) db.session.commit() else: return 'Failed to find map stats object', 404 return 'Success'
return val def guess_loops(subject: int, needle: int) -> int: val = 1 loops = 0 while val != needle: val = (val * subject) % 20201227 loops += 1 return loops def encryption_key(card: Rfid, door: Rfid) -> int: card_key = transform(door[0], card[1]) door_key = transform(card[0], door[1]) assert card_key == door_key print(card_key) return card_key if __name__ == "__main__": assert guess_loops(7, 5764801) == 8 assert guess_loops(7, 17807724) == 11 assert encryption_key((5764801, 8), (17807724, 11)) == 14897079 pub_keys = as_int(load('day25.txt')) card_rfid = pub_keys[0], guess_loops(7, pub_keys[0]) door_rfid = pub_keys[1], guess_loops(7, pub_keys[1]) key = encryption_key(card_rfid, door_rfid) print('Encryption key is %d' % key)
sums = sums[min(preamble - 1, i - preamble + 1):] return -1 def find_weakness(data: List[int], idx: int) -> int: needle = data[idx] for i in range(idx - 1, 0, -1): offset = 0 summed = data[i] while offset == 0 or summed < needle: offset += 1 summed += data[i - offset] if summed == needle: sub_data = data[i - offset:i] return min(sub_data) + max(sub_data) return -1 if __name__ == "__main__": numbers = as_int(load('day9-test.txt')) index = analyse(numbers, 5) weakness = find_weakness(numbers, index) assert numbers[index] == 127 assert weakness == 62 numbers = as_int(load('day9.txt')) index = analyse(numbers) weakness = find_weakness(numbers, index) assert numbers[index] == 41682220 print('Missing sum is %d, weakness is %d' % (numbers[index], weakness))
def test_as_int(self): self.assertEqual(util.as_int('abcdef'), 0) self.assertEqual(util.as_int('3'), 3)