Compare commits
No commits in common. "7891c409bb59494d946ff693d0cf3c8b96d1cc5c" and "8c2851967478109cf3bfe3dedb64aef1eac4c618" have entirely different histories.
7891c409bb
...
8c28519674
3 changed files with 0 additions and 140 deletions
78
06-a.kts
78
06-a.kts
|
@ -1,78 +0,0 @@
|
||||||
import java.util.Scanner
|
|
||||||
|
|
||||||
enum class Direction {
|
|
||||||
NORTH {
|
|
||||||
override fun moveFrom(row: Int, col: Int): Pair<Int, Int> {
|
|
||||||
return Pair(row-1, col)
|
|
||||||
}
|
|
||||||
}, SOUTH {
|
|
||||||
override fun moveFrom(row: Int, col: Int): Pair<Int, Int> {
|
|
||||||
return Pair(row+1, col)
|
|
||||||
}
|
|
||||||
}, WEST {
|
|
||||||
override fun moveFrom(row: Int, col: Int): Pair<Int, Int> {
|
|
||||||
return Pair(row, col-1)
|
|
||||||
}
|
|
||||||
}, EAST {
|
|
||||||
override fun moveFrom(row: Int, col: Int): Pair<Int, Int> {
|
|
||||||
return Pair(row, col+1)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
fun turnRight(): Direction {
|
|
||||||
return when (this) {
|
|
||||||
NORTH -> EAST
|
|
||||||
EAST -> SOUTH
|
|
||||||
SOUTH -> WEST
|
|
||||||
WEST -> NORTH
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract fun moveFrom(row: Int, col: Int): Pair<Int, Int>
|
|
||||||
}
|
|
||||||
|
|
||||||
val scanner = Scanner(System.`in`)
|
|
||||||
|
|
||||||
val map = ArrayList<Array<Char>>()
|
|
||||||
|
|
||||||
var rows = 0
|
|
||||||
var guardRow = -1
|
|
||||||
var guardCol = -1
|
|
||||||
while (scanner.hasNextLine()) {
|
|
||||||
val line = scanner.nextLine()
|
|
||||||
val pos = line.indexOf('^')
|
|
||||||
if (pos != -1) {
|
|
||||||
guardRow = rows
|
|
||||||
guardCol = pos
|
|
||||||
}
|
|
||||||
val chars = line.toCharArray().toTypedArray()
|
|
||||||
map.add(chars)
|
|
||||||
rows++
|
|
||||||
}
|
|
||||||
|
|
||||||
var facing = Direction.NORTH
|
|
||||||
|
|
||||||
var visitedCount = 0
|
|
||||||
walk@ while (true) {
|
|
||||||
if (map[guardRow][guardCol] != 'X') {
|
|
||||||
visitedCount++
|
|
||||||
}
|
|
||||||
map[guardRow][guardCol] = 'X'
|
|
||||||
|
|
||||||
var seeking = true
|
|
||||||
do {
|
|
||||||
val (nextRow, nextCol) = facing.moveFrom(guardRow, guardCol)
|
|
||||||
if (nextRow < 0 || nextRow >= map.size || nextCol < 0 || nextCol >= map[nextCol].size) {
|
|
||||||
break@walk
|
|
||||||
}
|
|
||||||
|
|
||||||
if (map[nextRow][nextCol] == '#') {
|
|
||||||
facing = facing.turnRight()
|
|
||||||
} else {
|
|
||||||
guardRow = nextRow
|
|
||||||
guardCol = nextCol
|
|
||||||
seeking = false
|
|
||||||
}
|
|
||||||
} while (seeking)
|
|
||||||
}
|
|
||||||
println(visitedCount)
|
|
30
07-a.kts
30
07-a.kts
|
@ -1,30 +0,0 @@
|
||||||
import java.math.BigInteger
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
val scanner = Scanner(System.`in`)
|
|
||||||
|
|
||||||
var sum: BigInteger = BigInteger.ZERO
|
|
||||||
while (scanner.hasNextLine()) {
|
|
||||||
val line = scanner.nextLine()
|
|
||||||
val numbersInLine = line.split("[: ]+".toRegex()).map(String::toBigInteger)
|
|
||||||
val target = numbersInLine.first()
|
|
||||||
val numbers = numbersInLine.drop(1)
|
|
||||||
val possibleResults = findAllPossibleResult(setOf(numbers.first()), numbers.drop(1))
|
|
||||||
if (possibleResults.contains(target)) {
|
|
||||||
sum += target
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println(sum)
|
|
||||||
|
|
||||||
fun findAllPossibleResult(partialResults: Set<BigInteger>, numbers: List<BigInteger>): Set<BigInteger> {
|
|
||||||
if (numbers.isEmpty()) return partialResults
|
|
||||||
|
|
||||||
val head = numbers.first()
|
|
||||||
val tail = numbers.drop(1)
|
|
||||||
val newResults = partialResults.fold(HashSet<BigInteger>()) { r, p ->
|
|
||||||
r.add(p.plus(head))
|
|
||||||
r.add(p.times(head))
|
|
||||||
r
|
|
||||||
}
|
|
||||||
return findAllPossibleResult(newResults, tail)
|
|
||||||
}
|
|
32
07-b.kts
32
07-b.kts
|
@ -1,32 +0,0 @@
|
||||||
import java.math.BigInteger
|
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
val scanner = Scanner(System.`in`)
|
|
||||||
|
|
||||||
var sum: BigInteger = BigInteger.ZERO
|
|
||||||
while (scanner.hasNextLine()) {
|
|
||||||
val line = scanner.nextLine()
|
|
||||||
val numbersInLine = line.split("[: ]+".toRegex()).map(String::toBigInteger)
|
|
||||||
val target = numbersInLine.first()
|
|
||||||
val numbers = numbersInLine.drop(1)
|
|
||||||
val possibleResults = findAllPossibleResult(setOf(numbers.first()), numbers.drop(1))
|
|
||||||
if (possibleResults.contains(target)) {
|
|
||||||
sum += target
|
|
||||||
// println(line)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
println(sum)
|
|
||||||
|
|
||||||
fun findAllPossibleResult(partialResults: Set<BigInteger>, numbers: List<BigInteger>): Set<BigInteger> {
|
|
||||||
if (numbers.isEmpty()) return partialResults
|
|
||||||
|
|
||||||
val head = numbers.first()
|
|
||||||
val tail = numbers.drop(1)
|
|
||||||
val newResults = partialResults.fold(HashSet<BigInteger>()) { r, p ->
|
|
||||||
r.add("$p$head".toBigInteger())
|
|
||||||
r.add(p.plus(head))
|
|
||||||
r.add(p.times(head))
|
|
||||||
r
|
|
||||||
}
|
|
||||||
return findAllPossibleResult(newResults, tail)
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue