-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.kt
92 lines (77 loc) · 2.45 KB
/
App.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package day10.solution1
// Original solution used to solve the AoC day 10 puzzle
import common.Solution
typealias ParsedInput = List<String>
object Day10 : Solution.LinedInput<ParsedInput>(day = 10) {
override fun parseInput(input: List<String>): ParsedInput {
return input
.map { line ->
if (line == "noop") listOf(line)
else listOf("noop", line, "finish")
}
.flatten()
}
override fun part1(input: ParsedInput): Any {
var prepvalue = 0
var register = 1
var cycle = 1
var outcome = listOf<Triple<Int, Int, String>>()
for (line in input) {
when (line) {
"noop" -> {
outcome += Triple(cycle, register, line)
cycle += 1
}
"finish" -> {
register += prepvalue
prepvalue = 0
}
else -> {
val value = line.split(" ").last().toInt()
prepvalue = value
outcome += Triple(cycle, register, line)
cycle += 1
}
}
}
val interestedCycles = setOf(20, 60, 100, 140, 180, 220)
return outcome
.filter { (c) -> c % 20 == 0 }.map { (c, r) -> (c to r) }
.filter { (c) -> c in interestedCycles }
.sumOf { (c, r) -> c * r}
}
override fun part2(input: ParsedInput): Any {
var screen = mutableListOf<Char>()
var cycle = 1
var register = 1
var prepvalue = 0
fun drawPixel() {
val pixelPos = (cycle - 1) % 40
val pixel = if (pixelPos in register -1..register + 1) '#' else '.'
screen.add(pixel)
}
for (line in input) {
when (line) {
"noop" -> {
drawPixel()
cycle += 1
}
"finish" -> {
register += prepvalue
prepvalue = 0
}
else -> {
drawPixel()
val value = line.split(" ").last().toInt()
prepvalue = value
cycle += 1
}
}
}
screen.chunked(40).forEach { line -> println(line.joinToString("")) }
return Unit
}
}
fun main() {
Day10.solve(test = false)
}