diff --git a/input/day09 b/input/day09 new file mode 100644 index 0000000..c8563ea --- /dev/null +++ b/input/day09 @@ -0,0 +1,8 @@ +7,1 +11,1 +11,7 +9,7 +9,5 +2,5 +2,3 +7,3 diff --git a/src/day09.scala b/src/day09.scala new file mode 100644 index 0000000..e0b2d09 --- /dev/null +++ b/src/day09.scala @@ -0,0 +1,64 @@ +package aoc +package day09 + +import scala.math.{min, max} + +val dayNumber = "09" + +@main def part1: Unit = + println(part1(loadInput(dayNumber))) + +@main def part2: Unit = + println(part2(loadInput(dayNumber))) + +def part1(input: String): Long = + val tiles = input.split('\n').map { case s"$x,$y" => (x = x.toLong, y = y.toLong) } + tiles + .combinations(2) + .collect { + case Array(a, b) if a != b => + ((a.x - b.x + 1) * (a.y - b.y + 1)).abs + }.max + +def part2(input: String): Long = + val points = input.split('\n').map { case s"$x,$y" => (x = x.toLong, y = y.toLong) } + val polygon = points + + def corners(ax: Long, ay: Long, bx: Long, by: Long) = + List((ax, ay), (ax, by), (ay, bx), (bx, by)) + + def isInside(x: Long, y: Long) = + val counter = polygon.sliding(2).foldLeft(0) { case (counter, Array(a, b)) => + if y > min(a.y, b.y) && y <= max(a.y, b.y) && x <= max(a.x, b.x) then + val xinters = (y - a.y) * (b.x - a.x) / (b.y - a.y) + a.x + counter + (if a.x == b.x || x <= xinters then 1 else 0) + else counter + } + counter % 2 == 1 + + println(isInside(7, 1)) + + println(isInside(7, 2)) + println(isInside(0, 0)) + points + .combinations(2) + .collect { + case Array(a, b) if a != b && corners(a.x, a.y, b.x, b.y).forall((x, y) => isInside(x, y)) => + println(corners(a.x, a.y, b.x, b.y)) + (a, b) + } + .map((a, b) => ((a.x - b.x + 1) * (a.y - b.y + 1)).abs) + .max + + // if (p.y > MIN(p1.y,p2.y)) { + // if (p.y <= MAX(p1.y,p2.y)) { + // if (p.x <= MAX(p1.x,p2.x)) { + // if (p1.y != p2.y) { + // xinters = (p.y-p1.y)*(p2.x-p1.x)/(p2.y-p1.y)+p1.x; + // if (p1.x == p2.x || p.x <= xinters) + // counter++; + // } + // } + // } + // } +end part2