diff options
| author | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-11 07:06:18 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gusso230@student.liu.se> | 2019-12-11 07:06:18 +0100 |
| commit | bef82d77ceee6f34b9e50baf61461e7f3d5a6d6d (patch) | |
| tree | b4c112ed3c860609e3ae10fe67114bd30ab57055 | |
| parent | 7afab1ad3892084ee079fe5ef45edc7fb452af06 (diff) | |
| download | aoc-bef82d77ceee6f34b9e50baf61461e7f3d5a6d6d.tar.gz | |
Refactor and add timing/profiling day 11
| -rw-r--r-- | solutions/py/d11.py | 151 | ||||
| -rw-r--r-- | solutions/py/main.py | 3 |
2 files changed, 100 insertions, 54 deletions
diff --git a/solutions/py/d11.py b/solutions/py/d11.py index 88d942f..c97f3a4 100644 --- a/solutions/py/d11.py +++ b/solutions/py/d11.py @@ -1,23 +1,6 @@ import intcode import time -_input = open("../input/11","r").readlines() - -program = [int(x) for x in _input[0].split(",")] -x, y = 0, 0 -direction = 0 -# 0 is up -# 1 is left -# 2 is down -# 3 is right -# 4 is up (again) -# turning left is direction += 1 -# turning right is direction -= 1 -# direction is direction % 4 -painted = 0 -colors = {(0,0): 1} # (x,y): 1/0 (1 = white, 0 = black) - -got_color = False def draw(colors, ship, direction): min_x, max_x, min_y, max_y = 0, 0, 0, 0 @@ -35,45 +18,107 @@ def draw(colors, ship, direction): max_x = max(max_x, color[0]) min_y = min(min_y, color[1]) max_y = max(max_y, color[1]) - for y in range(min_y-5, max_y+5): - for x in range(min_x-5, max_x+5): + s = "" + for y in range(min_y-1, max_y+2): + s += "\n" + for x in range(min_x-1, max_x+1): if (x,y) == ship: - print(ship_c, end="") + s += ship_c elif (x,y) in colors: c = colors[(x,y)] - print("\u2588" if c == 1 else ".", end="") + s += "\u2588" if c == 1 else " " else: - print(" ", end="") - print("") + s += "." + return s + +def pt1(input): + program = [int(x) for x in input[0].split(",")] + x=y = 0 + direction = 0 + # 0 is up + # 1 is left + # 2 is down + # 3 is right + # 4 is up (again) + # turning left is direction += 1 + # turning right is direction -= 1 + # direction is direction % 4 + colors = {} # (x,y): 1/0 (1 = white, 0 = black) + + got_color = False + c = intcode.Computer(program) + while c.memory[c.pointer] != 99: + # input + #print(x, y) + #draw(colors, (x,y), direction % 4) + #input() + #time.sleep(0.1) + c.input = colors.get((x, y), 0) + c.step() + if c.output is not None: + if not got_color: + colors[(x, y)] = c.output + c.output = None + got_color = True + elif got_color: + direction += (1 if c.output == 0 else -1) + dir = direction % 4 + if dir == 0: + y -= 1 + elif dir == 1: + x -= 1 + elif dir == 2: + y += 1 + elif dir == 3: + x += 1 + got_color = False + c.output = None + return len(colors) + +def pt2(input): + program = [int(x) for x in input[0].split(",")] + x=y = 0 + direction = 0 + # 0 is up + # 1 is left + # 2 is down + # 3 is right + # 4 is up (again) + # turning left is direction += 1 + # turning right is direction -= 1 + # direction is direction % 4 + colors = {(0,0): 1} # (x,y): 1/0 (1 = white, 0 = black) + + got_color = False + c = intcode.Computer(program) + while c.memory[c.pointer] != 99: + c.input = colors.get((x, y), 0) + c.step() + if c.output is not None: + if not got_color: + colors[(x, y)] = c.output + c.output = None + got_color = True + elif got_color: + direction += (1 if c.output == 0 else -1) + dir = direction % 4 + if dir == 0: + y -= 1 + elif dir == 1: + x -= 1 + elif dir == 2: + y += 1 + elif dir == 3: + x += 1 + got_color = False + c.output = None + return draw(colors, (0,0), 0) +if __name__ == "__main__": + import cProfile -c = intcode.Computer(program) -while c.memory[c.pointer] != 99: - # input - #print(x, y) - #draw(colors, (x,y), direction % 4) - #input() - #time.sleep(0.1) - c.input = colors.get((x, y), 0) - c.step() - if c.output is not None: - if not got_color: - colors[(x, y)] = c.output - c.output = None - got_color = True - elif got_color: - direction += (1 if c.output == 0 else -1) - dir = direction % 4 - if dir == 0: - y -= 1 - elif dir == 1: - x -= 1 - elif dir == 2: - y += 1 - elif dir == 3: - x += 1 - got_color = False - c.output = None -draw(colors, (0,0), 0) -print(colors) -print(len(colors)) + input = open("../input/11", "r").readlines() + cProfile.run("pt1(input)") + cProfile.run("pt2(input)") + print(pt1(input)) + print(pt2(input)) diff --git a/solutions/py/main.py b/solutions/py/main.py index f3c5b78..e860885 100644 --- a/solutions/py/main.py +++ b/solutions/py/main.py @@ -10,8 +10,9 @@ import d07 import d08 import d09 import d10 +import d11 -mods = [d01, d02, d03, d04, d05, d06, d07, d08, d09, d10] +mods = [d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11] timings = [[0 for _ in range(2)] for _ in range(len(mods))] clock_type = time.CLOCK_MONOTONIC |
