コード例 #1
0
import sys

from shared import get_separated_rows


def divide_two_divisibles(l):
    l = list(l)
    for left_cur_index, left_cur_val in enumerate(l):
        for right_cur_val in l[left_cur_index + 1:]:
            if max(left_cur_val, right_cur_val) % min(left_cur_val,
                                                      right_cur_val) == 0:
                return max(left_cur_val, right_cur_val) / min(
                    left_cur_val, right_cur_val)


if __name__ == '__main__':
    lines = get_separated_rows(sys.argv[1], sep='\t', func=lambda x: int(x))
    print(sum(divide_two_divisibles(l) for l in lines))
コード例 #2
0
ファイル: p11-hexgrid.py プロジェクト: jonmcoe/aoc2017
    'n': (0, 1, -1),
    's': (0, -1, 1),
    'ne': (1, 0, -1),
    'nw': (-1, 1, 0),
    'se': (1, -1, 0),
    'sw': (-1, 0, 1)
}


def calc_distance(score):
    return sum(abs(x) for x in score) // 2


def perform_move(score, direction_str):
    current_direction = TURN_TO_DIRECTION[direction_str]
    new_score = score
    for i, _ in enumerate(score):
        new_score[i] += current_direction[i]
    return new_score


if __name__ == '__main__':
    steps_taken = next(
        shared.get_separated_rows(sys.argv[1], func=lambda x: x.strip()))
    score = [0, 0, 0]
    max_distance = 0
    for st in steps_taken:
        score = perform_move(score, st)
        max_distance = max(max_distance, calc_distance(score))
    print(calc_distance(score))
    print(max_distance)
コード例 #3
0
ファイル: p06b-memorybanks.py プロジェクト: jonmcoe/aoc2017
import sys

from typing import Dict, Tuple

import shared


if __name__ == '__main__':
    banks = tuple(list(shared.get_separated_rows(sys.argv[1], sep='\t', func=lambda x: int(x)))[0])
    banks_seen = {}  # type: Dict[Tuple[int, ...], int]
    steps = 0
    while banks not in banks_seen:
        banks_seen[banks] = steps
        banks = shared.reallocate_banks(banks)
        steps += 1
    print(steps - banks_seen[banks])
コード例 #4
0
ファイル: p04a-passphrase.py プロジェクト: jonmcoe/aoc2017
import sys

import shared

if __name__ == '__main__':
    lines = shared.get_separated_rows(sys.argv[1], ' ',
                                      lambda x: x.replace('\n', ''))
    print(shared.count_with_all_unique_elements(lines))
コード例 #5
0
ファイル: p02a-checksum.py プロジェクト: jonmcoe/aoc2017
import sys

from shared import get_separated_rows

if __name__ == '__main__':
    print(
        sum(
            max(map(int, l)) - min(map(int, l))
            for l in get_separated_rows(sys.argv[1], sep='\t')))
コード例 #6
0
ファイル: p24-bridge.py プロジェクト: jonmcoe/aoc2017
bad_global_paths = []  # this is terrible


def make_all_paths(so_far, available_components):
    if len(so_far) < 2:
        previous_spot = (0, 0)
    else:
        previous_spot = so_far[-2]
    searching_for = sorted(so_far[-1], key=lambda x: x in previous_spot)[0]
    next_up_candidates = find_all_components_containing(
        searching_for, available_components)
    if not next_up_candidates:
        bad_global_paths.append(so_far)
    else:
        # TODO: fix this flatmap and kill the global paths. maybe use itertools and chain?
        return [
            make_all_paths(so_far + [candidate],
                           available_components - {candidate})
            for candidate in next_up_candidates
        ]


if __name__ == '__main__':
    all_components = set(
        tuple(t)
        for t in shared.get_separated_rows(sys.argv[1], '/', func=int))
    all_paths = make_all_paths([(0, 0)], all_components)
    print(max(sum(map(sum, p)) for p in bad_global_paths))
    best_path = max(bad_global_paths, key=lambda x: (len(x), sum(map(sum, x))))
    print(sum(map(sum, best_path)))
コード例 #7
0
ファイル: p10a-knothash.py プロジェクト: jonmcoe/aoc2017
import sys

import shared

if __name__ == '__main__':
    lengths = next(shared.get_separated_rows(sys.argv[1], func=int))
    kt = shared.KnotTwine(256)
    for length in lengths:
        kt.transform(length)
    print(kt.twine[0] * kt.twine[1])