Last active
December 2, 2021 07:11
-
-
Save stefanhuber/bc4ff6fcc5abe94b6b1506d465d8236e to your computer and use it in GitHub Desktop.
Advent of Code 2021
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
fun main(args: Array<String>) { | |
day1puzzle2() | |
} | |
fun countHigher(lines:List<String>) { | |
var predecessor = Int.MAX_VALUE | |
var countHigher = 0 | |
for (line in lines) { | |
val current = line?.toInt() | |
if (current > predecessor) { | |
countHigher++ | |
} | |
predecessor = current | |
} | |
println(countHigher) | |
} | |
fun day1puzzle1() { | |
val lines = File("./puzzle_input.txt").readLines() | |
countHigher(lines) | |
} | |
fun day1puzzle2() { | |
val lines = File("./puzzle_input.txt").readLines() | |
var aggregations = ArrayList<String>() | |
var aggregateSum = mutableListOf(0, 0, 0) | |
var counter = 0 | |
for (index in lines.indices) { | |
val current = lines[index]?.toInt() | |
if (index % 3 == 0) { | |
aggregateSum[0] = current | |
aggregateSum[1] += current | |
aggregateSum[2] += current | |
if (counter > 0) { | |
aggregations.add(aggregateSum[1].toString()) | |
} | |
} else if (index % 3 == 1) { | |
aggregateSum[0] += current | |
aggregateSum[1] = current | |
aggregateSum[2] += current | |
if (counter > 1) { | |
aggregations.add(aggregateSum[2].toString()) | |
} | |
} else { | |
aggregateSum[0] += current | |
aggregateSum[1] += current | |
aggregateSum[2] = current | |
aggregations.add(aggregateSum[0].toString()) | |
} | |
counter++ | |
} | |
println("${lines.size} lines aggregated into ${aggregations.size} values") | |
countHigher(aggregations) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File | |
fun main() { | |
day2puzzle1() | |
} | |
fun day2puzzle1() { | |
println(aiming(readLines())) | |
} | |
fun aiming(lines: List<String>): Int { | |
var horizontal = 0 | |
var depth = 0 | |
var aim = 0 | |
for (line in lines) { | |
if (line.indexOf("forward") == 0) { | |
val horizontalValue = line.substring(8).toInt() | |
horizontal += horizontalValue | |
depth += aim * horizontalValue | |
} else if (line.indexOf("down") == 0) { | |
aim += line.substring(5).toInt() | |
} else if (line.indexOf("up") == 0) { | |
aim -= line.substring(3).toInt() | |
} | |
} | |
return horizontal * depth | |
} | |
fun processLines(lines: List<String>): Int { | |
var horizontal = 0 | |
var depth = 0 | |
for (line in lines) { | |
if (line.indexOf("forward") == 0) { | |
horizontal += line.substring(8).toInt() | |
} else if (line.indexOf("down") == 0) { | |
depth += line.substring(5).toInt() | |
} else if (line.indexOf("up") == 0) { | |
depth -= line.substring(3).toInt() | |
} | |
} | |
return horizontal * depth | |
} | |
fun readLines(): List<String> { | |
return File("./puzzle_input_2.txt").readLines() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment