from data import DATA from seat import Seat cleaned_data = DATA.splitlines() if __name__ == "__main__": seats = [Seat.from_string(data) for data in cleaned_data] max_id = max([seat.id for seat in seats]) print("PART ONE\n=======") print('Greatest passport ID: {}'.format(max_id)) sorted_ids = [seat.id for seat in sorted(seats, key=lambda seat: seat.id)] expected_id = None for id in sorted_ids: if expected_id is None: expected_id = id + 1 continue if not id == expected_id: break expected_id = id + 1 print('Missing Seat ID: {}'.format(expected_id)) print("\nPART TWO\n=======")
def parse_mask(mask_line): mask_string = mask_line.split(' = ')[-1] or_mask = int(mask_string.replace('X', '0'), 2) and_mask = int(mask_string.replace('X', '1'), 2) return or_mask, and_mask def parse_instruction(input_line): mem_string, val_string = input_line.split(' = ') address = int(mem_string[4:-1]) value = int(val_string) return address, value if __name__ == "__main__": mem = {} or_mask = 0 and_mask = 0 for line in DATA.splitlines(): if line[:4] == 'mask': or_mask, and_mask = parse_mask(line) continue address, value = parse_instruction(line) mem[address] = (value | or_mask) & and_mask mem_sum = sum([val for val in mem.values()]) print(mem_sum)
from data import DATA from pprint import pprint rules = DATA.splitlines() def process_bags(rules): bags = {} for rule in rules: bag, contents = rule.split(' contain ') bag = bag.replace(' bags', '') contents = contents[:-1] # remove period if bag not in bags: bags[bag] = {} if contents == 'no other bags': continue for content in contents.split(', '): content_split = content.split() count = int(content_split[0]) content_bag = ' '.join(content_split[1:3]) bags[bag][content_bag] = count return bags def search(bags, search): valid_bags = []
from data import DATA from floor import TileFloor instructions = DATA.splitlines() if __name__ == "__main__": floor = TileFloor() for instruction in instructions: floor.execute(instruction) print(floor.count_black)
turns -= 1 def go_to_waypoint(self, times): self.location.east += self.waypoint.east * times self.location.north += self.waypoint.north * times def execute(self, instruction): action, value = self.decode_instruction(instruction) if action in self.HEADINGS: self.move_waypoint(action, value) elif action in ['L', 'R']: self.rotate_waypoint(action, value) elif action == 'F': self.go_to_waypoint(value) if __name__ == "__main__": ship = Ship() for instruction in DATA.splitlines(): ship.execute(instruction) print('PART ONE\n=======') print('Ship Manhattan distance: {}'.format(ship.manhattan)) w_ship = WaypointShip() for instruction in DATA.splitlines(): w_ship.execute(instruction) print('\nPART TWO\n=======') print('WaypointShip Manhattan distance: {}'.format(w_ship.manhattan))