|
11 | 11 | {
|
12 | 12 | "cell_type": "code",
|
13 | 13 | "execution_count": 1,
|
14 |
| - "id": "a3a0e999", |
| 14 | + "id": "e8c9d0ce", |
15 | 15 | "metadata": {},
|
16 | 16 | "outputs": [],
|
17 | 17 | "source": [
|
18 |
| - "// Setup\n", |
19 | 18 | "import java.io.File"
|
20 | 19 | ]
|
21 | 20 | },
|
|
36 | 35 | ],
|
37 | 36 | "source": [
|
38 | 37 | "// Day 1\n",
|
| 38 | + "\n", |
39 | 39 | "val readings = File(\"1.txt\").readLines().map { it.toInt() }\n",
|
40 | 40 | "val part1 = readings.windowed(2).count { (a, b) -> a < b }\n",
|
41 | 41 | "val part2 = readings.windowed(3).windowed(2).count { (a, b) -> a.sum() < b.sum() }\n",
|
|
61 | 61 | ],
|
62 | 62 | "source": [
|
63 | 63 | "// Day 2\n",
|
| 64 | + "\n", |
64 | 65 | "val moves = File(\"2.txt\").readLines().map { it.split(\" \") }\n",
|
65 | 66 | "var horizontal = 0\n",
|
66 | 67 | "var depth = 0\n",
|
|
157 | 158 | }
|
158 | 159 | ],
|
159 | 160 | "source": [
|
160 |
| - "// day 6\n", |
| 161 | + "// Day 6\n", |
161 | 162 | "\n",
|
162 | 163 | "var fishes = File(\"6.txt\")\n",
|
163 | 164 | " .readLines()[0]\n",
|
|
185 | 186 | },
|
186 | 187 | {
|
187 | 188 | "cell_type": "code",
|
188 |
| - "execution_count": null, |
189 |
| - "id": "088da5fa", |
| 189 | + "execution_count": 6, |
| 190 | + "id": "410107ca", |
190 | 191 | "metadata": {},
|
191 | 192 | "outputs": [
|
192 | 193 | {
|
193 | 194 | "name": "stdout",
|
194 | 195 | "output_type": "stream",
|
195 | 196 | "text": [
|
196 |
| - "355592\n" |
| 197 | + "355592\n", |
| 198 | + "101618069\n" |
197 | 199 | ]
|
198 | 200 | }
|
199 | 201 | ],
|
200 | 202 | "source": [
|
201 |
| - "// day 7\n", |
| 203 | + "// Day 7\n", |
| 204 | + "\n", |
| 205 | + "var positions = File(\"7.txt\")\n", |
| 206 | + " .readLines()[0]\n", |
| 207 | + " .split(\",\")\n", |
| 208 | + " .map { it.toInt() }\n", |
202 | 209 | "\n",
|
203 |
| - "var positions = File(\"7.txt\").readLines()[0].split(\",\").map { it.toInt() }\n", |
204 |
| - "val median = positions.sorted().let { (it[it.size / 2] + it[(it.size - 1) / 2]) / 2 }\n", |
205 |
| - "val mean = ceil(positions.average())\n", |
| 210 | + "val median = positions\n", |
| 211 | + " .sorted()\n", |
| 212 | + " .let { (it[it.size / 2] + it[(it.size - 1) / 2]) / 2 }\n", |
| 213 | + "\n", |
| 214 | + "fun gauss(n: Int) = n * (n + 1) / 2\n", |
206 | 215 | "\n",
|
207 | 216 | "println(positions.sumOf { abs(it - median) })\n",
|
208 |
| - "println((1..positions.maxOrNull()!!).minOf { p -> positions.sumOf { (1..abs(p - it)).sum() } })" |
| 217 | + "println((1..positions.maxOrNull()!!).minOf { p -> positions.sumOf { gauss(abs(p - it)) } })" |
| 218 | + ] |
| 219 | + }, |
| 220 | + { |
| 221 | + "cell_type": "code", |
| 222 | + "execution_count": 7, |
| 223 | + "id": "ef0c50e7", |
| 224 | + "metadata": {}, |
| 225 | + "outputs": [ |
| 226 | + { |
| 227 | + "data": { |
| 228 | + "text/plain": [ |
| 229 | + "554" |
| 230 | + ] |
| 231 | + }, |
| 232 | + "execution_count": 7, |
| 233 | + "metadata": {}, |
| 234 | + "output_type": "execute_result" |
| 235 | + } |
| 236 | + ], |
| 237 | + "source": [ |
| 238 | + "// Day 8 part 1\n", |
| 239 | + "\n", |
| 240 | + "val signals = File(\"8.txt\").readLines()\n", |
| 241 | + "\n", |
| 242 | + "signals\n", |
| 243 | + " .map { it.split(\" | \").last().split(\" \") }\n", |
| 244 | + " .flatten()\n", |
| 245 | + " .groupingBy { it.length }\n", |
| 246 | + " .eachCount()\n", |
| 247 | + " .filterKeys { it in listOf(2, 3, 4, 7) }\n", |
| 248 | + " .values\n", |
| 249 | + " .sum()\n", |
| 250 | + "\n" |
| 251 | + ] |
| 252 | + }, |
| 253 | + { |
| 254 | + "cell_type": "code", |
| 255 | + "execution_count": 8, |
| 256 | + "id": "b04f6fa7", |
| 257 | + "metadata": {}, |
| 258 | + "outputs": [ |
| 259 | + { |
| 260 | + "name": "stdout", |
| 261 | + "output_type": "stream", |
| 262 | + "text": [ |
| 263 | + "537\n" |
| 264 | + ] |
| 265 | + } |
| 266 | + ], |
| 267 | + "source": [ |
| 268 | + "// Day 9 part 1\n", |
| 269 | + "\n", |
| 270 | + "val heights = File(\"9.txt\").readLines().map { it.toList().map { Character.getNumericValue(it) } }\n", |
| 271 | + "\n", |
| 272 | + "val row_size = heights.size\n", |
| 273 | + "val col_size = heights[0].size\n", |
| 274 | + "var low_pnts = mutableListOf<Int>()\n", |
| 275 | + "\n", |
| 276 | + "fun List<List<Int>>.at(i: Int, j: Int): Int {\n", |
| 277 | + " val inf = 99999999\n", |
| 278 | + " if (this.getOrNull(i).isNullOrEmpty()) return inf\n", |
| 279 | + " else if (this[i].getOrNull(j) == null) return inf\n", |
| 280 | + " else return this[i][j]\n", |
| 281 | + "}\n", |
| 282 | + "\n", |
| 283 | + "for (i in 0 until row_size) {\n", |
| 284 | + " for (j in 0 until col_size) {\n", |
| 285 | + " val neighbours = listOf(\n", |
| 286 | + " heights.at(i - 1, j - 1), heights.at(i - 1, j), heights.at(i - 1, j + 1), \n", |
| 287 | + " heights.at(i, j - 1), heights.at(i, j + 1),\n", |
| 288 | + " heights.at(i + 1, j - 1), heights.at(i + 1, j), heights.at(i + 1, j + 1)\n", |
| 289 | + " )\n", |
| 290 | + " if (neighbours.all { it > heights[i][j] }) {\n", |
| 291 | + " low_pnts += heights[i][j] + 1\n", |
| 292 | + " }\n", |
| 293 | + " }\n", |
| 294 | + "}\n", |
| 295 | + "\n", |
| 296 | + "println(low_pnts.sum())" |
209 | 297 | ]
|
210 | 298 | },
|
211 | 299 | {
|
212 | 300 | "cell_type": "code",
|
213 | 301 | "execution_count": null,
|
214 |
| - "id": "d68cd1fd", |
| 302 | + "id": "1b43b63a", |
215 | 303 | "metadata": {},
|
216 | 304 | "outputs": [],
|
217 | 305 | "source": []
|
|
0 commit comments