generated from arol-polito/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from B-AROL-O/gmacario-days-04-05-06-07-08
feat: gmacario solutions to days 04, 05, 06, 07, 08
- Loading branch information
Showing
10 changed files
with
4,880 additions
and
0 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
solutions/gmacario/day04-jupyter/day04-input-gmacario.txt
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,387 @@ | ||
{ | ||
"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\n", | ||
"import time" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "ba8767da-1138-466c-8756-496c662def09", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# use_test = True # Comment out for using actual puzzle input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "f3458754-aa32-44e5-b229-4f519108459e", | ||
"metadata": {}, | ||
"source": [ | ||
"## Part 1" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "b8ac6332-ebd1-424b-a781-df1771e81be8", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"ic| 'use_test was undefined - forcing to', use_test: False\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"test = \"\"\"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", | ||
"\"\"\"\n", | ||
"\n", | ||
"try:\n", | ||
" use_test\n", | ||
"except NameError:\n", | ||
" use_test = False\n", | ||
" ic(\"use_test was undefined - forcing to\", use_test)\n", | ||
" \n", | ||
"if use_test:\n", | ||
" assert test != None" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "6ac2a9c0-ef2f-496b-acd2-f8b47c89860b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if use_test:\n", | ||
" input_text = test\n", | ||
"else:\n", | ||
" with open(\"input_day05_gmacario.txt\", 'r') as file:\n", | ||
" input_text = file.read()\n", | ||
"\n", | ||
"# ic(input_text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"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": 6, | ||
"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": 7, | ||
"id": "ca2ece58-99f7-4657-a1e8-b164b5982a5e", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if use_test:\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": 8, | ||
"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": 9, | ||
"id": "d84ac7eb-0518-41bb-a6a7-e1c62086a71d", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"ic| \"elapsed=\": 'elapsed='\n", | ||
" tm_end - tm_begin: 0.05955862998962402\n", | ||
" \"result=\": 'result='\n", | ||
" part1_result: 5329\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Day 05 Part 1 RESULT:\n", | ||
"5329\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def solve_part1():\n", | ||
" total = 0\n", | ||
" for seq in page_numbers:\n", | ||
" # ic(seq)\n", | ||
" \n", | ||
" if is_sequence_ordered(seq):\n", | ||
" total += middle_element(seq)\n", | ||
" return total\n", | ||
"\n", | ||
"tm_begin = time.time()\n", | ||
"part1_result = solve_part1()\n", | ||
"tm_end = time.time()\n", | ||
"ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part1_result)\n", | ||
"print(\"Day 05 Part 1 RESULT:\")\n", | ||
"print(part1_result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "fc439b96-c48d-482f-a0c9-a63572f7b7de", | ||
"metadata": {}, | ||
"source": [ | ||
"## Part 2" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 10, | ||
"id": "3e522b8b-8eae-40bf-844f-12ff20957c00", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"def reorder_sequence(seq):\n", | ||
" # ic(\"BEGIN reorder_sequence\", seq)\n", | ||
" # Consider only the page_ordering_rules which contain all element\n", | ||
" relevant_ordering_rules = [r for r in page_ordering_rules if r[0] in seq and r[1] in seq]\n", | ||
" # ic(len(page_ordering_rules), len(relevant_ordering_rules), relevant_ordering_rules)\n", | ||
"\n", | ||
" result = list(seq)\n", | ||
" must_reorder = True\n", | ||
" while must_reorder:\n", | ||
" must_reorder = False\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", | ||
" # print(\"DEBUG: Rule\", rule, \"hit, swapping items\", pos1, \"and\", pos2)\n", | ||
" a, b = result[pos2], result[pos1]\n", | ||
" result[pos1], result[pos2] = a, b\n", | ||
" # print(f\"DEBUG: After swapping positions {pos1:3} and {pos2:3} ->\", result)\n", | ||
" must_reorder = True\n", | ||
" break\n", | ||
" \n", | ||
" # ic(\"reorder sequence\", seq, \"->\", result)\n", | ||
" # assert is_sequence_ordered(result), ic(seq, result)\n", | ||
" return result" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 11, | ||
"id": "d7393bcb-135a-470e-a37b-4149449ef1d8", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"if use_test:\n", | ||
" assert is_sequence_ordered(page_numbers[3]) == False\n", | ||
" assert reorder_sequence(page_numbers[3]) == [97, 75, 47, 61, 53]\n", | ||
" assert reorder_sequence(page_numbers[4]) == [61, 29, 13]\n", | ||
" assert reorder_sequence(page_numbers[5]) == [97, 75, 47, 29, 13]" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 12, | ||
"id": "6ed8e5c4-625b-4c4b-be23-5ccbdeaee80e", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"ic| reorder_sequence(page_numbers[0]): [67, 82, 22, 76, 69, 45, 98, 47, 37, 35, 36, 39, 73]\n" | ||
] | ||
}, | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"[67, 82, 22, 76, 69, 45, 98, 47, 37, 35, 36, 39, 73]" | ||
] | ||
}, | ||
"execution_count": 12, | ||
"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": 13, | ||
"id": "9e518d75-370f-495c-9a86-8887562ef15b", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"ic| \"elapsed=\": 'elapsed='\n", | ||
" tm_end - tm_begin: 0.12361502647399902\n", | ||
" \"result=\": 'result='\n", | ||
" part2_result: 5833\n" | ||
] | ||
}, | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Day 05 Part 2 RESULT:\n", | ||
"5833\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def solve_part2():\n", | ||
" 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", | ||
" return total\n", | ||
"\n", | ||
"tm_begin = time.time()\n", | ||
"part2_result = solve_part2()\n", | ||
"tm_end = time.time()\n", | ||
"ic(\"elapsed=\", tm_end - tm_begin, \"result=\", part2_result)\n", | ||
"print(\"Day 05 Part 2 RESULT:\")\n", | ||
"print(part2_result)\n", | ||
"# assert part2_result == 123 # test_data" | ||
] | ||
} | ||
], | ||
"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 | ||
} |
Oops, something went wrong.