generated from kotlin-hands-on/advent-of-code-kotlin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
373 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
noop | ||
noop | ||
noop | ||
addx 6 | ||
addx -1 | ||
addx 5 | ||
noop | ||
noop | ||
noop | ||
addx 5 | ||
addx 11 | ||
addx -10 | ||
addx 4 | ||
noop | ||
addx 5 | ||
noop | ||
noop | ||
noop | ||
addx 1 | ||
noop | ||
addx 4 | ||
addx 5 | ||
noop | ||
noop | ||
noop | ||
addx -35 | ||
addx -2 | ||
addx 5 | ||
addx 2 | ||
addx 3 | ||
addx -2 | ||
addx 2 | ||
addx 5 | ||
addx 2 | ||
addx 3 | ||
addx -2 | ||
addx 2 | ||
addx 5 | ||
addx 2 | ||
addx 3 | ||
addx -28 | ||
addx 28 | ||
addx 5 | ||
addx 2 | ||
addx -9 | ||
addx 10 | ||
addx -38 | ||
noop | ||
addx 3 | ||
addx 2 | ||
addx 7 | ||
noop | ||
noop | ||
addx -9 | ||
addx 10 | ||
addx 4 | ||
addx 2 | ||
addx 3 | ||
noop | ||
noop | ||
addx -2 | ||
addx 7 | ||
noop | ||
noop | ||
noop | ||
addx 3 | ||
addx 5 | ||
addx 2 | ||
noop | ||
noop | ||
noop | ||
addx -35 | ||
noop | ||
noop | ||
noop | ||
addx 5 | ||
addx 2 | ||
noop | ||
addx 3 | ||
noop | ||
noop | ||
noop | ||
addx 5 | ||
addx 3 | ||
addx -2 | ||
addx 2 | ||
addx 5 | ||
addx 2 | ||
addx -25 | ||
noop | ||
addx 30 | ||
noop | ||
addx 1 | ||
noop | ||
addx 2 | ||
noop | ||
addx 3 | ||
addx -38 | ||
noop | ||
addx 7 | ||
addx -2 | ||
addx 5 | ||
addx 2 | ||
addx -8 | ||
addx 13 | ||
addx -2 | ||
noop | ||
addx 3 | ||
addx 2 | ||
addx 5 | ||
addx 2 | ||
addx -15 | ||
noop | ||
addx 20 | ||
addx 3 | ||
noop | ||
addx 2 | ||
addx -4 | ||
addx 5 | ||
addx -38 | ||
addx 8 | ||
noop | ||
noop | ||
noop | ||
noop | ||
noop | ||
noop | ||
addx 2 | ||
addx 17 | ||
addx -10 | ||
addx 3 | ||
noop | ||
addx 2 | ||
addx 1 | ||
addx -16 | ||
addx 19 | ||
addx 2 | ||
noop | ||
addx 2 | ||
addx 5 | ||
addx 2 | ||
noop | ||
noop | ||
noop | ||
noop | ||
noop | ||
noop |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
addx 15 | ||
addx -11 | ||
addx 6 | ||
addx -3 | ||
addx 5 | ||
addx -1 | ||
addx -8 | ||
addx 13 | ||
addx 4 | ||
noop | ||
addx -1 | ||
addx 5 | ||
addx -1 | ||
addx 5 | ||
addx -1 | ||
addx 5 | ||
addx -1 | ||
addx 5 | ||
addx -1 | ||
addx -35 | ||
addx 1 | ||
addx 24 | ||
addx -19 | ||
addx 1 | ||
addx 16 | ||
addx -11 | ||
noop | ||
noop | ||
addx 21 | ||
addx -15 | ||
noop | ||
noop | ||
addx -3 | ||
addx 9 | ||
addx 1 | ||
addx -3 | ||
addx 8 | ||
addx 1 | ||
addx 5 | ||
noop | ||
noop | ||
noop | ||
noop | ||
noop | ||
addx -36 | ||
noop | ||
addx 1 | ||
addx 7 | ||
noop | ||
noop | ||
noop | ||
addx 2 | ||
addx 6 | ||
noop | ||
noop | ||
noop | ||
noop | ||
noop | ||
addx 1 | ||
noop | ||
noop | ||
addx 7 | ||
addx 1 | ||
noop | ||
addx -13 | ||
addx 13 | ||
addx 7 | ||
noop | ||
addx 1 | ||
addx -33 | ||
noop | ||
noop | ||
noop | ||
addx 2 | ||
noop | ||
noop | ||
noop | ||
addx 8 | ||
noop | ||
addx -1 | ||
addx 2 | ||
addx 1 | ||
noop | ||
addx 17 | ||
addx -9 | ||
addx 1 | ||
addx 1 | ||
addx -3 | ||
addx 11 | ||
noop | ||
noop | ||
addx 1 | ||
noop | ||
addx 1 | ||
noop | ||
noop | ||
addx -13 | ||
addx -19 | ||
addx 1 | ||
addx 3 | ||
addx 26 | ||
addx -30 | ||
addx 12 | ||
addx -1 | ||
addx 3 | ||
addx 1 | ||
noop | ||
noop | ||
noop | ||
addx -9 | ||
addx 18 | ||
addx 1 | ||
addx 2 | ||
noop | ||
noop | ||
addx 9 | ||
noop | ||
noop | ||
noop | ||
addx -1 | ||
addx 2 | ||
addx -37 | ||
addx 1 | ||
addx 3 | ||
noop | ||
addx 15 | ||
addx -21 | ||
addx 22 | ||
addx -6 | ||
addx 1 | ||
noop | ||
addx 2 | ||
addx 1 | ||
noop | ||
addx -10 | ||
noop | ||
noop | ||
addx 20 | ||
addx 1 | ||
addx 2 | ||
addx 2 | ||
addx -6 | ||
addx -11 | ||
noop | ||
noop | ||
noop |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
fun main() { | ||
val input = readInput("10") | ||
// val input = readInput("10_test") | ||
|
||
fun valuesAtCycle(input: List<String>, cycle: Int) : HashMap<Int, Int> { | ||
val values: HashMap<Int, Int> = hashMapOf() | ||
var currentCycle = 1 | ||
var value = 1 | ||
|
||
values[1] = value | ||
|
||
for (i in 1 .. cycle) { | ||
val cmd = input[i - 1] | ||
if (cmd.startsWith("addx")) { | ||
currentCycle += 2 | ||
|
||
if (currentCycle > cycle) { | ||
break | ||
} | ||
|
||
values[currentCycle - 1] = value | ||
value += cmd.split(" ")[1].toInt() | ||
values[currentCycle] = value | ||
} else { | ||
currentCycle += 1 | ||
if (currentCycle > cycle) { | ||
break | ||
} | ||
|
||
values[currentCycle] = value | ||
} | ||
} | ||
|
||
if (values[cycle] == null) { | ||
values[cycle] = value | ||
} | ||
|
||
return values | ||
} | ||
|
||
fun signalStrength(input: List<String>, cycle: Int) : Int { | ||
val value = valuesAtCycle(input, cycle)[cycle] ?: 0 | ||
return value * cycle | ||
} | ||
|
||
fun part1(input: List<String>): Int { | ||
val cycles = arrayListOf(20, 60, 100, 140, 180, 220) | ||
|
||
return cycles.sumOf { | ||
signalStrength(input, it) | ||
} | ||
} | ||
|
||
fun part2(input: List<String>) { | ||
val cycle = 240 | ||
val valueRegister = valuesAtCycle(input, cycle) | ||
var spriteX: Int | ||
|
||
var x = 1 | ||
for (i in 1..cycle) { | ||
spriteX = valueRegister[i] ?: 0 | ||
|
||
if (spriteX - x == 0 || x - spriteX == 1 || x - spriteX == 2) { | ||
print("#") | ||
} else { | ||
print(".") | ||
} | ||
|
||
x++ | ||
|
||
if (i % 40 == 0) { | ||
x = 1 | ||
println() | ||
} | ||
} | ||
} | ||
|
||
println(part1(input)) | ||
println(part2(input)) | ||
} |