예제 #1
0
def run_solution(args) -> None:
    year = args.year
    day = args.day
    if year is None:
        year = get_latest_year()
    solution_module_path = f'adventofcode.solutions.{get_year_id(year)}.{get_day_id(day)}'
    solution_module = importlib.import_module(solution_module_path)
    data = get_input(year, day)
    print(
        f'Running solution for year {highlight(year)}, day {highlight(day)}.')
    print()
    answer1, answer2 = solution_module.run(data)
    print()
    print(highlight('Solutions found.', color='g'))
    print()
    print('Answer to puzzle 1:', highlight(answer1, color='g'))
    print('Answer to puzzle 2:', highlight(answer2, color='g'))
예제 #2
0
def solution_2(records_by_guard_id: DefaultDict[int, List[int]]) -> int:
    # restructure records_by_guard_id into a flat list of (guard_id, minute, sleep_count) tuples
    combined_records = chain(
        *([(guard_id, minute, record) for minute, record in enumerate(records)]
          for guard_id, records in records_by_guard_id.items()))
    sleepiest_minute_guard_id, sleepiest_minute, sleep_count = max(
        combined_records, key=lambda x: x[2])
    solution = sleepiest_minute_guard_id * sleepiest_minute
    print(
        f'Guard #{highlight(sleepiest_minute_guard_id)} spent minute 00:{highlight(sleepiest_minute)}',
        'sleeping more than any other guard spent any other single minute sleeping.',
        'They were asleep during that minute on', highlight(sleep_count),
        'different days.')
    print(
        f"{sleepiest_minute_guard_id} * {sleepiest_minute} = {highlight(solution, color='g')}"
    )
    return solution
예제 #3
0
def solution_1(records_by_guard_id: DefaultDict[int, List[int]]) -> int:
    id_of_guard_that_sleeps_the_most = max(
        records_by_guard_id.items(),
        key=lambda indexed_tuple: sum(indexed_tuple[1]))[0]
    total_minutes_slept_by_that_guard = sum(
        records_by_guard_id[id_of_guard_that_sleeps_the_most])
    indexed_minute_most_slept_by_that_guard = max(
        enumerate(records_by_guard_id[id_of_guard_that_sleeps_the_most]),
        key=lambda indexed_tuple: indexed_tuple[1])
    solution = id_of_guard_that_sleeps_the_most * indexed_minute_most_slept_by_that_guard[
        0]
    print(
        f'Guard #{highlight(id_of_guard_that_sleeps_the_most)} slept the most minutes ({highlight(total_minutes_slept_by_that_guard)}).',
        f'They spent minute 00:{highlight(indexed_minute_most_slept_by_that_guard[0])}',
        'sleeping most often--they were asleep during that minute on',
        highlight(indexed_minute_most_slept_by_that_guard[1]),
        'different days.')
    print(
        f"{id_of_guard_that_sleeps_the_most} * {indexed_minute_most_slept_by_that_guard[0]} = {highlight(solution, color='g')}"
    )
    return solution
예제 #4
0
def run_make_new_year(args) -> None:
  year = args.year
  if year is None:
    latest_year = get_latest_year()
    year = date.today().year if latest_year is None else (latest_year + 1)
  elif year < 2000:
    raise ValueError('Year must not be shorthand. E.g. "2018", not "18".')

  paths = make_new_year(year)

  print(highlight('Success.', color='g'))
  if shutil.which('tree') is not None:
    trees = '\n'.join(
      subprocess.check_output(['tree', '-C', '--noreport', path]).decode('utf8')
      for path in paths
    )
    print('Created the following directories and files:')
    print(trees)
  else:
    solutions_year_dir_path, tests_year_dir_path = paths
    print(f'Created solution directory {highlight(solutions_year_dir_path)} and starter solution files.')
    print(f'Created test directory {highlight(tests_year_dir_path)} and starter test files.')
예제 #5
0
def solution_2(fuel_grid: SummedAreaFuelGrid) -> str:
  print('Determining region with overall max power... ')
  power, x, y, size = max(fuel_grid.max_region_power(size) + (size,) for size in range(1, 301))
  print()
  print('Max power overall:', highlight(power, color='g'), 'for region of size', highlight(size))
  return f'{x + 1},{y + 1},{size}'
예제 #6
0
def solution_1(fuel_grid: SummedAreaFuelGrid) -> str:
  power, x, y = fuel_grid.max_region_power()
  print('Max power for regions of size 3:', highlight(power), end='\n\n')
  return f'{x + 1},{y + 1}'