From 73fd14df89f13e0bbe436986671b713e10f24df8 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 5 Dec 2024 15:12:31 +0100 Subject: [PATCH] day(05): fix linting and convert state to enum Signed-off-by: Matej Focko --- src/Day05.kt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Day05.kt b/src/Day05.kt index 6da163c..e771320 100644 --- a/src/Day05.kt +++ b/src/Day05.kt @@ -4,10 +4,10 @@ class Day05( private val rules: Map> private val updates: List> - companion object { - val UNVISITED = 0 - val OPEN = 1 - val DONE = 2 + private enum class VertexState { + Unvisited, + Open, + Done, } init { @@ -29,7 +29,7 @@ class Day05( private fun printGraph() { println("digraph G {") - rules.forEach { u, vs -> + rules.forEach { (u, vs) -> vs.forEach { v -> println("\t$u -> $v") } @@ -54,19 +54,19 @@ class Day05( // Toposort private fun fix(xs: Set): List { val ordering = mutableListOf() - val state = mutableMapOf() + val state = mutableMapOf() fun visit(u: Int) { - if (state.getOrDefault(u, UNVISITED) == DONE) { + if (state.getOrDefault(u, VertexState.Unvisited) == VertexState.Done) { return } - check(state.getOrDefault(u, UNVISITED) != OPEN) { "We have found a loop ending in $u" } + check(state.getOrDefault(u, VertexState.Unvisited) != VertexState.Open) { "We have found a loop ending in $u" } - state[u] = OPEN + state[u] = VertexState.Open // ‹.intersect()›: Recursively search only the interesting successors rules.getOrDefault(u, emptySet()).intersect(xs).forEach { visit(it) } - state[u] = DONE + state[u] = VertexState.Done ordering.add(u) } xs.forEach { visit(it) }