day04
This commit is contained in:
@@ -1,13 +1,8 @@
|
|||||||
|
|
||||||
package day03
|
package day03
|
||||||
|
|
||||||
import kotlin.io.println
|
import kotlin.io.println
|
||||||
|
|
||||||
class Day03 {
|
class Day03 {
|
||||||
fun parse(input: String): String {
|
|
||||||
throw NotImplementedError()
|
|
||||||
}
|
|
||||||
|
|
||||||
fun part1(input: String): Int {
|
fun part1(input: String): Int {
|
||||||
val regex = Regex("mul\\((\\d{1,3}),(\\d{1,3})\\)")
|
val regex = Regex("mul\\((\\d{1,3}),(\\d{1,3})\\)")
|
||||||
val muls = regex.findAll(input)
|
val muls = regex.findAll(input)
|
||||||
@@ -47,6 +42,5 @@ class Day03 {
|
|||||||
.sum()
|
.sum()
|
||||||
|
|
||||||
return product
|
return product
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
68
app/src/main/kotlin/day04/Day04.kt
Normal file
68
app/src/main/kotlin/day04/Day04.kt
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package day04
|
||||||
|
|
||||||
|
import kotlin.io.println
|
||||||
|
|
||||||
|
class Day04 {
|
||||||
|
fun part1(input: String): Int {
|
||||||
|
val lines = input.lines().map { it.toList() }
|
||||||
|
var count = 0
|
||||||
|
for (x in 0..<lines.count()) {
|
||||||
|
for (y in 0..<lines[x].count()) {
|
||||||
|
if (lines[x][y] == 'X') {
|
||||||
|
count += checkMASPart1(x, y, lines)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
fun checkMASPart1(x: Int, y: Int, lines: List<List<Char>>): Int {
|
||||||
|
val maxX = lines.count() - 1
|
||||||
|
val maxY = lines[0].count() - 1
|
||||||
|
var count = 0
|
||||||
|
|
||||||
|
for (dX in -1..1) {
|
||||||
|
if (x+(dX*3) < 0 || x+(dX*3) > maxX) { continue }
|
||||||
|
for (dY in -1..1) {
|
||||||
|
if (y+(dY*3) < 0 || y+(dY*3) > maxY) { continue }
|
||||||
|
if (lines[x+dX][y+dY] == 'M' && lines[x+(dX*2)][y+(dY*2)] == 'A' && lines[x+(dX*3)][y+(dY*3)] == 'S') {
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(input: String): Int {
|
||||||
|
val lines = input.lines().map { it.toList() }
|
||||||
|
var count = 0
|
||||||
|
for (x in 0..<lines.count()) {
|
||||||
|
for (y in 0..<lines[x].count()) {
|
||||||
|
if (lines[x][y] == 'A') {
|
||||||
|
count += checkMASPart2(x, y, lines)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count
|
||||||
|
}
|
||||||
|
|
||||||
|
fun checkMASPart2(x: Int, y: Int, lines: List<List<Char>>): Int {
|
||||||
|
val maxX = lines.count() - 1
|
||||||
|
val maxY = lines[0].count() - 1
|
||||||
|
if (x-1 < 0 || x+1 > maxX) { return 0 }
|
||||||
|
if (y-1 < 0 || y+1 > maxY) { return 0 }
|
||||||
|
val topLeft = lines[x-1][y-1]
|
||||||
|
val bottomRight = lines[x+1][y+1]
|
||||||
|
if (topLeft != bottomRight && (bottomRight == 'M' || bottomRight == 'S') && (topLeft == 'M' || topLeft == 'S')) {
|
||||||
|
val topRight = lines[x-1][y+1]
|
||||||
|
val bottomLeft = lines[x+1][y-1]
|
||||||
|
if (topRight != bottomLeft && (bottomLeft == 'M' || bottomLeft == 'S') && (topRight == 'M' || topRight == 'S')) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
package day03
|
package day03
|
||||||
|
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
|
49
app/src/test/kotlin/day04/Day04Test.kt
Normal file
49
app/src/test/kotlin/day04/Day04Test.kt
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package day04
|
||||||
|
|
||||||
|
import kotlin.test.Test
|
||||||
|
import util.InputDownloader
|
||||||
|
|
||||||
|
class Day04Test {
|
||||||
|
val dayNum = 4
|
||||||
|
val day = Day04()
|
||||||
|
|
||||||
|
val input = InputDownloader().getInput(dayNum)
|
||||||
|
val example = """MMMSXXMASM
|
||||||
|
MSAMXMSMSA
|
||||||
|
AMXSXMAAMM
|
||||||
|
MSAMASMSMX
|
||||||
|
XMASAMXAMM
|
||||||
|
XXAMMXXAMA
|
||||||
|
SMSMSASXSS
|
||||||
|
SAXAMASAAA
|
||||||
|
MAMMMXMMMM
|
||||||
|
MXMXAXMASX"""
|
||||||
|
|
||||||
|
@Test fun part1Example() {
|
||||||
|
part1(example, 18)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun part1Solution() {
|
||||||
|
part1(input, 2545)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun part2Example() {
|
||||||
|
part2(example, 9)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test fun part2Solution() {
|
||||||
|
part2(input, 1886)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part1(input: String, expected: Int) {
|
||||||
|
val output = day.part1(input)
|
||||||
|
println("output: $output")
|
||||||
|
assert(output == expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun part2(input: String, expected: Int) {
|
||||||
|
val output = day.part2(input)
|
||||||
|
println("output: $output")
|
||||||
|
assert(output == expected)
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user