def main(): if len(sys.argv) != 2: print('args: file_name') exit(1) print('loading data') fn = sys.argv[1] with open(fn) as f: lines = f.read().splitlines() activities = utils.parse_lines(lines) num = len(activities) print('data loaded. {} activities'.format(num)) utils.normalize_length(activities, 1, 400) tid_prefix = socket.gethostname() print('begin. prefix of tweet_id is {}'.format(tid_prefix)) t0 = time.time() c = 0 calcers_get = defaultdict(utils.Calcer) calcers_create = defaultdict(utils.Calcer) for ts, action, user_id, tlen in activities: if action == 'Retrieve': resp = requests.get(url_get, params={ 'user_id': user_id, 'limit': 50, 'pop': 0 }) respj = resp.json() utils.save_timer(calcers_get, respj['timer']) else: resp = requests.post(url_create, data={ 'tweet_id': tid_prefix + '-' + str(ts), 'user_id': user_id, 'ts': ts, 'content': utils.random_string(tlen) }) respj = resp.json() utils.save_timer(calcers_create, respj['timer']) pprint(respj) c += 1 if c % 100 == 0: print('progress:', time.time() - t0, c, c / (time.time() - t0)) t999 = time.time() duration = t999 - t0 print('finished. {:.2f}, {:.2f}/s, {:.5f}/req'.format( duration, num / duration, duration / num)) print('---timers of get---') utils.print_calcers(calcers_get) print('---timers of create---') utils.print_calcers(calcers_create)
def main(): with open("05/input.txt", encoding="UTF-8") as file: content = file.read() text_lines = content.split("\n") all_lines = [parse_lines(line) for line in text_lines] valid_lines = [ line for line in all_lines if is_horizontal_vertical_line(line) ] matrix = get_initial_matrix(valid_lines) process_horizontal_vertical(valid_lines, matrix) result = np.count_nonzero(matrix >= 2) print(result)
import torch.nn as nn import torch.nn.functional as F import torch.optim as optim from torch.utils.data import DataLoader, Dataset from torchtext.data import get_tokenizer from gensim.models import Word2Vec from utils import parse_lines from dataset import NGramDataset from NeuralLM import NeuralLM from LstmLM import LstmLM EMBEDDING_DIM = 300 CONTEXT_SIZE = 5 list_list_words = parse_lines('data/shakespeare.txt') model = Word2Vec(list_list_words, size=EMBEDDING_DIM, window=CONTEXT_SIZE, min_count=2, workers=16) words = model.wv.index2word weights = torch.FloatTensor(model.wv.vectors) embedding = nn.Embedding.from_pretrained(weights) filtered_data = [ word for line in list_list_words for word in line if word in words ]
def test_first(self): data = utils.parse_lines("test_input.txt", int) self.assertEqual(day01.count_larger(data), 7)
def test_first(self): template, pairs = utils.parse_lines("test_input.txt") self.assertEqual(day14.puzzle(template, pairs, 10), 1588)
def test_first(self): in_data = utils.parse_lines("test_input.txt", str) self.assertEqual(day08.puzzle1(in_data), 26)
def test_first(self): dots, instructions = utils.parse_lines("test_input.txt") self.assertEqual(day13.puzzle(dots, instructions, 1), 17)
def test_first(self): in_data = utils.parse_lines("test_input.txt", int) self.assertEqual(day09.puzzle1(in_data), 15)
from string import ascii_uppercase from utils import (parse_lines, triangular, capacity) letters_positions = { letter: position for position, letter in enumerate(ascii_uppercase, start=1) } def triangular_word(word: str) -> bool: word_value = sum(letters_positions[letter] for letter in word) return triangular(word_value) with open('words.txt') as words_file: words = list(parse_lines(words_file)) triangular_words = filter(triangular_word, words) assert triangular_word('SKY') assert capacity(triangular_words) == 162
self.idx_to_word = {} for idx,word in enumerate(vocab): self.word_to_idx[word] = idx self.idx_to_word[idx] = word def __len__(self): return self.length-self.context_size def __getitem__(self, idx): if torch.is_tensor(idx): idx = idx.tolist() context_idx = [self.word_to_idx[self.data[i+idx]] for i in range(self.context_size)] label_idx = self.word_to_idx[self.data[idx+self.context_size]] return context_idx, label_idx # %% if __name__ == "__main__": context_size = 5 lines = parse_lines('data/shakespeare.txt') word_data = [word for line in lines for word in line] vocab = list(set(word_data)) dataset = NGramDataset(word_data, vocab, context_size) dataloader = DataLoader(dataset, batch_size=1) for idx, batch in enumerate(dataloader): context = batch[0] label = batch[1] print(f"context : {context} \nlabel : {label}") break
def test_first(self): data = utils.parse_lines("test_input.txt", str) self.assertEqual(day05.puzzle(data), 5)
def test_second(self): data = utils.parse_lines("test_input.txt", str) self.assertEqual(day05.puzzle(data, True), 12)
def test_second(self): test = [utils.parse_lines(f"test_input{i}.txt") for i in range(1, 4)] self.assertEqual(day12.puzzle(test[0], True), 36) self.assertEqual(day12.puzzle(test[1], True), 103) self.assertEqual(day12.puzzle(test[2], True), 3509)
def test_first(self): test = [utils.parse_lines(f"test_input{i}.txt") for i in range(1, 4)] self.assertEqual(day12.puzzle(test[0]), 10) self.assertEqual(day12.puzzle(test[1]), 19) self.assertEqual(day12.puzzle(test[2]), 226)
def puzzle(in_dots: list[str], in_instructions: list[str], fold_no: int): init_dots = { Point(int(x), int(y)) for x, y, in (line.split(",") for line in in_dots.split("\n")) } paper = Paper(init_dots) for ins_no, instruction in enumerate(in_instructions.split("\n")): instruction = instruction.split("fold along ")[-1] axis, value = instruction.split("=") value = int(value) paper.fold(axis, value) if (ins_no + 1) == fold_no: return len(paper.dots) print(paper) if __name__ == "__main__": in_fpath = input("Enter file path containing dots and instructions: ") dots, instructions = utils.parse_lines(in_fpath) # Puzzle 1 print(f"The number of dot after one fold is: {puzzle(dots, instructions, 1)}") # Puzzle 2 print(f"The secret code is: \n{puzzle(dots, instructions, None)}")
from string import ascii_uppercase from typing import Iterable from utils import parse_lines def alphabetical_value(word: str) -> int: return sum(index + 1 for index in map(ascii_uppercase.index, word)) def total_names_score(names: Iterable[str]) -> int: sorted_names = sorted(names) return sum(index * alphabetical_value(name) for index, name in enumerate(sorted_names, start=1)) with open('names.txt') as names_file: names = list(parse_lines(names_file)) assert alphabetical_value('COLIN') == 53 assert total_names_score(names) == 871_198_282
def test_second(self): in_data = utils.parse_lines("test_input.txt", int) self.assertEqual(day09.puzzle2(in_data), 1134)
def count_overlapping(self) -> int: """Count number of overlapping vents""" return sum(1 for _, count in self.points.items() if count >= 2) def puzzle(in_coords, diag: bool = False): vent_grid = Grid() # Add lines for line in in_coords: line = Line.from_string(line) try: vent_grid.add_line(line, diag) except ValueError: pass # Count overlap return vent_grid.count_overlapping() if __name__ == "__main__": data_fpath = input("Enter the filepath containing the location of vents: ") vent_coords = utils.parse_lines(data_fpath, str) print(f"Number of overlapping points: {puzzle(vent_coords)}") print( f"Number of overlapping vents including diagonals: {puzzle(vent_coords, True)}" )
def test_second(self): dots, instructions = utils.parse_lines("test_input.txt") print(day13.puzzle(dots, instructions, None))
def test_first(self): in_data = utils.parse_lines("test_input.txt", int) self.assertEqual(day11.puzzle1(in_data), 204) self.assertEqual(day11.puzzle1(in_data, 100), 1656)
def test_second(self): in_data = utils.parse_lines("test_input.txt", str) self.assertEqual(day08.puzzle2(in_data), 61229)
for char in self.frequency: new_freq[char[0] + self.rules[char]] += self.frequency[char] new_freq[self.rules[char] + char[1]] += self.frequency[char] self.frequency = new_freq def get_common(self): return max(self.frequency.values()), min(self.frequency.values()) def puzzle(in_template: str, in_pairs: list[str], steps: int) -> int: polymer = Polymer(in_template, in_pairs) polymer.build(steps) mc, lc = polymer.get_common() return mc - lc if __name__ == "__main__": in_fpath = input("Enter file containing manual: ") template, pairs = utils.parse_lines(in_fpath) # Puzzle 1 print( f"The difference between most and least common element is: {puzzle(template, pairs, 10)}" ) # Puzzle 2 print( f"The difference between most and least common element is: {puzzle(template, pairs, 40)}" )
return { decoded_pattern: value for value, decoded_pattern in enumerate(reverse_segmap) } def puzzle1(in_data: str) -> int: return sum(count_unique(line.strip("\n").split(" | ")[-1]) for line in in_data) def puzzle2(in_data: str) -> int: total = 0 for line in in_data: pattern, digits = map(str.split, line.split(" | ")) decoded_pattern = decode_pattern(pattern) total += int( "".join(map(str, [decoded_pattern[frozenset(digit)] for digit in digits])) ) return total if __name__ == "__main__": in_fpath = input("Enter the file path containing signal output: ") in_data = utils.parse_lines(in_fpath, str) # Puzzle 1 print(f"Number of times unique digits appear: {puzzle1(in_data)}") # Puzzle 2 print(f"Number of times unique digits appear: {puzzle2(in_data)}")
import utils def count_larger(in_data: list[int]) -> int: return sum(in_data[idx - 1] < in_data[idx] for idx in range(1, len(in_data))) def compute_window(in_data: list[int], window: int = 3): return [ sum(in_data[idx : idx + window]) for idx in range(len(in_data) - window + 1) ] if __name__ == "__main__": data_fpath = input("Enter the path to the file: ") data_content = utils.parse_lines(data_fpath, int) # Puzzle 1 print(f"Number of larger elements in list: {count_larger(data_content)}") # Puzzle 2 windowed_data = compute_window(data_content) print(f"Number of larger elements in window: {count_larger(windowed_data)}")
def test_second(self): template, pairs = utils.parse_lines("test_input.txt") self.assertEqual(day14.puzzle(template, pairs, 40), 2188189693529)
def test_second(self): data = utils.parse_lines("test_input.txt", int) windowed_data = day01.compute_window(data) self.assertEqual(day01.count_larger(windowed_data), 5)