From 077fbe6e21f7da89cb9385893549ff046ae43290 Mon Sep 17 00:00:00 2001 From: chrisly42 Date: Sun, 3 Dec 2023 07:12:48 +0100 Subject: [PATCH] Day 3 and I am beginning to hate this. --- src/aoc2023/Day03.kt | 175 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 src/aoc2023/Day03.kt diff --git a/src/aoc2023/Day03.kt b/src/aoc2023/Day03.kt new file mode 100644 index 0000000..497c839 --- /dev/null +++ b/src/aoc2023/Day03.kt @@ -0,0 +1,175 @@ +package aoc2023 + +import println +import readInput + +/* +--- Day 3: Gear Ratios --- +You and the Elf eventually reach a gondola lift station; he says the gondola lift will take you up to the water source, but this is as far as he can bring you. You go inside. +It doesn't take long to find the gondolas, but there seems to be a problem: they're not moving. +"Aaah!" +You turn around to see a slightly-greasy Elf with a wrench and a look of surprise. "Sorry, I wasn't expecting anyone! The gondola lift isn't working right now; it'll still be a while before I can fix it." You offer to help. +The engineer explains that an engine part seems to be missing from the engine, but nobody can figure out which one. If you can add up all the part numbers in the engine schematic, it should be easy to work out which part is missing. +The engine schematic (your puzzle input) consists of a visual representation of the engine. There are lots of numbers and symbols you don't really understand, but apparently any number adjacent to a symbol, even diagonally, is a "part number" and should be included in your sum. (Periods (.) do not count as a symbol.) +Here is an example engine schematic: +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. + +In this schematic, two numbers are not part numbers because they are not adjacent to a symbol: 114 (top right) and 58 (middle right). Every other number is adjacent to a symbol and so is a part number; their sum is 4361. +Of course, the actual engine schematic is much larger. What is the sum of all of the part numbers in the engine schematic? + +*/ +fun main() { + + val inlineTestInput = """ +467..114.. +...*...... +..35..633. +......#... +617*...... +.....+.58. +..592..... +......755. +...$.*.... +.664.598.. +""" + + fun squareLinearizedArray(input: List): Pair { + val dim = input.first().length + val out = IntArray(dim * dim) + val symbols = CharArray(dim * dim) + for ((rnum, r) in input.withIndex()) { + r.forEachIndexed { index, c -> out[rnum * dim + index] = if (c.isDigit()) c - '0' else -1 } + r.forEachIndexed { index, c -> symbols[rnum * dim + index] = if (!c.isDigit()) c else '.' } + } + return out to symbols + } + + fun part1(input: List): Int { + val dim = input.first().length + val (out, symbols) = squareLinearizedArray(input) + var sum = 0 + val adjMap = BooleanArray(dim * dim) + for (i in out.indices) { + val x = i % dim + val y = i / dim + adjMap[i] = (x > 0 && (symbols[i - 1] != '.')) || + (x < dim - 1 && (symbols[i + 1] != '.')) || + (y > 0 && x > 0 && (symbols[i - dim - 1] != '.')) || + (y > 0 && (symbols[i - dim] != '.')) || + (y > 0 && x < dim - 1 && (symbols[i - dim + 1] != '.')) || + (y < dim - 1 && x > 0 && (symbols[i + dim - 1] != '.')) || + (y < dim - 1 && (symbols[i + dim] != '.')) || + (y < dim - 1 && x < dim - 1 && (symbols[i + dim + 1] != '.')) + } +// adjMap.asSequence().chunked(dim).joinToString("\n") { it.joinToString("") { if (it) "*" else " " } }.println() + var num = 0 + var pickMe = false + for (i in out.indices) { + if (out[i] < 0 || i % dim == 0) { + if (pickMe) sum += num + pickMe = false + num = 0 + } + if (out[i] >= 0) { + pickMe = pickMe or adjMap[i] + num *= 10 + num += out[i] + } + } + if (pickMe) sum += num + return sum + } + + fun part2(input: List): Int { + val dim = input.first().length + val (out, symbols) = squareLinearizedArray(input) + var sum = 0 + val adjMap = BooleanArray(dim * dim) + for (i in out.indices) { + val x = i % dim + val y = i / dim + + if (symbols[i] == '*') { + var digaround = 0 + val posMaps = ArrayList>() + if (x > 0 && (out[i - 1] >= 0)) { + digaround += 1 + posMaps.add(-1 to 0) + } + if (x < dim - 1 && (out[i + 1] >= 0)) { + digaround += 1 + posMaps.add(+1 to 0) + } + + var digtop = 0 + if (y > 0 && x > 0 && (out[i - dim - 1] >= 0)) digtop += 1 + if (y > 0 && (out[i - dim] >= 0)) digtop += 2 + if (y > 0 && x < dim - 1 && (out[i - dim + 1] >= 0)) digtop += 4 + if (listOf(1, 2, 3, 4, 6, 7).contains(digtop)) { + digaround += 1 + if (digtop and 1 != 0) posMaps.add(-1 to -1) + else if (digtop and 2 != 0) posMaps.add(0 to -1) + else if (digtop and 4 != 0) posMaps.add(1 to -1) + } + if (listOf(5).contains(digtop)) { + digaround += 2 + posMaps.add(-1 to -1) + posMaps.add(1 to -1) + } + + var digbottom = 0 + if (y < dim - 1 && x > 0 && (out[i + dim - 1] >= 0)) digbottom += 1 + if (y < dim - 1 && (out[i + dim] >= 0)) digbottom += 2 + if (y < dim - 1 && x < dim - 1 && (out[i + dim + 1] >= 0)) digbottom += 4 + if (listOf(1, 2, 3, 4, 6, 7).contains(digbottom)) { + digaround += 1 + if (digbottom and 1 != 0) posMaps.add(-1 to 1) + else if (digbottom and 2 != 0) posMaps.add(0 to 1) + else if (digbottom and 4 != 0) posMaps.add(1 to 1) + } + if (listOf(5).contains(digbottom)) { + digaround += 2 + posMaps.add(-1 to 1) + posMaps.add(1 to 1) + } + if (digaround == 2) { + val numbers = posMaps.map { + var xx = x + it.first + var yy = y + it.second + while (xx > 0 && out[xx - 1 + yy * dim] >= 0) xx-- + var num = 0 + while (out[xx + yy * dim] >= 0 && xx < dim) { + num *= 10 + num += out[xx + yy * dim] + xx++ + } + num + } + sum += numbers[0] * numbers[1] + } + } + } + return sum + } + + // test if implementation meets criteria from the description, like: + val testInput = inlineTestInput.trim().reader().readLines() + //val testInput = readInput("aoc2023/Day03_test") + println("Part 1 Test: " + part1(testInput)) + println("Part 2 Test: " + part2(testInput)) + check(part1(testInput) == 4361) + check(part2(testInput) == 467835) + + val input = readInput("aoc2023/Day03") + part1(input).println() + part2(input).println() +}