Skip to content

Commit d45790c

Browse files
authored
Merge pull request #4 from palinkiewicz/2023_d03
2023 Day 03
2 parents 175837a + b2446e1 commit d45790c

File tree

3 files changed

+269
-3
lines changed

3 files changed

+269
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ In this repository, I am going to store my solutions to the [Advent of Code](htt
55
I started taking part in the event in 2023 but may also consider doing tasks from the previous years.
66

77
## 2023
8-
| | My solution |
9-
| -------------------------------------------- | ----------------------------------------------------------------------------------------------------- |
8+
| | My solution |
9+
| -------------------------------------------- |-------------------------------------------------------------------------------------------------------|
1010
| [Day 1](https://adventofcode.com/2023/day/1) | [Solution](https://github.com/palinkiewicz/adventofcode-kotlin/blob/master/adventofcode2023/Day01.kt) |
1111
| [Day 2](https://adventofcode.com/2023/day/2) | [Solution](https://github.com/palinkiewicz/adventofcode-kotlin/blob/master/adventofcode2023/Day02.kt) |
12-
| [Day 3](https://adventofcode.com/2023/day/3) | Soon |
12+
| [Day 3](https://adventofcode.com/2023/day/3) | [Solution](https://github.com/palinkiewicz/adventofcode-kotlin/blob/master/adventofcode2023/Day03.kt) |

adventofcode2023/Day03.kt

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package adventofcode2023
2+
3+
import java.io.File
4+
5+
object Day03 {
6+
fun part1(inputs: List<String>) {
7+
val firstDigitChecks = setOf(
8+
Pair(-1, 0),
9+
Pair(-1, -1),
10+
Pair(0, -1),
11+
Pair(-1, 1),
12+
Pair(0, 1)
13+
)
14+
val centralDigitChecks = setOf(
15+
Pair(0, 1),
16+
Pair(0, -1)
17+
)
18+
val lastDigitChecks = setOf(
19+
Pair(1, 0),
20+
Pair(1, 1),
21+
Pair(0, 1),
22+
Pair(1, -1),
23+
Pair(0, -1)
24+
)
25+
26+
fun checkPositions(x: Int, y: Int, checks: Set<Pair<Int, Int>>): Boolean {
27+
return checks.any {
28+
val checkX = x + it.first
29+
val checkY = y + it.second
30+
31+
if (checkX < 0 || checkY < 0
32+
|| checkX >= inputs[0].length || checkY >= inputs.size
33+
) {
34+
false
35+
} else {
36+
inputs[checkY][checkX] != '.'
37+
}
38+
}
39+
}
40+
41+
fun checkPartNumberDigit(x: Int, y: Int): Boolean =
42+
(x > 0 && !inputs[y][x - 1].isDigit() && checkPositions(x, y, firstDigitChecks)
43+
|| x < inputs[0].length - 1 && !inputs[y][x + 1].isDigit() && checkPositions(x, y, lastDigitChecks)
44+
|| checkPositions(x, y, centralDigitChecks))
45+
46+
var sum = 0
47+
48+
for (y in inputs.indices) {
49+
var x = 0
50+
while (x < inputs[0].length) {
51+
if (inputs[y][x].isDigit()) {
52+
var isPartNumber = checkPartNumberDigit(x, y)
53+
var number = inputs[y][x].toString()
54+
55+
while (x < inputs[0].length - 1 && inputs[y][x + 1].isDigit()) {
56+
x++
57+
number += inputs[y][x]
58+
if (!isPartNumber && checkPartNumberDigit(x, y)) isPartNumber = true
59+
}
60+
61+
if (isPartNumber) sum += number.toInt()
62+
}
63+
x++
64+
}
65+
}
66+
67+
println(sum)
68+
}
69+
70+
fun part2(inputs: List<String>) {
71+
fun getAsteriskPositions(): List<Pair<Int, Int>> {
72+
val asteriskPositions = mutableListOf<Pair<Int, Int>>()
73+
for (y in inputs.indices) {
74+
for (x in inputs[0].indices) {
75+
if (inputs[y][x] == '*') {
76+
asteriskPositions.add(Pair(x, y))
77+
}
78+
}
79+
}
80+
return asteriskPositions
81+
}
82+
83+
fun getPartNumber(x: Int, y: Int): Pair<Int, Int>? {
84+
if (inputs[y][x].isDigit()) {
85+
var number = inputs[y][x].toString()
86+
var checkLeft = x - 1
87+
var checkRight = x + 1
88+
89+
while (checkLeft >= 0 && inputs[y][checkLeft].isDigit()) {
90+
number = inputs[y][checkLeft] + number
91+
checkLeft--
92+
}
93+
while (checkRight < inputs[0].length && inputs[y][checkRight].isDigit()) {
94+
number += inputs[y][checkRight]
95+
checkRight++
96+
}
97+
98+
return Pair(checkLeft * y, number.toInt())
99+
}
100+
return null
101+
}
102+
103+
println(getAsteriskPositions().sumOf { // first: x, second: y
104+
val partNumbers = mutableListOf<Pair<Int, Int>>()
105+
106+
for (i in -1..1) {
107+
for (j in -1..1) {
108+
val number = getPartNumber(it.first + j, it.second + i)
109+
110+
if (number != null && !partNumbers.contains(number)) {
111+
partNumbers.add(number)
112+
}
113+
}
114+
}
115+
116+
if (partNumbers.size == 2) partNumbers[0].second * partNumbers[1].second else 0
117+
})
118+
}
119+
120+
}
121+
122+
fun main() {
123+
val inputs = File("resources/adventofcode2023/Day03.txt").readLines()
124+
Day03.part1(inputs)
125+
Day03.part2(inputs)
126+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy