Skip to content

Commit

Permalink
feat(solutions/gmacario): add day05 (Part 1 OK, Part 2 WIP)
Browse files Browse the repository at this point in the history
Signed-off-by: Gianpaolo Macario <[email protected]>
  • Loading branch information
gmacario committed Dec 5, 2024
1 parent d233e9e commit b3fc3e2
Show file tree
Hide file tree
Showing 2 changed files with 1,729 additions and 0 deletions.
359 changes: 359 additions & 0 deletions solutions/gmacario/day05-jupyter/day05.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,359 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "e8c36490-ac44-4e4f-9fc3-97d15fbef673",
"metadata": {},
"source": [
"# AoC 2024 - Day 5\n",
"\n",
"<https://adventofcode.com/2024/day/5>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "6b7c6dc6-b77c-47e0-ac97-33f7f86c484c",
"metadata": {},
"outputs": [],
"source": [
"from icecream import ic"
]
},
{
"cell_type": "markdown",
"id": "f3458754-aa32-44e5-b229-4f519108459e",
"metadata": {},
"source": [
"## Part 1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b8ac6332-ebd1-424b-a781-df1771e81be8",
"metadata": {},
"outputs": [],
"source": [
"test_input = \"\"\"47|53\n",
"97|13\n",
"97|61\n",
"97|47\n",
"75|29\n",
"61|13\n",
"75|53\n",
"29|13\n",
"97|29\n",
"53|29\n",
"61|53\n",
"97|53\n",
"61|29\n",
"47|13\n",
"75|47\n",
"97|75\n",
"47|61\n",
"75|61\n",
"47|29\n",
"75|13\n",
"53|13\n",
"\n",
"75,47,61,53,29\n",
"97,61,53,29,13\n",
"75,29,13\n",
"75,97,47,61,53\n",
"61,13,29\n",
"97,13,75,29,47\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b",
"metadata": {},
"outputs": [],
"source": [
"with open(\"input_day05_gmacario.txt\", 'r') as file:\n",
" input_text = file.read()\n",
"\n",
"# input_text = test_input # DEBUG\n",
"\n",
"# ic(input_text)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "48622bfa-12c9-43ff-9758-28047f26047e",
"metadata": {},
"outputs": [],
"source": [
"parse_rules = True\n",
"page_ordering_rules = list()\n",
"page_numbers = list()\n",
"\n",
"for line in input_text.splitlines():\n",
" # ic(line)\n",
" if line == \"\":\n",
" parse_rules = False\n",
" elif parse_rules:\n",
" page_ordering_rules.append(tuple(int(ele) for ele in line.split(\"|\")))\n",
" else:\n",
" page_numbers.append(tuple(int(ele) for ele in line.split(\",\")))\n",
"\n",
"# ic(page_ordering_rules)\n",
"# ic(page_numbers)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "93c4e5b7-4d9d-4b82-a530-fd045e2fc6ab",
"metadata": {},
"outputs": [],
"source": [
"def is_sequence_ordered(seq):\n",
" # ic(\"BEGIN is_sequence_ordered\", seq)\n",
" result = True\n",
" for rule in page_ordering_rules:\n",
" if rule[0] not in seq or rule[1] not in seq:\n",
" # ic(\"DEBUG: Skipping(1) rule\", rule)\n",
" continue\n",
" pos1 = seq.index(rule[0])\n",
" pos2 = seq.index(rule[1])\n",
" # ic(pos1, pos2)\n",
" if pos1 > pos2:\n",
" # ic(\"DEBUG: Skipping(2) rule\", rule)\n",
" result = False\n",
" # ic(\"DEBUG: Relevant\", rule, result)\n",
" # ic(\"is_sequence_ordered\", seq, result)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "ca2ece58-99f7-4657-a1e8-b164b5982a5e",
"metadata": {},
"outputs": [],
"source": [
"# Sanity Checks with test1\n",
"#\n",
"# assert is_sequence_ordered(page_numbers[0]) == True\n",
"# assert is_sequence_ordered(page_numbers[1]) == True\n",
"# assert is_sequence_ordered(page_numbers[2]) == True\n",
"# assert is_sequence_ordered(page_numbers[3]) == False"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "2f1e2b3c-db37-4ea3-9e24-7cb8cf15dcde",
"metadata": {},
"outputs": [],
"source": [
"def middle_element(seq):\n",
" result = seq[len(seq) // 2]\n",
" # ic(\"middle_element\", seq, result)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d84ac7eb-0518-41bb-a6a7-e1c62086a71d",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| 'Part 1 result', total: 5329\n"
]
},
{
"data": {
"text/plain": [
"('Part 1 result', 5329)"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total = 0\n",
"for seq in page_numbers:\n",
" # ic(seq)\n",
"\n",
" if is_sequence_ordered(seq):\n",
" total += middle_element(seq) \n",
"\n",
"ic(\"Part 1 result\", total)"
]
},
{
"cell_type": "markdown",
"id": "fc439b96-c48d-482f-a0c9-a63572f7b7de",
"metadata": {},
"source": [
"## Part 2"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "3e522b8b-8eae-40bf-844f-12ff20957c00",
"metadata": {},
"outputs": [],
"source": [
"def reorder_sequence(seq):\n",
" # ic(\"BEGIN reorder_sequence\", seq)\n",
"\n",
" # Consider only the page_ordering_rules which contain all element\n",
"\n",
" # ic(len(page_ordering_rules))\n",
" relevant_ordering_rules = [r for r in page_ordering_rules if r[0] in seq and r[1] in seq]\n",
" sorted(relevant_ordering_rules)\n",
" # ic(len(relevant_ordering_rules), relevant_ordering_rules)\n",
" \n",
" result = list(seq)\n",
"\n",
" # for rule in sorted(page_ordering_rules):\n",
" # if rule[0] not in result or rule[1] not in result:\n",
" # # ic(\"DEBUG: Skipping(1) rule\", rule)\n",
" # continue\n",
" # pos1 = result.index(rule[0])\n",
" # pos2 = result.index(rule[1])\n",
" # # ic(pos1, pos2)\n",
" # if pos1 > pos2:\n",
" # # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n",
" # result[pos1], result[pos2] = result[pos2], result[pos1]\n",
" # ic(\"DEBUG: After swapping\", pos1, \"and\", pos2, \"->\", result)\n",
" # # ic(\"DEBUG: Relevant\", rule, result)\n",
"\n",
" for rule in relevant_ordering_rules:\n",
" pos1 = result.index(rule[0])\n",
" pos2 = result.index(rule[1])\n",
" # ic(pos1, pos2)\n",
" if pos1 > pos2:\n",
" # ic(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n",
" result[pos1], result[pos2] = result[pos2], result[pos1]\n",
" # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n",
" # ic(\"DEBUG: Relevant\", rule, result)\n",
" \n",
" #\n",
" \n",
" # ic(\"reorder sequence\", seq, \"->\", result)\n",
" # assert is_sequence_ordered(result), ic(seq, result)\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "d7393bcb-135a-470e-a37b-4149449ef1d8",
"metadata": {},
"outputs": [],
"source": [
"# Sanity Checks with test1\n",
"#\n",
"# assert is_sequence_ordered(page_numbers[3]) == False\n",
"\n",
"# ic(reorder_sequence(page_numbers[3]))\n",
"# ic(reorder_sequence(page_numbers[4]))\n",
"# ic(reorder_sequence(page_numbers[5]))"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "6ed8e5c4-625b-4c4b-be23-5ccbdeaee80e",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| reorder_sequence(page_numbers[0]): [67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]\n"
]
},
{
"data": {
"text/plain": [
"[67, 22, 45, 82, 69, 98, 76, 36, 39, 37, 73, 35, 47]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sanity Checks on real data\n",
"#\n",
"# assert is_sequence_ordered(page_numbers[0]) == False\n",
"ic(reorder_sequence(page_numbers[0]))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "9e518d75-370f-495c-9a86-8887562ef15b",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"ic| 'Part 2 result', total: 5896\n"
]
},
{
"data": {
"text/plain": [
"('Part 2 result', 5896)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"total = 0\n",
"for seq in page_numbers:\n",
" # ic(seq)\n",
"\n",
" if not is_sequence_ordered(seq):\n",
" ordered_seq = reorder_sequence(seq)\n",
" # ic(\"DEBUG: incorrectly-ordered\", seq, \"->\", ordered_seq)\n",
" total += middle_element(ordered_seq)\n",
"\n",
"ic(\"Part 2 result\", total)"
]
}
],
"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.11.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading

0 comments on commit b3fc3e2

Please sign in to comment.