From d49d3614c7d14dd07d898ed2d44d35b324fc5f50 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Wed, 25 Dec 2024 12:07:21 +0100 Subject: [PATCH] AoC 2015, day 6. --- src/aoc2015/Day06.kt | 52 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/aoc2015/Day06.kt diff --git a/src/aoc2015/Day06.kt b/src/aoc2015/Day06.kt new file mode 100644 index 0000000..0b2c837 --- /dev/null +++ b/src/aoc2015/Day06.kt @@ -0,0 +1,52 @@ +package aoc2015 + +import CharGrid +import println +import readInput + +/* +--- Day 6: Probably a Fire Hazard --- +https://adventofcode.com/2015/day/6 +*/ +fun main() { + + fun part1(input: List): Int { + val grid = CharGrid(1000, 1000) + input.forEach { + val (op, x1, y1, x2, y2) = "(toggle|turn off|turn on) (\\d+),(\\d+) through (\\d+),(\\d+)".toRegex().matchEntire(it)!!.destructured + for (y in y1.toInt()..y2.toInt()) { + for (x in x1.toInt()..x2.toInt()) { + grid[x, y] = when (op) { + "turn on" -> '*' + "turn off" -> ' ' + "toggle" -> if (grid[x, y] == '*') ' ' else '*' + else -> throw IllegalStateException() + } + } + } + } + return grid.generateGridPos().count { grid[it] == '*' } + } + + fun part2(input: List): Int { + val grid = IntArray(1000 * 1000) + input.forEach { + val (op, x1, y1, x2, y2) = "(toggle|turn off|turn on) (\\d+),(\\d+) through (\\d+),(\\d+)".toRegex().matchEntire(it)!!.destructured + for (y in y1.toInt()..y2.toInt()) { + for (x in x1.toInt()..x2.toInt()) { + grid[x + y * 1000] = (grid[x + y * 1000] + when (op) { + "turn on" -> 1 + "turn off" -> -1 + "toggle" -> 2 + else -> throw IllegalStateException() + }).coerceAtLeast(0) + } + } + } + return grid.sum() + } + + val input = readInput("aoc2015/Day06") + part1(input).println() + part2(input).println() +}