Part 1
This commit is contained in:
parent
6861e00c47
commit
d80a8ce32f
2 changed files with 72 additions and 0 deletions
8
input/day09
Normal file
8
input/day09
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
7,1
|
||||||
|
11,1
|
||||||
|
11,7
|
||||||
|
9,7
|
||||||
|
9,5
|
||||||
|
2,5
|
||||||
|
2,3
|
||||||
|
7,3
|
||||||
64
src/day09.scala
Normal file
64
src/day09.scala
Normal file
|
|
@ -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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue