day 1
This commit is contained in:
parent
f61f3952d6
commit
af90d44169
5 changed files with 101 additions and 27 deletions
25
eds
25
eds
|
@ -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
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
testing/
|
testing/
|
||||||
input.txt
|
input.txt
|
||||||
|
.idea/
|
|
@ -15,7 +15,7 @@ AOC but one programming language per day
|
||||||
- [ ] SQL
|
- [ ] SQL
|
||||||
- [ ] C
|
- [ ] C
|
||||||
- [ ] C#
|
- [ ] C#
|
||||||
- [ ] Kotlin
|
- [x] 1: Kotlin (holy shit its actually a good lang)
|
||||||
- [ ] PHP
|
- [ ] PHP
|
||||||
- [ ] JavaScript
|
- [ ] JavaScript
|
||||||
- [ ] Crystal
|
- [ ] Crystal
|
||||||
|
|
6
2024/day1/example.txt
Normal file
6
2024/day1/example.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
92
2024/day1/solution.kts
Executable file
92
2024/day1/solution.kts
Executable 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!")
|
Loading…
Reference in a new issue