1
1
Fork 0
This commit is contained in:
nin0dev 2024-12-01 06:16:16 -05:00
parent f61f3952d6
commit af90d44169
Signed by: nin0
SSH key fingerprint: SHA256:Is2DvJdw1OkSopR4wKJfdWV0fZhMLxpnCs1P1nPhIgA
5 changed files with 101 additions and 27 deletions

25
eds
View file

@ -1,25 +0,0 @@
- [ ] V
- [ ] Swift
- [ ] Java
- [ ] C++
- [ ] Elle
- [ ] Go
- [ ] Rust
- [ ] Dart
- [ ] SQL
- [ ] C
- [ ] C#
- [ ] Kotlin
- [ ] PHP
- [ ] JavaScript
- [ ] Crystal
- [ ] Bash
- [ ] Nim
- [ ] Perl
- [ ] Haskell
- [ ] Ruby
- [ ] pwsh
- [ ] F#
- [ ] Python
- [ ] Visual Basic
- [ ] Zig

3
.gitignore vendored
View file

@ -1,2 +1,3 @@
testing/
input.txt
input.txt
.idea/

View file

@ -15,7 +15,7 @@ AOC but one programming language per day
- [ ] SQL
- [ ] C
- [ ] C#
- [ ] Kotlin
- [x] 1: Kotlin (holy shit its actually a good lang)
- [ ] PHP
- [ ] JavaScript
- [ ] Crystal

6
2024/day1/example.txt Normal file
View file

@ -0,0 +1,6 @@
3 4
4 3
2 5
1 3
3 9
3 3

92
2024/day1/solution.kts Executable file
View file

@ -0,0 +1,92 @@
#!/usr/bin/env kotlin
import java.io.File
import java.util.Scanner
import kotlin.math.abs
val expectedExampleAnswer = 11
val secondExpectedExampleAnswer = 31
fun useProd(): Boolean {
print("Run on (p)roduction values or on (e)xample values w/ a known answer? >")
val scanner = Scanner(System.`in`)
val result = scanner.next().single() == 'p'
if(result) {
print("Running in production mode\n\n")
return true
}
print("\nRunning in example mode, expected values are $expectedExampleAnswer and $secondExpectedExampleAnswer\n")
return false
}
fun getChallenge(): Char {
val scanner = Scanner(System.`in`)
print("Challenge (1) or (2)? >")
return scanner.next().single()
}
fun challenge1(input: String): Int {
val lines = input.split("\n")
var invalidLines = 0
val leftLocations: MutableList<Int> = mutableListOf()
val rightLocations: MutableList<Int> = mutableListOf()
var answer = 0
for(line in lines) {
val locations = line.split(" ")
try {
leftLocations += locations[0].toInt()
rightLocations += locations[1].toInt()
}
catch(e: Exception) {
invalidLines++
continue
}
}
for(i in 0..<lines.size - invalidLines) {
val minLeft = leftLocations.min()
val minRight = rightLocations.min()
leftLocations.remove(minLeft)
rightLocations.remove(minRight)
answer += abs(minLeft - minRight)
}
return answer
}
fun challenge2(input: String): Int {
val lines = input.split("\n")
var invalidLines = 0
val leftLocations: MutableList<Int> = mutableListOf()
var rightLocations: MutableList<Int> = mutableListOf()
var answer = 0
for(line in lines) {
val locations = line.split(" ")
try {
leftLocations += locations[0].toInt()
rightLocations += locations[1].toInt()
}
catch(e: Exception) {
invalidLines++
continue
}
}
for(location in leftLocations) {
val amountOfTimes = rightLocations.count { it == location }
answer += location * amountOfTimes
}
return answer
}
val prodCheck = useProd()
val file = File(if(prodCheck) "input.txt" else "example.txt")
val content = file.readText()
val challengeNumber = getChallenge()
val answer = if(challengeNumber == '1') challenge1(content) else if(challengeNumber == '2') challenge2(content) else 0
println("The answer is $answer!")