{"id":411,"date":"2025-01-31T14:19:11","date_gmt":"2025-01-31T13:19:11","guid":{"rendered":"https:\/\/informedica.nl\/?p=411"},"modified":"2025-01-31T14:22:28","modified_gmt":"2025-01-31T13:22:28","slug":"fun-with-advent-of-code-2024-day-1","status":"publish","type":"post","link":"https:\/\/informedica.nl\/?p=411","title":{"rendered":"F#un with Advent Of Code 2024 day 1"},"content":{"rendered":"\n<p>I may not be the most qualified, but I\u2019m having a lot of F#un with <strong><a href=\"https:\/\/github.com\/halcwb\/AOC2024\" target=\"_blank\" rel=\"noreferrer noopener\">Advent of Code 2024<\/a><\/strong> (AOC)! It\u2019s incredible how F# enables you to focus on problem-solving, making the experience even more enjoyable.<\/p>\n\n\n\n<p>I had heard about AOC in the past but always dismissed it, thinking I was too busy and should prioritize solving \u201creal-world\u201d problems. However, tackling these challenges has proven to be a great learning experience\u2014and, as it turns out, <strong>quite addictive<\/strong> once you earn that first gold star! &#x2b50;<\/p>\n\n\n<p><!--more--><\/p>\n\n\n<p>The first thing that stands out is how <strong>easy<\/strong> it is to dive in and start solving puzzles with F#. Unlike other languages where you might need to set up complex project structures, F# lets you <strong>just start coding<\/strong>.<\/p>\n\n\n\n<p><strong>What You Need:<\/strong><\/p>\n\n\n\n<p>\u2022 An editor like <strong><a href=\"https:\/\/code.visualstudio.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Visual Studio Code<\/a><\/strong><\/p>\n\n\n\n<p>\u2022 The <strong><a href=\"https:\/\/dotnet.microsoft.com\/en-us\/download\" target=\"_blank\" rel=\"noreferrer noopener\">.NET SDK<\/a><\/strong><\/p>\n\n\n\n<p>\u2022 The <strong><a href=\"https:\/\/ionide.io\/\" target=\"_blank\" rel=\"noreferrer noopener\">Ionide F# extension<\/a><\/strong> for VS Code<\/p>\n\n\n\n<p>With this setup, you can simply create an <strong>F# script (<\/strong>.fsx<strong> file)<\/strong> and start coding immediately\u2014no need for projects, solutions, compiled apps, or even tests (<em>oops!<\/em> &#x1f606;).<\/p>\n\n\n\n<p><strong>Day 1 \u2013 Part 1 Solution<\/strong><\/p>\n\n\n\n<p>Here\u2019s my solution for <strong>Day 1, Part 1<\/strong> using F#:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"fsharp\" class=\"language-fsharp line-numbers\">\n#load \"utils.fsx\"\n\nopen Utils\n\n\n\/\/ utility functions\nlet parseLine (line: string) =\n    line.Split(\"   \")\n    |&gt; Array.map int\n    |&gt; function\n        | [| a; b |] -&gt; a, b\n        | _ -&gt; failwith \"Invalid input\"\n\n\n\/\/ test lists\nlet list1 = [ 3; 4; 2; 1; 3; 3 ]\nlet list2 = [ 4; 3; 5; 3; 9; 3 ]\n\n\n\/\/ solution\nlet calcDist list1 list2 =\n    let list1 = list1 |&gt; Seq.sort\n    let list2 = list2 |&gt; Seq.sort\n    \/\/ calculate the absolute difference between each pair of elements\n    \/\/ and sum them up\n    Seq.zip list1 list2 \n    |&gt; Seq.map (fun (a, b) -&gt; abs (a - b)) \n    |&gt; Seq.sum\n\n\n\/\/ printing the results\nprintfn $\"Test lists: %d{calcDist list1 list2}\"\n\n\nreadLines \"input-day1.txt\"\n|&gt; Array.map parseLine\n|&gt; Array.unzip\n|&gt; fun (list1, list2) -&gt; calcDist list1 list2\n|&gt; printfn \"Distance: %d\"\n<\/code><\/pre>\n\n\n\n<p><strong>Observations:<\/strong><\/p>\n\n\n\n<p>\u2022 <strong>Minimal boilerplate<\/strong> \u2013 F# allows you to express logic succinctly.<\/p>\n\n\n\n<p>\u2022 <strong>Easy modularization<\/strong> \u2013 Other script files (utils.fsx) can be loaded just like modules, making it easy to <strong>reuse and share code<\/strong>.<\/p>\n\n\n\n<p><strong>Day 1 \u2013 Part 2 Solution<\/strong><\/p>\n\n\n\n<p>The second part of the puzzle turned out to be relatively straightforward as well:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"fsharp\" class=\"language-fsharp line-numbers\">\nlet calcSim (list1: int seq) (list2: int seq) =\n    list1\n    |&gt; Seq.map (fun x -&gt;\n        \/\/ count the number of times x appears in list2\n        let c = \n            list2 \n            |&gt; Seq.countBy ((=) x) \n            |&gt; Seq.filter (fst) \n            |&gt; Seq.sumBy snd\n        \/\/ multiply x by the count\n        c * x)\n    |&gt; Seq.sum\n<\/code><\/pre>\n\n\n\n<p><strong>Final Thoughts<\/strong><\/p>\n\n\n\n<p>Without any unnecessary complexity or ceremony, <strong>F# proves to be a powerful and F#un language<\/strong> for solving Advent of Code puzzles. The combination of <strong>functional programming, immutability, and concise syntax<\/strong> makes tackling these challenges not only efficient but also enjoyable.<\/p>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_411\" class=\"pvc_stats all  \" data-element-id=\"411\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/informedica.nl\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>I may not be the most qualified, but I\u2019m having a lot of F#un with Advent of Code 2024 (AOC)! It\u2019s incredible how F# enables you to focus on problem-solving, making the experience even more enjoyable. I had heard about AOC in the past but always dismissed it, thinking I was too busy and should &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/informedica.nl\/?p=411\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;F#un with Advent Of Code 2024 day 1&#8221;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_411\" class=\"pvc_stats all  \" data-element-id=\"411\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/informedica.nl\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,9],"tags":[],"class_list":["post-411","post","type-post","status-publish","format-standard","hentry","category-general","category-programming"],"_links":{"self":[{"href":"https:\/\/informedica.nl\/index.php?rest_route=\/wp\/v2\/posts\/411","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/informedica.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/informedica.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/informedica.nl\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/informedica.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=411"}],"version-history":[{"count":6,"href":"https:\/\/informedica.nl\/index.php?rest_route=\/wp\/v2\/posts\/411\/revisions"}],"predecessor-version":[{"id":417,"href":"https:\/\/informedica.nl\/index.php?rest_route=\/wp\/v2\/posts\/411\/revisions\/417"}],"wp:attachment":[{"href":"https:\/\/informedica.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=411"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/informedica.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=411"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/informedica.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=411"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}