diff --git a/2021/nim/17/example.txt b/2021/nim/17/example.txt new file mode 100644 index 0000000..f40609b --- /dev/null +++ b/2021/nim/17/example.txt @@ -0,0 +1 @@ +target area: x=20..30, y=-10..-5 \ No newline at end of file diff --git a/2021/nim/17/input.txt b/2021/nim/17/input.txt new file mode 100644 index 0000000..6c282f5 --- /dev/null +++ b/2021/nim/17/input.txt @@ -0,0 +1 @@ +target area: x=139..187, y=-148..-89 \ No newline at end of file diff --git a/2021/nim/17/main.nim b/2021/nim/17/main.nim new file mode 100644 index 0000000..7159196 --- /dev/null +++ b/2021/nim/17/main.nim @@ -0,0 +1,42 @@ +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)