def tickets_from_file(filepath='input.txt'): tickets = [] lines = get_lines_from_file(filepath, True) for line in lines: if line.strip(): tickets.append(line.strip()) return tickets
def day_seven(): lines = get_lines_from_file('day7/day7.txt', True) rules = get_rules(lines) # Step 1 # How many bag colors can eventually contain at least one shiny gold bag? # Test Data = 4 print(part_a(rules)) print(part_b(rules))
def get_passwords_from_file(filepath='day1.txt'): passwords = [] file_lines = get_lines_from_file(filepath) for line in file_lines: try: validator, password = split(':', line) except ValueError: print('Cannot find : in ', line) entry = {'password': password, 'validator': validator} passwords.append(entry) return passwords
def answers_from_file_preserve_people(filepath='day6.txt'): groups = [] lines = get_lines_from_file(filepath, True) entry = [] for line in lines: if line.strip(): entry.append(line.strip()) else: groups.append(entry) entry = [] return groups
def passports_from_file(filepath='day4.txt'): passports = [] lines = get_lines_from_file(filepath, True) entry = {} for line in lines: if line.strip(): # split default is a ' ' for kv in line.split(): if ':' in kv: k, v = kv.split(':') entry[k] = v else: passports.append(entry) entry = {} return passports
def test_get_bags(self): lines = get_lines_from_file('day7test.txt', True) rules = get_rules(lines) # rules = get_bag_data(lines) test_rules = { 'light red': { 'bright white': '1', 'muted yellow': '2' }, 'dark orange': { 'bright white': '3', 'muted yellow': '4' }, 'bright white': { 'shiny gold': '1' }, 'muted yellow': { 'shiny gold': '2', 'faded blue': '9' }, 'shiny gold': { 'dark olive': '1', 'vibrant plum': '2' }, 'dark olive': { 'faded blue': '3', 'dotted black': '4' }, 'vibrant plum': { 'faded blue': '5', 'dotted black': '6' }, 'faded blue': {}, 'dotted black': {} } self.assertEqual(rules, test_rules)
def test_part_a(self): lines = get_lines_from_file('day7test.txt', True) rules = get_rules(lines) matches = part_a(rules) self.assertEqual(4, matches)
def get_numbers_from_file(filepath='day1.txt'): numbers = [] file_lines = get_lines_from_file(filepath) for line in file_lines: numbers.append(int(line)) return numbers
def get_trees_from_file(filepath='day3/day3.txt'): trees = get_lines_from_file(filepath) return trees