Files
adventcode2024/1/1.ipynb
2024-12-11 23:09:05 +01:00

95 lines
1.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"with open('1/input', 'r') as f:\n",
" data = f.readlines()\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2378066\n"
]
}
],
"source": [
"left = []\n",
"right = []\n",
"distances = []\n",
"for line in data:\n",
" a, b = map(int, line.split())\n",
" left.append(a)\n",
" right.append(b)\n",
"\n",
"left = sorted(left)\n",
"right = sorted(right)\n",
"\n",
"for i in range(len(left)):\n",
" distances.append(abs(left[i] - right[i]))\n",
" \n",
"print(sum(distances))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#write a function that returns the amount of value existing in the 'right' list\n",
"def count_occurence(value):\n",
" return sum([ True for x in right if x == value ])\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"18934359\n"
]
}
],
"source": [
"print(sum([count_occurence(x)*x for x in left]))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}