Skip to content

Instantly share code, notes, and snippets.

View thenixan's full-sized avatar
🐢
On vacation

Ilya Nixan thenixan

🐢
On vacation
View GitHub Profile
@thenixan
thenixan / main.kt
Created January 10, 2021 22:10
AoC 2020 Day 1
import java.io.File
fun main(args: Array<String>) {
val result = DayOneTask.run()
println(result)
}
object DayOneTask {
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
service.loadDataWithoutAnyParsingAtAll()
.flatMapObservable { responseBody ->
Observable.create<DataItem> { emitter ->
JsonReader(responseBody.charStream())
.use { reader ->
while (reader.hasNext()) {
if (reader.peek() == JsonToken.BEGIN_OBJECT) {
reader.beginObject()
if (reader.nextName() == "data") {
class JsonReaderObservable<T>(elementType: Type, gson: Gson, private val reader: JsonReader) : Observable<T>() {
private val subject = ReplaySubject.create<T>()
init {
reader.beginArray()
while (reader.hasNext()) {
subject.onNext(gson.fromJson(reader, elementType))
}
reader.endArray()
data class NewDataHolder(
@JsonAdapter(ObservableTypeAdapterFactory::class)
@SerializedName("data")
val data: Observable<DataItem>
)
class ObservableTypeAdapterFactory : TypeAdapterFactory {
service.loadDataWithNewTypeConverter()
.flatMapObservable {
it.data
}
.count()
.subscribe({ result ->
println("Size is: $result")
}, { error -> error.printStackTrace() })
data class NewDataHolder(
@SerializedName("data")
val data: Observable<DataItem>
)
interface Api {
@GET("thenixan-blogposts/json-streaming-data/master/one-large-file.json")
fun loadDataInUsualWay(): Single<DataHolder>
}
val service = Retrofit.Builder()
.baseUrl("https://raw.githubusercontent.com/")
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
fun benchmark(f: () -> Unit) {
System.gc()
val startTime = System.currentTimeMillis()
val startMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
f.invoke()
System.gc()
val finishTime = System.currentTimeMillis()
val finishMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
data class DataHolder(
@SerializedName("data") val data: Array<DataItem>
)
data class DataItem(
@SerializedName("about") val about: String,
@SerializedName("email") val email: String,
@SerializedName("first_name") val firstName: String,
@SerializedName("gender") val gender: String,
@SerializedName("hash") val hash: String,