From 40355f34d2afd7b6f0072a244b3981753804e59e Mon Sep 17 00:00:00 2001 From: Bas Dado Date: Sun, 1 Dec 2019 20:36:17 +0100 Subject: [PATCH] Initial project setup + Day 1 --- .gitignore | 129 ++++++++++++++++++ .idea/.gitignore | 2 + .idea/.name | 1 + .idea/codeStyles/Project.xml | 10 ++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/compiler.xml | 13 ++ .idea/encodings.xml | 6 + .idea/vcs.xml | 6 + pom.xml | 69 ++++++++++ .../kotlin/com/basdado/adventofcode/Utils.kt | 11 ++ .../com/basdado/adventofcode/day1/Day1.kt | 40 ++++++ src/main/resources/day/1/input.txt | 100 ++++++++++++++ 12 files changed, 392 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/vcs.xml create mode 100644 pom.xml create mode 100644 src/main/kotlin/com/basdado/adventofcode/Utils.kt create mode 100644 src/main/kotlin/com/basdado/adventofcode/day1/Day1.kt create mode 100644 src/main/resources/day/1/input.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..39592f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,129 @@ + +# Created by https://www.gitignore.io/api/maven,kotlin,intellij +# Edit at https://www.gitignore.io/?templates=maven,kotlin,intellij + +### Intellij ### +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +auto-import. +.idea/modules.xml +.idea/*.iml +.idea/modules +*.iml +*.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +### Intellij Patch ### +# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 + +modules.xml +.idea/misc.xml + +# Sonarlint plugin +.idea/**/sonarlint/ + +# SonarQube Plugin +.idea/**/sonarIssues.xml + +# Markdown Navigator plugin +.idea/**/markdown-navigator.xml +.idea/**/markdown-navigator/ + +### Kotlin ### +# Compiled class file +*.class + +# Log file +*.log + +# BlueJ files +*.ctxt + +# Mobile Tools for Java (J2ME) +.mtj.tmp/ + +# Package Files # +*.jar +*.war +*.nar +*.ear +*.zip +*.tar.gz +*.rar + +# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml +hs_err_pid* + +### Maven ### +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar +.flattened-pom.xml + +# End of https://www.gitignore.io/api/maven,kotlin,intellij diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..ccf0348 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +adventofcode2019 \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..1bec35e --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..8cc3e22 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..b26911b --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..d86ce79 --- /dev/null +++ b/pom.xml @@ -0,0 +1,69 @@ + + + + 4.0.0 + + com.basdado.adventofcode + advent-of-code-2019 + 1.0-SNAPSHOT + jar + + com.basdado.adventofcode advent-of-code-2019 + + + UTF-8 + 1.3.61 + official + 4.12 + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin.version} + test + + + junit + junit + ${junit.version} + test + + + + + src/main/kotlin + src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + compile + compile + + compile + + + + test-compile + test-compile + + test-compile + + + + + + + + diff --git a/src/main/kotlin/com/basdado/adventofcode/Utils.kt b/src/main/kotlin/com/basdado/adventofcode/Utils.kt new file mode 100644 index 0000000..da0916f --- /dev/null +++ b/src/main/kotlin/com/basdado/adventofcode/Utils.kt @@ -0,0 +1,11 @@ +package com.basdado.adventofcode + +import java.nio.file.Files +import java.nio.file.Paths +import java.util.stream.Stream + +fun lines(resourceFile: String): Stream { + return Files.lines(Paths.get(Utils::class.java.getResource(resourceFile).toURI())) +} + +class Utils diff --git a/src/main/kotlin/com/basdado/adventofcode/day1/Day1.kt b/src/main/kotlin/com/basdado/adventofcode/day1/Day1.kt new file mode 100644 index 0000000..e043ac2 --- /dev/null +++ b/src/main/kotlin/com/basdado/adventofcode/day1/Day1.kt @@ -0,0 +1,40 @@ +package com.basdado.adventofcode.day1 + +import com.basdado.adventofcode.lines + +const val DAY1_PATH = "/day/1/input.txt" + +fun main(args: Array) { + println(Day1.puzzle1()) + println(Day1.puzzle2()) +} + +object Day1 { + + fun puzzle1(): Long { + return lines(DAY1_PATH) + .filter { !it.isBlank() } + .mapToLong { it.toLong() } + .map { (it / 3) - 2 } + .sum() + } + + fun puzzle2(): Long { + return lines(DAY1_PATH) + .filter { !it.isBlank() } + .mapToLong { it.toLong() } + .map { (it / 3) - 2 } + .map { it + calculateFuelFuel(it) } + .sum() + } + + private fun calculateFuelFuel(initialFuel: Long): Long { + var currentFuel = initialFuel + var sum = 0L + while (currentFuel > 8) { + currentFuel = currentFuel / 3 - 2 + sum += currentFuel + } + return sum + } +} \ No newline at end of file diff --git a/src/main/resources/day/1/input.txt b/src/main/resources/day/1/input.txt new file mode 100644 index 0000000..f103e22 --- /dev/null +++ b/src/main/resources/day/1/input.txt @@ -0,0 +1,100 @@ +66626 +95053 +84365 +111504 +97412 +124986 +133224 +60267 +68096 +120910 +105547 +138575 +112841 +113102 +92387 +83511 +62646 +98974 +138093 +59417 +137854 +78318 +143846 +81514 +86217 +98493 +82056 +129376 +61322 +51045 +115467 +106540 +149439 +141253 +65608 +130480 +79444 +80032 +101908 +105695 +145502 +87538 +68817 +128768 +148784 +115464 +147306 +148666 +118258 +82755 +68422 +98333 +105334 +120963 +89349 +78675 +99151 +61383 +81549 +106544 +72880 +88152 +110879 +91277 +123584 +78430 +51658 +126045 +93833 +52535 +130831 +130920 +80069 +140263 +50943 +63503 +116135 +112686 +67582 +83515 +128736 +136447 +69998 +72472 +61009 +136054 +124675 +134813 +149765 +132135 +127787 +148333 +78020 +94212 +81407 +58994 +146820 +66603 +62761 +86955