8000 2023: Add day 1. · HoffmannChristian/adventofcode@6a815bd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a815bd

Browse files
2023: Add day 1.
1 parent 325c448 commit 6a815bd

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

2023/advent_of_code_2023.ipynb

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,60 @@
66
"source": [
77
"# Advent of Code 2023\n",
88
"\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)"
1063
]
1164
}
1265
],
@@ -17,7 +70,15 @@
1770
"name": "python3"
1871
},
1972
"language_info": {
73+
"codemirror_mode": {
74+
"name": "ipython",
75+
"version": 3
76+
},
77+
"file_extension": ".py",
78+
"mimetype": "text/x-python",
2079
"name": "python",
80+
"nbconvert_exporter": "python",
81+
"pygments_lexer": "ipython3",
2182
"version": "3.11.5"
2283
}
2384
},

0 commit comments

Comments
 (0)
0