https://kotlinlang.org logo
Join the conversationJoin Slack
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by Linen
advent-of-code
  • j

    joelpedraza

    12/07/2018, 7:05 PM
    What are you guys using to time your solutions?
    a
    g
    e
    • 4
    • 7
  • j

    joelpedraza

    12/08/2018, 12:01 AM
    https://gist.github.com/joelpedraza/b43efc1d91053d9e9ff18872cbb3c1b2
    k
    • 2
    • 1
  • k

    keturn

    12/08/2018, 4:45 AM
    my 7.2 runs in a quarter of the time of my 7.1. I am not sure if it's just JVM warmup or what.
    g
    j
    • 3
    • 3
  • a

    andyb

    12/08/2018, 7:08 AM
    Think Day 8 was pretty straightforward. Spoiler in thread.
    j
    g
    • 3
    • 3
  • j

    Joris PZ

    12/08/2018, 7:24 AM
    Day 8 was indeed straightforward: https://github.com/jorispz/aoc-2018/blob/master/src/commonMain/kotlin/P08.kt Timings: JVM
    61/2
    , JS
    62/4
    , native
    47/38
    l
    • 2
    • 3
  • t

    todd.ginsberg

    12/08/2018, 3:11 PM
    class Day08(rawInput: String) {
        private val tree: Node = Node.of(rawInput.split(" ").map { it.toInt() }.iterator())
    
        fun solvePart1(): Int =
            tree.metadataTotal
    
        fun solvePart2(): Int =
            tree.value
    
        private class Node(children: List<Node>, metadata: List<Int>) {
            companion object {
                fun of(values: Iterator<Int>): Node {
                    val numChildren: Int = values.next()
                    val numMetadata: Int = values.next()
                    val children = (0 until numChildren).map { Node.of(values) }
                    val metadata = (0 until numMetadata).map { values.next() }.toList()
                    return Node(children, metadata)
                }
            }
    
            val metadataTotal: Int =
                metadata.sum() + children.sumBy { it.metadataTotal }
    
            val value: Int =
                if (children.isEmpty()) metadata.sum()
                else metadata.map { children.getSafely(it)?.value ?: 0 }.sum()
    
            private fun List<Node>.getSafely(i: Int): Node? =
                if (i > this.size) null
                else this[i - 1]
    
        }
    }
    t
    k
    • 3
    • 5
  • k

    karelpeeters

    12/08/2018, 3:17 PM
    But is it possible?
    tailrec
    is only if you recurse only once per call at most, right?
    j
    • 2
    • 2
  • k

    karelpeeters

    12/08/2018, 7:51 PM
    There's also a list initializer,
    List(...) { ... }
    , which reads a bit funny 😒imple_smile:
    l
    • 2
    • 4
  • k

    karelpeeters

    12/08/2018, 8:05 PM
    Well but there you only call
    callatz_tailrec
    once, while in the challenge you need to call it
    n
    times.
    j
    • 2
    • 7
  • k

    keturn

    12/08/2018, 9:03 PM
    oh wow complete with custom CharSequence.parseInt(start)
    j
    o
    k
    • 4
    • 37
  • k

    karelpeeters

    12/08/2018, 9:15 PM
    I think substrings are already "views".
    o
    k
    • 3
    • 4
  • l

    littlelightcz

    12/09/2018, 9:43 AM
    Day 9 assignment sounds like hell already ... so let's do this! 😄 Seems like we're gonna need to code some kind of Turing machine to solve this 😁
    • 1
    • 1
  • j

    Joris PZ

    12/09/2018, 9:56 AM
    At the very least I know I won't be running this version of the code 25 times per platform 🙂
    a
    • 2
    • 4
  • r

    Rostyslav

    12/09/2018, 11:55 AM
    My day9 solution: https://github.com/Rtchaik/AoC2018/blob/master/src/advent2018/day09/solution09.kt
    👍 4
    a
    • 2
    • 1
  • j

    Joris PZ

    12/09/2018, 12:37 PM
    Timings were interesting today. I only measured part 2, (first/best) out of 25: JVM
    911/563 ms
    , JS
    7972/5725 ms
    . Native did not finish but exited very quickly with
    Process finished with exit code -1,073,741,571.
    Anyone have any idea how I can find out what went wrong there? My native debugging skills are less than weak
    k
    s
    • 3
    • 10
  • f

    fdlk

    12/09/2018, 3:21 PM
    I got
    LinkedList
    with
    listIterator()
    to work. Quite simple to make it circular. Took some debugging to get the off-by-one cases right. https://github.com/fdlk/advent-2018/blob/5093cbd1006f017b9400a25a27c58a2feec6358a/src/main/kotlin/nl/kelpin/fleur/advent2018/Day09.kt
    k
    • 2
    • 5
  • j

    joelpedraza

    12/09/2018, 3:24 PM
    I’m almost certain theres a closed form solution to this one
    • 1
    • 1
  • t

    todd.ginsberg

    12/09/2018, 5:26 PM
    I opted for a LinkedList and added a shift function to it. That way I was only ever looking at the head of the list and it made reasoning about it easier.
    👍 1
    l
    • 2
    • 2
  • t

    todd.ginsberg

    12/09/2018, 5:34 PM
    I really tried maintaining a pointer but I was just getting confused and off by one and just figured this was easier for me. Might be slower.
    a
    • 2
    • 5
  • t

    todd.ginsberg

    12/09/2018, 10:27 PM
    Well, depending on your algorithm it might not scale linearly. And you might have GC pressure if you are doing a lot of allocating.
    e
    • 2
    • 1
  • f

    felipecsl

    12/09/2018, 10:43 PM
    here’s mine btw https://github.com/felipecsl/advent-of-code/blob/master/src/main/kotlin/aoc2018/Day9.kt fairly short this time
    👍 1
    k
    • 2
    • 3
  • k

    kef

    12/10/2018, 12:53 AM
    Hi, can you help me understand why I got such a strange execution times with my solution for the last challenge? https://kotlinlang.slack.com/archives/C0922A726/p1544400345429600
    l
    • 2
    • 2
  • j

    Joris PZ

    12/10/2018, 8:22 AM
    My Day 10 solution is here: https://github.com/jorispz/aoc-2018/blob/master/src/commonMain/kotlin/P10.kt Simulating particles we have seen in earlier years, and as before the problem is in defining a stopping condition. I cheated somewhat and resorted to thoughtful trial-and-error, but I am sure some people here or on Reddit will come up with cleverer stuff. Today's timings for both parts (first/best of 25): JVM
    262/44
    , JS
    454/254
    , native
    5830/5830
    (first run was the best)
    l
    • 2
    • 1
  • l

    littlelightcz

    12/10/2018, 10:00 AM
    Hmh, it seems that for day 10, when saved to file, the 1 night sky snapshot is going to have like 10GB of data? 🤔
    j
    • 2
    • 3
  • l

    littlelightcz

    12/10/2018, 11:41 AM
    Finally solved it as well 🙂. I started with a Matrix, which ended up on OutOfMemory instantly 😄 . Then I thought I am gonna write it to file, which gave me an estimate for 10GB per file - that was also not way to go 😁 , but finally I figured out the trick, although left my code optimised to write down a gigabite-sized sky snapshots to the files! 😆
    Day_10.kt
    k
    • 2
    • 3
  • t

    todd.ginsberg

    12/10/2018, 3:31 PM
    I guess I don't really have to account for changes in font size... haha
    k
    • 2
    • 1
  • k

    karelpeeters

    12/10/2018, 5:40 PM
    Binary? That requires that you have an a maximum length, right?
    j
    • 2
    • 1
  • a

    andyb

    12/10/2018, 6:02 PM
    I used an accumulator function which folded over an infinite sequence & only kept the star fields which have converged to a small size. I then just picked the one of these with the smallest size. Worked in this instance.
    Day10.kt
    t
    k
    • 3
    • 5
  • t

    todd.ginsberg

    12/10/2018, 8:43 PM
    I wish I understood enough math to know what's going on with all these mathy solutions. Maybe a good 2019 goal is to relearn math I forgot.
    j
    • 2
    • 3
  • j

    joelpedraza

    12/11/2018, 11:41 AM
    Day11.js
    👍 3
    j
    t
    l
    • 4
    • 12
