1
0

Day17 2021 in nim

This commit is contained in:
Hubert Van De Walle 2021-12-18 00:51:38 +01:00
parent 1d745a0855
commit 3906d6ef72
3 changed files with 44 additions and 0 deletions

1
2021/nim/17/example.txt Normal file
View File

@ -0,0 +1 @@
target area: x=20..30, y=-10..-5

1
2021/nim/17/input.txt Normal file
View File

@ -0,0 +1 @@
target area: x=139..187, y=-148..-89

42
2021/nim/17/main.nim Normal file
View File

@ -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)