-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDay20.kt
57 lines (47 loc) · 1.61 KB
/
Day20.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
49
50
51
52
53
54
55
56
57
/*
* Copyright (c) 2018 by Todd Ginsberg
*/
/**
* Advent of Code 2018, Day 20 - A Regular Map
*
* Problem Description: http://adventofcode.com/2018/day/20
* Blog Post/Commentary: https://todd.ginsberg.com/post/advent-of-code/2018/day20/
*/
package com.ginsberg.advent2018
import java.util.ArrayDeque
class Day20(rawInput: String) {
private val grid: Map<Point, Int> = parseGrid(rawInput)
fun solvePart1(): Int =
grid.maxBy { it.value }!!.value
fun solvePart2(): Int =
grid.count { it.value >= 1000 }
private fun parseGrid(input: String): Map<Point, Int> {
val grid = mutableMapOf(startingPoint to 0)
val stack = ArrayDeque<Point>()
var current = startingPoint
input.forEach {
when (it) {
'(' -> stack.push(current)
')' -> current = stack.pop()
'|' -> current = stack.peek()
in movementRules -> {
// If we are moving to a spot we haven't seen before, we can
// record this as a new distance.
val nextDistance = grid.getValue(current) + 1
current = movementRules.getValue(it).invoke(current)
grid[current] = minOf(grid.getOrDefault(current, Int.MAX_VALUE), nextDistance)
}
}
}
return grid
}
companion object {
private val startingPoint = Point(0, 0)
private val movementRules = mapOf(
'N' to Point::up,
'S' to Point::down,
'E' to Point::right,
'W' to Point::left
)
}
}