Powered by Linen
Title
j

joelpedraza

12/11/2018, 11:41 AM
Day11.js
👍 3
j

Joris PZ

12/11/2018, 12:08 PM
I have brute-forced part 2, knowing that there has to be a better way. I am positive you found it, but I just can't grasp the essence of your algorithm. Can you elaborate a bit about how you do it? (And how fast is your part 2?)
j

joelpedraza

12/11/2018, 12:58 PM
The relation of partial sums in the comment is the key to understanding
t

tarek

12/11/2018, 1:06 PM
The gist of the idea is explained by this data structure https://en.wikipedia.org/wiki/Summed-area_table
👍 3
j

joelpedraza

12/11/2018, 1:14 PM
Consider a 4x4 grid of all 1s
gridOfPartialSums(4, 4) { _, _ -> 1 }
Partial sum table looks like:
1, 2, 3, 4
 2, 4, 6, 8
 3, 6, 9,12
 4, 8,12,16
My implementation has a leading row/col of zeros, to keep from having to do heroics with indices
0, 0, 0, 0, 0
 0, 1, 2, 3, 4
 0, 2, 4, 6, 8
 0, 3, 6, 9,12
 0, 4, 8,12,16
Suppose you wanted the sum for the cell at 3,3 of size 1 9-6-6+4 = 1
3,3 of size 2 9-3-3+1 = 4
And so on
As for perf, between 8 and 9 ms on my hardware
j

Joris PZ

12/11/2018, 2:13 PM
Thanks, that is perfect
l

littlelightcz

12/11/2018, 2:27 PM
Nice algorithm ... if my "home-made" solution doesn't end within 20minutes, I am going to try it out 😄
View count: 3