Skip to content

Commit 54a7ab1

Browse files
committed
Update Advent of Code
1 parent 8240c74 commit 54a7ab1

File tree

2 files changed

+101
-12
lines changed

2 files changed

+101
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
temp.py
12
*.txt
23
*.csv
34
.ipynb_checkpoints/

advent_of_code_2021.ipynb

+100-12
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,10 @@
1111
{
1212
"cell_type": "code",
1313
"execution_count": 1,
14-
"id": "a3a0e999",
14+
"id": "e8c9d0ce",
1515
"metadata": {},
1616
"outputs": [],
1717
"source": [
18-
"// Setup\n",
1918
"import java.io.File"
2019
]
2120
},
@@ -36,6 +35,7 @@
3635
],
3736
"source": [
3837
"// Day 1\n",
38+
"\n",
3939
"val readings = File(\"1.txt\").readLines().map { it.toInt() }\n",
4040
"val part1 = readings.windowed(2).count { (a, b) -> a < b }\n",
4141
"val part2 = readings.windowed(3).windowed(2).count { (a, b) -> a.sum() < b.sum() }\n",
@@ -61,6 +61,7 @@
6161
],
6262
"source": [
6363
"// Day 2\n",
64+
"\n",
6465
"val moves = File(\"2.txt\").readLines().map { it.split(\" \") }\n",
6566
"var horizontal = 0\n",
6667
"var depth = 0\n",
@@ -157,7 +158,7 @@
157158
}
158159
],
159160
"source": [
160-
"// day 6\n",
161+
"// Day 6\n",
161162
"\n",
162163
"var fishes = File(\"6.txt\")\n",
163164
" .readLines()[0]\n",
@@ -185,33 +186,120 @@
185186
},
186187
{
187188
"cell_type": "code",
188-
"execution_count": null,
189-
"id": "088da5fa",
189+
"execution_count": 6,
190+
"id": "410107ca",
190191
"metadata": {},
191192
"outputs": [
192193
{
193194
"name": "stdout",
194195
"output_type": "stream",
195196
"text": [
196-
"355592\n"
197+
"355592\n",
198+
"101618069\n"
197199
]
198200
}
199201
],
200202
"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",
202209
"\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",
206215
"\n",
207216
"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())"
209297
]
210298
},
211299
{
212300
"cell_type": "code",
213301
"execution_count": null,
214-
"id": "d68cd1fd",
302+
"id": "1b43b63a",
215303
"metadata": {},
216304
"outputs": [],
217305
"source": []

0 commit comments

Comments
 (0)