Compare commits
2 commits
8c28519674
...
7891c409bb
Author | SHA1 | Date | |
---|---|---|---|
7891c409bb | |||
64dadfa0da |
3 changed files with 140 additions and 0 deletions
78
06-a.kts
Normal file
78
06-a.kts
Normal file
|
@ -0,0 +1,78 @@
|
|||
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
Normal file
30
07-a.kts
Normal file
|
@ -0,0 +1,30 @@
|
|||
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
Normal file
32
07-b.kts
Normal file
|
@ -0,0 +1,32 @@
|
|||
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