#!/bin/python3 import sys,time,re,os,copy from pprint import pprint sys.path.insert(0, '../../') from fred import * from itertools import chain start_time = time.time() if sys.argv[1] == 'test': input_f = 'test' elif sys.argv[1] == 'input': input_f = 'input' else: print('No argv provided') exit() ######################################### # # # Part 1 # # # ######################################### def part1(): score = 0 lines = loadFile(input_f) ranges = [] fruits = [] split = False for l in lines: #split the input into two groups. I use a boolean, so i don't have to do a special comparison on each line (eg. check for -) if l == '': split = True elif not split: ranges.append(l) else: fruits.append(l) for f in list2int(fruits): found = False while not found: for r in ranges: [start,end] = list2int(r.split('-')) if start <= f <= end: found = True score += 1 break break return score start_time = time.time() p1 = part1() print('Part 1:',p1, '\t\t', round((time.time() - start_time)*1000), 'ms') ######################################### # # # Part 2 # # # ######################################### def part2(): score = 0 lines = loadFile(input_f) ranges = [] for l in lines: if l == '': break ranges.append(l) total_ranges = [] for r in ranges: start,end = list2int(r.split('-')) total_ranges.append([start,end]) total_ranges = sorted(total_ranges) new_range = [] c = total_ranges[0] #current for x in range(1,len(total_ranges)): n = total_ranges[x] #next if c[1] >= n[0]: c = [c[0], max(c[1],n[1])] else: new_range.append(c) c = n new_range.append(c) for x in new_range: score += (x[1]-x[0]+1) return score start_time = time.time() p2 = part2() print('Part 2:',p2, '', round((time.time() - start_time)*1000), 'ms')