|
1 | 1 | Advent of Code 2024 walkthrough
|
2 | 2 | ===============================
|
3 | 3 |
|
4 |
| -*Coming soon!* |
| 4 | +Table of Contents |
| 5 | +----------------- |
5 | 6 |
|
| 7 | +- [Day 1 - Historian Hysteria][d01] |
| 8 | +<!-- |
| 9 | +- [Day 2 - xxx][d02] |
| 10 | +- [Day 3 - xxx][d03] |
| 11 | +- [Day 4 - xxx][d04] |
| 12 | +- [Day 5 - xxx][d05] |
| 13 | +- [Day 6 - xxx][d06] |
| 14 | +- [Day 7 - xxx][d07] |
| 15 | +- [Day 8 - xxx][d08] |
| 16 | +- [Day 9 - xxx][d09] |
| 17 | +- [Day 10 - xxx][d10] |
| 18 | +- [Day 11 - xxx][d11] |
| 19 | +- [Day 12 - xxx][d12] |
| 20 | +- [Day 13 - xxx][d13] |
| 21 | +- [Day 14 - xxx][d14] |
| 22 | +- [Day 15 - xxx][d15] |
| 23 | +- [Day 16 - xxx][d16] |
| 24 | +- [Day 17 - xxx][d17] |
| 25 | +- [Day 18 - xxx][d18] |
| 26 | +- [Day 19 - xxx][d19] |
| 27 | +- [Day 20 - xxx][d20] |
| 28 | +- [Day 21 - xxx][d20] |
| 29 | +- [Day 22 - xxx][d20] |
| 30 | +- [Day 23 - xxx][d20] |
| 31 | +- [Day 24 - xxx][d20] |
| 32 | +- [Day 25 - xxx][d20] |
| 33 | +--> |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +Day 1 - Historian Hysteria |
| 38 | +-------------------------- |
| 39 | + |
| 40 | +[Problem statement][d01-problem] — [Complete solution][d01-solution] — [Back to top][top] |
| 41 | + |
| 42 | +### Part 1 |
| 43 | + |
| 44 | +All right, let's get this year started! We have two lists of integers and need |
| 45 | +to find differences between their elements, pairing them up in increasing order. |
| 46 | +First, read each input line, split it and convert to `int` using |
| 47 | +[`map()`][py-builtin-map]. |
| 48 | + |
| 49 | +```python |
| 50 | +fin = open(...) |
| 51 | + |
| 52 | +total = 0 |
| 53 | +left = [] |
| 54 | +right = [] |
| 55 | + |
| 56 | +for line in fin: |
| 57 | + l, r = map(int, line.split()) |
| 58 | + left.append(l) |
| 59 | + right.append(r) |
| 60 | +``` |
| 61 | + |
| 62 | +Now all we need to do is [`.sort()`][py-list-sort] them, then then iterate over |
| 63 | +the sorted pairs obtained via [`zip()`][py-builtin-zip] summing up their |
| 64 | +differences. For that, [`sum()`][py-builtin-sum] plus a [generator |
| 65 | +expression][py-gen-expr] will do the trick. |
| 66 | + |
| 67 | + |
| 68 | +```python |
| 69 | +left.sort() |
| 70 | +right.sort() |
| 71 | + |
| 72 | +total = sum(abs(l - r) for l, r in zip(left, right)) |
| 73 | +print('Part 1:', total) |
| 74 | +``` |
| 75 | + |
| 76 | +### Part 2 |
| 77 | + |
| 78 | +Now instead of summing differences we need to sum products. Each number on the |
| 79 | +left list needs to be multiplied by the number of times it appears on the right |
| 80 | +list. We can again use a generator expression or convert everything into a `for` |
| 81 | +loop to do both parts at once. The number of occurrences can be obtained via |
| 82 | +[`.count()`][py-list-count]. |
| 83 | + |
| 84 | +```python |
| 85 | +for l, r in zip(left, right): |
| 86 | + total1 += abs(l - r) |
| 87 | + total2 += l * right.count(l) |
| 88 | + |
| 89 | +print('Part 1:', total1) |
| 90 | +print('Part 2:', total2) |
| 91 | +``` |
6 | 92 |
|
7 | 93 | ---
|
8 | 94 |
|
@@ -86,3 +172,12 @@ Advent of Code 2024 walkthrough
|
86 | 172 | [d22-solution]: solutions/day22.py
|
87 | 173 | [d24-solution]: solutions/day24.py
|
88 | 174 | [d25-solution]: solutions/day25.py
|
| 175 | + |
| 176 | + |
| 177 | +[py-gen-expr]: https://docs.python.org/3/reference/expressions.html#generator-expressions |
| 178 | + |
| 179 | +[py-builtin-map]: https://docs.python.org/3/library/functions.html#map |
| 180 | +[py-builtin-sum]: https://docs.python.org/3/library/functions.html#sum |
| 181 | +[py-builtin-zip]: https://docs.python.org/3/library/functions.html#zip |
| 182 | +[py-list-count]: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists |
| 183 | +[py-list-sort]: https://docs.python.org/3/tutorial/datastructures.html#more-on-lists |
0 commit comments