import nre, strutils let regex = re"x=(-?\d+)..(-?\d+), y=(-?\d+)..(-?\d+)" data = readFile("input.txt") captures = data.find(regex).get.captures x1 = captures[0].parseInt() x2 = captures[1].parseInt() y1 = captures[2].parseInt() y2 = captures[3].parseInt() minX = min(x1, x2) maxX = max(x1, x2) minY = min(y1, y2) maxY = max(y1, y2) proc land(dx: int, dy: int): int = var dx = dx var dy = dy var x = 0 var y = 0 var high = y while x < maxX and y > minY: x += dx y += dy if dx > 0: dx -= 1 elif dx < 0: dx += 1 dy -= 1 high = max(high, y) if x in minX..maxX and y in minY..maxY: return high return -1 var high = 0 var match = 0 for x in -1000..1000: for y in -1000..1000: let res = land(x, y) if res != -1: match += 1 high = max(res, high) echo(high) echo(match)