-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added puzzle download and submission automation. New structure for puzzle classes and more
- Loading branch information
Showing
16 changed files
with
212 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
input.txt filter=git-crypt diff=git-crypt | ||
correctedinput.txt filter=git-crypt diff=git-crypt | ||
sample.txt filter=git-crypt diff=git-crypt | ||
d*.txt filter=git-crypt diff=git-crypt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,6 @@ build/ | |
.idea/ | ||
.gradle/ | ||
out.txt | ||
.DS_Store | ||
.DS_Store | ||
.env | ||
submission.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"d":"red","e":[1,2,3,4],"f":5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1113122113 |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package networking | ||
|
||
import io.github.cdimascio.dotenv.Dotenv | ||
import io.ktor.client.* | ||
import io.ktor.client.engine.okhttp.* | ||
import io.ktor.client.plugins.* | ||
import io.ktor.client.request.* | ||
|
||
val client = HttpClient(OkHttp) { | ||
followRedirects = false | ||
install(UserAgent) { | ||
agent = "https://github.com/hibob224/aoc/ by contact@galajeu.com" | ||
} | ||
defaultRequest { | ||
cookie("session", Dotenv.load().get("aocSession")) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package template | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.runBlocking | ||
import networking.client | ||
import java.io.File | ||
|
||
object InputProvider { | ||
|
||
fun provideInput(year: Int, day: Int, example: Boolean): String = if (example) { | ||
File("input/examples/y$year/d$day.txt") | ||
} else { | ||
File("input/y$year/d$day.txt") | ||
.also { | ||
if (!it.exists()) downloadInput(year, day, it) | ||
} | ||
}.readText() | ||
|
||
private fun downloadInput(year: Int, day: Int, out: File) = runBlocking { | ||
println("Downloading input: Y${year}D$day") | ||
val response = client.get("https://adventofcode.com/$year/day/$day/input") | ||
require(response.status.isSuccess()) | ||
out.writeBytes(response.bodyAsBytes()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package template | ||
|
||
abstract class Puzzle<P1, P2>( | ||
val year: Int, | ||
val day: Int, | ||
val example: Boolean = false, | ||
) { | ||
|
||
val rawInput = InputProvider.provideInput(year, day, example) | ||
|
||
abstract val input: Any | ||
|
||
abstract fun solvePartOne(): P1 | ||
|
||
abstract fun solvePartTwo(): P2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package template | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import kotlinx.coroutines.runBlocking | ||
import networking.client | ||
import utils.print | ||
import java.io.File | ||
import kotlin.time.measureTimedValue | ||
|
||
fun solve(puzzleProvider: () -> Puzzle<*, *>) { | ||
val puzzle = puzzleProvider() | ||
val (p1, p1d) = measureTimedValue { puzzle.solvePartOne() } | ||
val (p2, p2d) = measureTimedValue { puzzle.solvePartTwo() } | ||
println("Solving Y${puzzle.year}-D${puzzle.day}") | ||
println("Part One: ${p1!!.bold()} - ${p1d.inWholeMilliseconds} ms") | ||
submitAnswer(p1.toString(), puzzle.year, puzzle.day, 1, puzzle.example) | ||
println("Part Two: ${p2!!.bold()} - ${p2d.inWholeMilliseconds} ms") | ||
submitAnswer(p2.toString(), puzzle.year, puzzle.day, 2, puzzle.example) | ||
} | ||
|
||
private fun submitAnswer(answer: String, year: Int, day: Int, part: Int, example: Boolean) { | ||
if (isSolved(year, day, part) || example) return | ||
print("Submit answer? (Yes/No/Ignore): ") | ||
val inp = readln().lowercase() | ||
when (inp) { | ||
"y" -> submitAnswer(answer, year, day, part) | ||
"i" -> markSubmitted(year, day, part) | ||
} | ||
} | ||
|
||
private fun submitAnswer(answer: String, year: Int, day: Int, part: Int) = runBlocking { | ||
val response = client.post("https://adventofcode.com/$year/day/$day/answer") { | ||
setBody(parameters { | ||
append("level", part.toString()) | ||
append("answer", answer) | ||
}.formUrlEncode()) | ||
contentType(ContentType.Application.FormUrlEncoded) | ||
} | ||
|
||
val result = """article>(.*)</article""".toRegex() | ||
.find(response.bodyAsText())!! | ||
.groupValues[1] | ||
.replace("""<a href.*?</a>""".toRegex(), "") | ||
.replace("<p>", "") | ||
.replace("</p>", "") | ||
.print() | ||
|
||
if ("That's the right answer!" in result) { | ||
markSubmitted(year, day, part) | ||
} | ||
} | ||
|
||
private fun isSolved(year: Int, day: Int, part: Int): Boolean { | ||
val key = "${year}.${day}.${part}" | ||
val submission = File("submission.cache").readLines() | ||
return key in submission | ||
} | ||
|
||
private fun markSubmitted(year: Int, day: Int, part: Int) { | ||
File("submission.cache").appendText("${year}.${day}.$part\n") | ||
} | ||
|
||
private fun Any.bold(): String = "\u001B[33;1m$this\u001B[0m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package y2015 | ||
|
||
import kotlinx.serialization.json.* | ||
import template.Puzzle | ||
import template.solve | ||
import utils.orZero | ||
|
||
fun main() = solve { Day12() } | ||
|
||
class Day12 : Puzzle<Int, Int>(year = 2015, day = 12, example = false) { | ||
|
||
override val input = rawInput | ||
private val numberRegex = """-?\d+""".toRegex() | ||
|
||
override fun solvePartOne(): Int { | ||
return numberRegex | ||
.findAll(input) | ||
.map { it.value.toInt() } | ||
.sum() | ||
} | ||
|
||
override fun solvePartTwo(): Int { | ||
return Json.parseToJsonElement(input).sum() | ||
} | ||
|
||
private fun JsonElement.sum(): Int { | ||
return when (this) { | ||
is JsonObject -> if (values.any { (it as? JsonPrimitive)?.content == "red" }) { | ||
0 | ||
} else { | ||
values.sumOf { it.sum() } | ||
} | ||
is JsonArray -> sumOf { it.sum() } | ||
is JsonPrimitive -> intOrNull.orZero() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import io.kotest.matchers.shouldBe | ||
import template.Puzzle | ||
|
||
abstract class PuzzleTest<P1, P2> { | ||
|
||
abstract val puzzle: () -> Puzzle<P1, P2> | ||
|
||
fun runTest(p1: P1, p2: P2) { | ||
val p = puzzle() | ||
p.solvePartOne() shouldBe p1 | ||
p.solvePartTwo() shouldBe p2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package y2015 | ||
|
||
import PuzzleTest | ||
import org.junit.jupiter.api.Test | ||
import template.Puzzle | ||
|
||
class Day12Test : PuzzleTest<Int, Int>() { | ||
|
||
override val puzzle: () -> Puzzle<Int, Int> = { Day12() } | ||
|
||
@Test | ||
fun `test y2015d12`() { | ||
runTest(111754, 65402) | ||
} | ||
} |