{"id":77,"date":"2024-12-03T22:03:57","date_gmt":"2024-12-03T22:03:57","guid":{"rendered":"https:\/\/blogs.oregonstate.edu\/codedcat\/?p=77"},"modified":"2024-12-03T22:03:57","modified_gmt":"2024-12-03T22:03:57","slug":"advent-of-code-2024-day-1-historian-hysteria","status":"publish","type":"post","link":"https:\/\/blogs.oregonstate.edu\/codedcat\/2024\/12\/03\/advent-of-code-2024-day-1-historian-hysteria\/","title":{"rendered":"Advent of Code 2024: Day 1 \u2014 Historian Hysteria"},"content":{"rendered":"\n<p>This year, I&#8217;m diving into Advent of Code for the first time, and I couldn&#8217;t be more excited! As a newcomer to this coding challenge, I wasn&#8217;t sure what to expect, but Day 1 proved to be the perfect introduction. The puzzles were engaging yet approachable, providing a fantastic opportunity to flex my problem-solving muscles while building confidence in my skills.<\/p>\n\n\n\n<hr>\n\n\n\n<h2 class=\"wp-block-heading\">Day 1 Puzzle: Reconciling Historians&#8217; Notes<\/h2>\n\n\n\n<p>The scenario for Day 1 was intriguing: two mismatched lists of location IDs needed to be reconciled to help a group of Elvish Senior Historians track down their missing Chief Historian. The task was split into two parts:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Calculate the total distance between the two lists by pairing the numbers from smallest to largest and summing their absolute differences.<\/li>\n\n\n\n<li>Determine a similarity score by calculating how many times each number from the first list appears in the second and summing weighted contributions.<\/li>\n<\/ol>\n\n\n\n<hr>\n\n\n\n<h2 class=\"wp-block-heading\">Pseudocode for My Solutions<\/h2>\n\n\n\n<p>Here&#8217;s how I approached each part of the puzzle:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Part 1: Total Distance<\/strong><\/p>\n\n\n\n<p>1. Parse Input<\/p>\n\n\n\n<p>Split the raw input into two separate lists of integers.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function: parse_input(input_string)\n    Initialize list1 and list2\n    For each line in input_string:\n        If line is not empty:\n            Split the line into two numbers\n            Append the first number to list1\n            Append the second number to list2\n    Return list1 and list2\n<\/code><\/pre>\n\n\n\n<p>2. Sort the Lists:<\/p>\n\n\n\n<p>Sorting ensures that the smallest numbers from each list are paired together.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function: sort_list(list)\n    Return list sorted in ascending order\n<\/code><\/pre>\n\n\n\n<p>3. Compute Total Distance:<\/p>\n\n\n\n<p>For each index, compute the absolute difference between paired numbers and sum them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function: calculate_total_distance(list1, list2)\n    Initizialize total_distance to 0\n    For i from 0 to length of list1:\n        Calculate absolute difference between list1&#091;i] and list2&#091;i]\n        Add difference to total_distance\n    Return total_distance\n<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><strong>Part 2: Similarity Score<\/strong><\/p>\n\n\n\n<p>Calculate Occurrence Similarity:<\/p>\n\n\n\n<p>1. Count how many times each number in the first list appears in the second list and create a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function: similarity_list(list1, list2)\n    Initialize similarity_list as empty\n    For each num1 in list1:\n        Count occurrences of num1 in list 2\n        Append count to similarity_list\n    Return similarity_list\n<\/code><\/pre>\n\n\n\n<p>2. Use these counts to compute the weighted similarity score.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function: similarity_score(list1, similarity_list)\n    Initialize similarity_score to 0\n    For i from 0 to length of list1:\n        similarity_score += list1&#091;i] * similarity_list{i}\n    Return similarity_score\n<\/code><\/pre>\n\n\n\n<hr>\n\n\n\n<h2 class=\"wp-block-heading\">Implementing with TDD<\/h2>\n\n\n\n<p>For both parts, I used Test-Driven Development (TDD) to structure my work:<\/p>\n\n\n\n<p>1. Write Tests First:<\/p>\n\n\n\n<p>Before writing any functional code, I created tests for each function. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>parse_input should correctly parse the input into two lists<\/li>\n\n\n\n<li>calculate_total_distance should compute the correct total for paired absolute differences<\/li>\n\n\n\n<li>similarity_list should return the correct count of occurrences for each number<\/li>\n<\/ul>\n\n\n\n<p>Example test for part 2:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>def test_similarity_score():\n    list1 = &#091;1, 2, 3, 4, 5]\n    list2 = &#091;1, 2, 1, 1, 0]\n    expected_similarity_score = 1 + 4 + 3 + 4 + 0\n    assert similarity_score(list1, similarity_list(list1, list2)) == expected_similarity_score\n<\/code><\/pre>\n\n\n\n<p>2. Develop Code to Pass Tests:<\/p>\n\n\n\n<p>Functions were implemented iteratively, ensuring each passed its corresponding tests before moving on.<\/p>\n\n\n\n<p>3. Refactor for Clarity:<\/p>\n\n\n\n<p>Once all tests passed, I revisited the code to clean up and optimize where possible.<\/p>\n\n\n\n<p>4. Continuous Testing<\/p>\n\n\n\n<p>I ran tests frequently to catch errors early and verify that new changes didn&#8217;t break existing functionality.<\/p>\n\n\n\n<hr>\n\n\n\n<h2 class=\"wp-block-heading\">Lessons from Day 1<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>TDD Keeps You Focused: Writing tests first clarified my goals and made debugging easier.<\/li>\n\n\n\n<li>Breaking Down Problems Helps: Writing pseudocode and taking the problem step by step reduced overwhelm.<\/li>\n\n\n\n<li>Python is a Great Tool for Problem-Solving: Its readability and powerful standard library made implementation smooth.<\/li>\n<\/ul>\n\n\n\n<hr>\n\n\n\n<h2 class=\"wp-block-heading\">Final Thoughts<\/h2>\n\n\n\n<p>Participating in Advent of Code for the first time has already been both challenging and rewarding. Day 1 taught me the value of a systematic approach, and I can&#8217;t wait to see what the rest of the month holds. Whether you&#8217;re a seasoned Advent of Code participant or just starting like me, I&#8217;d love to hear about your experiences!<\/p>\n\n\n\n<p>Are you taking part in Advent of Code this year? Let me know in the comments!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This year, I&#8217;m diving into Advent of Code for the first time, and I couldn&#8217;t be more excited! As a newcomer to this coding challenge, I wasn&#8217;t sure what to expect, but Day 1 proved to be the perfect introduction. The puzzles were engaging yet approachable, providing a fantastic opportunity to flex my problem-solving muscles &hellip; <a href=\"https:\/\/blogs.oregonstate.edu\/codedcat\/2024\/12\/03\/advent-of-code-2024-day-1-historian-hysteria\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Advent of Code 2024: Day 1 \u2014 Historian Hysteria&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14468,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-77","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts\/77","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/users\/14468"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/comments?post=77"}],"version-history":[{"count":1,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/media?parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/categories?post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.oregonstate.edu\/codedcat\/wp-json\/wp\/v2\/tags?post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}