This commit is contained in:
Gleb Koval 2023-12-06 03:07:11 +00:00
parent a007a5a19d
commit ea5c6c1e3b
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
12 changed files with 398 additions and 1 deletions

View File

@ -23,4 +23,9 @@ I'll probably try to vary languages a bit to get some variety.
**Note**: This one was re-written because what I initially wrote belongs on [r/programminghorror](https://www.reddit.com/r/programminghorror/).
### [Day 4](day4/)
**Language**: Golang
**Language**: Golang
### [Day 5](day5/)
**Language**: Kotlin
**Note**: Here begin the optimisations! (some people got away with brute force, I decided not to even go there).

1
day5/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
out/

8
day5/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

12
day5/.idea/day5.iml Normal file
View File

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
</component>
</module>

6
day5/.idea/kotlinc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Kotlin2JvmCompilerArguments">
<option name="jvmTarget" value="1.8" />
</component>
</project>

View File

@ -0,0 +1,23 @@
<component name="libraryTable">
<library name="KotlinJavaRuntime" type="repository">
<properties maven-id="org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.21" />
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.21/kotlin-stdlib-jdk8-1.9.21.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.21/kotlin-stdlib-1.9.21.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.21/kotlin-stdlib-jdk7-1.9.21.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.21/kotlin-stdlib-jdk8-1.9.21-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.21/kotlin-stdlib-1.9.21-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-javadoc.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.21/kotlin-stdlib-jdk7-1.9.21-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk8/1.9.21/kotlin-stdlib-jdk8-1.9.21-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib/1.9.21/kotlin-stdlib-1.9.21-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/13.0/annotations-13.0-sources.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/kotlin/kotlin-stdlib-jdk7/1.9.21/kotlin-stdlib-jdk7-1.9.21-sources.jar!/" />
</SOURCES>
</library>
</component>

6
day5/.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" project-jdk-name="openjdk-21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
day5/.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/day5.iml" filepath="$PROJECT_DIR$/.idea/day5.iml" />
</modules>
</component>
</project>

6
day5/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

33
day5/eg.txt Normal file
View File

@ -0,0 +1,33 @@
seeds: 79 14 55 13
seed-to-soil map:
50 98 2
52 50 48
soil-to-fertilizer map:
0 15 37
37 52 2
39 0 15
fertilizer-to-water map:
49 53 8
0 11 42
42 0 7
57 7 4
water-to-light map:
88 18 7
18 25 70
light-to-temperature map:
45 77 23
81 45 19
68 64 13
temperature-to-humidity map:
0 69 1
1 0 69
humidity-to-location map:
60 56 37
56 93 4

189
day5/input.txt Normal file
View File

@ -0,0 +1,189 @@
seeds: 1848591090 462385043 2611025720 154883670 1508373603 11536371 3692308424 16905163 1203540561 280364121 3755585679 337861951 93589727 738327409 3421539474 257441906 3119409201 243224070 50985980 7961058
seed-to-soil map:
3305253869 1699909104 39566623
3344820492 1130725752 384459310
3244681427 1739475727 60572442
951517531 1800048169 868898709
1820416240 951517531 179208221
1999624461 2668946878 219310925
3729279802 1515185062 184724042
2218935386 2898481077 1015522767
3234458153 2888257803 10223274
soil-to-fertilizer map:
1569885498 220184682 161941102
3711640300 872157831 344226893
1934701528 0 25420995
3394438846 2543943059 181930710
2957858493 2070565336 135870012
1416178127 25420995 36071868
1030022714 2029369539 9317803
1039340517 1216384724 133685745
695011633 382125784 335011081
2317056551 1350070469 640801942
1731826600 2887680679 151926123
0 3039606802 695011633
2030497645 2257384153 286558906
1960122523 2038687342 31877994
3576369556 2840047597 42331426
1536338434 4027621785 33547064
1452249995 3964767856 62853929
1331718081 787697785 84460046
4055867193 2882379023 5301656
3093728505 3734618435 230149421
1173026262 61492863 158691819
1883752723 2206435348 50948805
3618700982 2725873769 92939318
1515103924 2818813087 21234510
1992000517 1990872411 38497128
3323877926 717136865 70560920
fertilizer-to-water map:
898769374 211542615 277361469
2901739042 2299030230 213178977
207924763 1114173904 26774777
3752402183 1968349402 71176470
1176130843 625299169 68863743
3114918019 3783121220 137843736
1244994586 488904084 103878858
3252761755 2915409726 98951129
2779748334 3652754391 121990708
3351712884 2593688676 245406043
0 1252990064 95883380
2007290234 3920964956 325008750
3823578653 2039525872 202532901
2443486939 2512209207 81479469
4231974038 3371954615 62993258
3698239076 2242058773 54163107
2340675105 3014360855 102811834
4219112930 4245973706 12861108
4026111554 2839094719 76315007
3597118927 3434947873 101120149
2332298984 3774745099 8376121
95883380 1140948681 112041383
4102426561 3536068022 116686369
1968349402 2296221880 2808350
1971157752 4258834814 36132482
866253147 592782942 32516227
446242155 694162912 420010992
2524966408 3117172689 254781926
234699540 0 211542615
water-to-light map:
3564276417 3073533986 256027539
540951899 3329561525 136112599
3123682450 3465674124 119685876
2479417373 4222809437 72157859
1957776831 2195006920 74795586
3089045940 3585360000 28951457
3820985109 2269802506 288781515
1285562478 1664965131 530041789
234319697 234026754 79806762
3243368326 1344057040 320908091
3117997397 541633052 5685053
2551575232 547318105 349893979
3820303956 540951899 681153
0 369583040 292943
314126459 313833516 55749524
4109766624 2888333314 185200672
292943 0 234026754
677064498 3614311457 608497980
2032572417 897212084 446844956
2901469211 2700756585 187576729
1815604267 2558584021 142172564
light-to-temperature map:
2658328410 4044901271 250066025
866264123 157899985 185676775
2062023507 343576760 307684950
1535723010 981670684 313982539
3292868240 2534053678 579746095
3905180794 3703329819 341571452
2908394435 3318856014 384473805
1194970869 1601263273 340752141
427693043 1299669198 158564104
748173107 651261710 118091016
1051940898 1458233302 143029971
586257147 0 157899985
4246752246 2485838628 48215050
1849705549 769352726 212317958
0 2368902754 805703
744157132 1295653223 4015975
805703 1942015414 426887340
3872614335 3113799773 32566459
2485838628 3146366232 172489782
temperature-to-humidity map:
2731357374 2535823037 72664015
2987243945 1266132780 17518070
3983567677 3876954134 113067367
1669770178 435765631 9597802
192217183 2087059527 132586479
324803662 1449340061 36910958
82239523 1283650850 47718149
4251710314 3400860374 43256982
788691045 2012848120 74211407
139712452 383260900 52504731
1679367980 1064470362 18439862
3627675589 3649605531 101947626
1761484189 1024489895 39980467
3964889474 4132263773 7628253
469056934 1486251019 222779936
3808503415 3992550600 118386133
4096635044 4139892026 155075270
0 328658745 54602155
3929418647 3751553157 35470827
3729623215 3787023984 78880200
129957672 2003093340 9754780
3926889548 3990021501 2529099
2130123401 3260061108 28438224
1154320063 445363433 515450115
2331952939 1331368999 72869773
2804021389 1082910224 183222556
1140184340 2219646006 14135723
3606348549 4110936733 21327040
3004762015 1709030955 50302777
1801464656 0 328658745
691836870 1759333732 96854175
447785309 2608487052 21271625
3400860374 3444117356 205488175
3055064792 2233781729 261071908
2185047506 1856187907 146905433
1697807842 960813548 63676347
361714620 2494853637 40969400
2404822712 2933526446 326534662
402684020 1404238772 45101289
3972517727 3865904184 11049950
2158561625 2907040565 26485881
862902452 2629758677 277281888
54602155 3288499332 27637368
humidity-to-location map:
1368371614 3063096196 39876417
2318920763 3734391855 138926764
2980019498 3984955289 310012007
3732521234 1430493364 562446062
213274662 484132485 78936678
0 892307918 213274662
1023610211 575518293 214768297
2807160244 2776517513 21582263
2457847527 2833630022 176634966
3619027057 2663023336 113494177
2926107621 1368371614 53911877
1784866635 3524616139 209775716
2695523574 3873318619 111636670
1250827638 790286590 102021328
3290031505 1992939426 224427328
954760770 415283044 68849441
1408248031 2286404732 376618604
292211340 0 415283044
1994642351 3102972613 324278412
2642692366 3034347120 28749076
3514458833 2798099776 35530246
1238378508 563069163 12449130
2828742507 3427251025 97365114
3549989079 2217366754 69037978
2671441442 3010264988 24082132
707494384 1105582580 247266386
2634482493 1422283491 8209873

100
day5/main.kt Normal file
View File

@ -0,0 +1,100 @@
import java.nio.file.Files
import java.nio.file.Path
import kotlin.math.max
import kotlin.math.min
class Mapping(val to: String, private val lines: List<MapLine>) {
fun get(n: Long): Long =
lines.firstNotNullOfOrNull { it.get(n) } ?: n
fun getInterval(interval: Interval): List<Interval> =
lines.mapNotNull { it.getInterval(interval) }.let { intervals ->
val result = mutableListOf<Interval>()
var remaining = listOf(interval)
for ((f, t) in intervals) {
remaining = remaining.flatMap { it.difference(f) }
result.add(t)
}
result.addAll(remaining)
result
}
}
class MapLine(line: Triple<Long, Long, Long>) {
private val interval = Interval(line.second, line.second + line.third - 1)
private val offset = line.first - line.second
fun get(n: Long): Long? =
if (interval.start <= n && n <= interval.end) n + offset
else null
fun getInterval(i: Interval): Pair<Interval, Interval>? =
interval.overlap(i)?.let {
it to it.applyOffset(offset)
}
}
class Interval(val start: Long, val end: Long) {
fun overlap(interval: Interval): Interval? {
val s = max(interval.start, start)
val e = min(interval.end, end)
if (s > e) return null
return Interval(s, e)
}
fun difference(interval: Interval): List<Interval> {
val diffs = mutableListOf<Interval>()
if (start < interval.start) {
diffs.add(Interval(start, min(interval.start - 1, end)))
}
if (interval.end < end) {
diffs.add(Interval(max(interval.end + 1, start), end))
}
return diffs
}
fun applyOffset(offset: Long): Interval =
Interval(start + offset, end + offset)
}
fun main() {
val sections = Files.readString(Path.of("input.txt"))
.split("\n\n")
val seeds = sections[0].substringAfter(": ")
.split(' ')
.map { it.toLong() }
val mappings = sections.subList(1, sections.size).associate { section ->
val lines = section.split('\n')
val (from, to) = lines[0].substringBefore(" map:")
.split("-to-")
val map = Mapping(to, lines.subList(1, lines.size).map { line ->
val (a, b, c) = line.split(' ').map { it.toLong() }
MapLine(Triple(a, b, c))
})
from to map
}
val p1 = seeds.minOf { s ->
var category = "seed"
var n = s
while (category != "location") {
val mapping = mappings[category]!!
n = mapping.get(n)
category = mapping.to
}
n
}
println("Part 1: $p1")
val p2 = run {
var category = "seed"
var intervals = seeds.chunked(2)
.map { (s, l) -> Interval(s, s + l - 1) }
while (category != "location") {
val mapping = mappings[category]!!
intervals = intervals.flatMap { mapping.getInterval(it) }
category = mapping.to
}
intervals.minOf { it.start }
}
println("Part 2: $p2")
}