aoc-2018-python

Advent of Code 2018 Solutions in Python
git clone https://git.sinitax.com/sinitax/aoc-2018-python
Log | Files | Refs | README | sfeed.txt

aoc.py (999B)


      1import sys, os
      2
      3debug_lvl = os.getenv("AOC_DEBUG")
      4debug_lvl = int(debug_lvl) if debug_lvl is not None else 0
      5
      6input_name = os.getenv("AOC_INPUT")
      7input_name = input_name if input_name is not None else "input"
      8
      9data = open(input_name).read().strip("\n")
     10
     11def debug(*args, **kwargs):
     12    if debug_lvl:
     13        print(*args, **kwargs, file=sys.stderr)
     14
     15def run(solve1, solve2, sols=[None, None]):
     16    if len(sys.argv) <= 1:
     17        sys.exit(0)
     18    part = int(sys.argv[1])
     19    if part == 1:
     20        answer = solve1(sys.argv[2:])
     21        print(answer)
     22        if sols[part - 1] is not None:
     23            assert(answer == sols[part - 1])
     24        else:
     25            print("warn: no solution available", file=sys.stderr)
     26    elif part == 2:
     27        answer = solve2(sys.argv[2:])
     28        print(answer)
     29        if sols[part - 1] is not None:
     30            assert(answer == sols[part - 1])
     31        else:
     32            print("warn: no solution available", file=sys.stderr)
     33    else:
     34        assert(False) # bad part
     35