-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay03.kt
48 lines (39 loc) · 1.41 KB
/
Day03.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
* Copyright (c) 2019 by Todd Ginsberg
*/
/**
* Advent of Code 2019, Day 3 - Crossed Wires
* Problem Description: http://adventofcode.com/2019/day/3
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2019/day3/
*/
package com.ginsberg.advent2019
import java.lang.IllegalArgumentException
class Day03(input: List<String>) {
private val wire1: List<Point2D> = parseWire(input[0])
private val wire2: List<Point2D> = parseWire(input[1])
private val intersections: Set<Point2D> = wire1.intersect(wire2)
fun solvePart1(): Int =
intersections.map { it.distanceTo(Point2D.ORIGIN) }.min()!!
fun solvePart2(): Int =
intersections.map { cross ->
wire1.indexOf(cross) + wire2.indexOf(cross) + 2
}.min()!!
private fun parseWire(line: String): List<Point2D> {
var current = Point2D.ORIGIN
return line.split(",").flatMap {
val direction = it.first()
val steps = it.drop(1).toInt()
(0 until steps).map {
val next = when(direction) {
'U' -> current.up()
'D' -> current.down()
'L' -> current.left()
'R' -> current.right()
else -> throw IllegalArgumentException("Invalid direction: $direction")
}
current = next
next
}
}
}
}