Day 5 prt 2

This commit is contained in:
Daniel Flanagan 2021-12-05 14:22:46 -06:00
parent c547ce863c
commit f011067e6c
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4

View file

@ -22,22 +22,26 @@ proc part2(inputLines: seq[string]): int =
for l in inputLines: for l in inputLines:
let (x1, y1, x2, y2) = l.toPointPair() let (x1, y1, x2, y2) = l.toPointPair()
let ang = arctan2(float(y2 - y1), float(x2 - x1)) * 180 / PI let ang = arctan2(float(y2 - y1), float(x2 - x1)) * 180 / PI
echo ang if (ang.abs() mod 90) == 45.0:
if x1 == x2 or y1 == y2 or (ang.abs() mod 90) == 45.0: var x = x1
var y = y1
let xd = if x1 > x2: -1 else: 1
let yd = if y1 > y2: -1 else: 1
while x >= min(x1, x2) and x <= max(x1, x2) and y >= min(y1, y2) and y <= max(y1, y2):
points.inc((x, y))
x += xd
y += yd
elif x1 == x2 or y1 == y2:
for x in min(x1, x2)..max(x1, x2): for x in min(x1, x2)..max(x1, x2):
for y in min(y1, y2)..max(y1, y2): for y in min(y1, y2)..max(y1, y2):
echo &"{x} {y}"
points.inc((x, y)) points.inc((x, y))
for p in points.values(): for p in points.values():
echo p
if p >= 2: if p >= 2:
inc result inc result
echo result
echo points, points.len()
let input = 5.loadInput() let input = 5.loadInput()
# time("day 5 part 1"): echo input.part1() time("day 5 part 1"): echo input.part1()
# time("day 5 part 2"): echo input.part2() time("day 5 part 2"): echo input.part2()
when not defined(release): when not defined(release):
let testInput = """0,9 -> 5,9 let testInput = """0,9 -> 5,9
@ -50,5 +54,5 @@ when not defined(release):
3,4 -> 1,4 3,4 -> 1,4
0,0 -> 8,8 0,0 -> 8,8
5,5 -> 8,2""".split('\n') 5,5 -> 8,2""".split('\n')
# doAssert testInput.part1() == 5 doAssert testInput.part1() == 5
doAssert testInput.part2() == 12 doAssert testInput.part2() == 12