|
6 | 6 | "source": [
|
7 | 7 | "# Advent of Code 2023\n",
|
8 | 8 | "\n",
|
9 |
| - "[Website](https://adventofcode.com/2023)" |
| 9 | + "[Website](https://adventofcode.com/2023)\n", |
| 10 | + "\n", |
| 11 | + "## Day 1: Trebuchet?!\n", |
| 12 | + "\n", |
| 13 | + "What is the sum of all of the calibration values?" |
| 14 | + ] |
| 15 | + }, |
| 16 | + { |
| 17 | + "cell_type": "code", |
| 18 | + "execution_count": null, |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "import re\n", |
| 23 | + "\n", |
| 24 | + "def get_digits(line):\n", |
| 25 | + " return re.findall(r'\\d', line)\n", |
| 26 | + "\n", |
| 27 | + "def get_calibration_value(line):\n", |
| 28 | + " digits = get_digits(line)\n", |
| 29 | + " return int(digits[0] + digits[-1]) if len(digits) else 0\n", |
| 30 | + "\n", |
| 31 | + "def get_calibration_sum(lines):\n", |
| 32 | + " return sum([get_calibration_value(line) for line in lines])\n", |
| 33 | + "\n", |
| 34 | + "lines = open('01_input.txt', 'r').readlines()\n", |
| 35 | + "get_calibration_sum(lines)" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "markdown", |
| 40 | + "metadata": {}, |
| 41 | + "source": [ |
| 42 | + "What is the sum of all of the calibration values?" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": null, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [], |
| 50 | + "source": [ |
| 51 | + "def get_digits(line):\n", |
| 52 | + " numbers = {'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9', \\\n", |
| 53 | + " 'zero': '0', 'one': '1', 'two': '2', 'three': '3', 'four': '4', 'five': '5', 'six': '6', 'seven': '7', 'eight': '8', 'nine': '9'}\n", |
| 54 | + "\n", |
| 55 | + " digits = []\n", |
| 56 | + " for start_pos in range(len(line)):\n", |
| 57 | + " digit = next((numbers[number] for number in numbers if line[start_pos:].startswith(number)), None)\n", |
| 58 | + " if digit != None:\n", |
| 59 | + " digits.append(digit)\n", |
| 60 | + " return digits\n", |
| 61 | + "\n", |
| 62 | + "get_calibration_sum(lines)" |
10 | 63 | ]
|
11 | 64 | }
|
12 | 65 | ],
|
|
17 | 70 | "name": "python3"
|
18 | 71 | },
|
19 | 72 | "language_info": {
|
| 73 | + "codemirror_mode": { |
| 74 | + "name": "ipython", |
| 75 | + "version": 3 |
| 76 | + }, |
| 77 | + "file_extension": ".py", |
| 78 | + "mimetype": "text/x-python", |
20 | 79 | "name": "python",
|
| 80 | + "nbconvert_exporter": "python", |
| 81 | + "pygments_lexer": "ipython3", |
21 | 82 | "version": "3.11.5"
|
22 | 83 | }
|
23 | 84 | },
|
|
0 commit comments