{"id":1113,"date":"2024-12-03T09:35:58","date_gmt":"2024-12-03T07:35:58","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=1113"},"modified":"2024-12-03T09:36:00","modified_gmt":"2024-12-03T07:36:00","slug":"advent-of-code-day-3-mull-it-over","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2024\/12\/03\/advent-of-code-day-3-mull-it-over\/","title":{"rendered":"Advent of Code Day 3 &#8211; Mull It Over"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Today&#8217;s puzzle was about parsing. Quite straightforward to implement, having done parsing earlier for different types of structured strings, like tsv, csv, json, xml, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In part 1, I anticipated that part 2 would add parsing some other instructions than <code>mul<\/code> so I did implement it so. There is an Instruction structure that specifies:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>the instruction name<\/li>\n\n\n\n<li>expected number of parameters and<\/li>\n\n\n\n<li>the list of parameter values<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Then the instruction is able to evaluate the result. Parsing returns an array of Instructions to execute, and then they are executed, accumulating the result of each <code>mul<\/code> instruction evaluated:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private struct Instruction {\n  let instruction: String\n  let params: Int\n  var values: &#91;Int] = &#91;]\n    \n  var result: Int {\n    if instruction == \"mul\" {\n      return values.reduce(1, *)\n    }\n    return 0\n  }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In part 2, the execution just changes the state; after <code>do<\/code> instruction, following instructions are evaluated, and after <code>don't<\/code>, the instructions are ignored, until next <code>do<\/code> instruction:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func part1() -> Any {\n    let instructions = parse(data, \n                     for: &#91;\n                        Instruction(instruction: \"mul\", params: 2)\n                     ])\n    return execute(instructions)\n}\n\nfunc part2() -> Any {\n    let instructions = parse(data,\n                     for: &#91;\n                      Instruction(instruction: \"mul\", params: 2),\n                      Instruction(instruction: \"do\", params: 0),\n                      Instruction(instruction: \"don't\", params: 0)\n                     ])\n    return execute(instructions)\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Benchmarked:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$ swift run -c release AdventOfCode 3 --benchmark<br>Building for production...<br>[6\/6] Linking AdventOfCode<br>Build of product 'AdventOfCode' complete! (2.55s)<br>Executing Advent of Code challenge 3...<br>Part 1: 188116424<br>Part 2: 104245808<br>Part 1 took 0.00304425 seconds, part 2 took 0.004164459 seconds.<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s puzzle was about parsing. Quite straightforward to implement, having done parsing earlier for different types of structured strings, like tsv, csv, json, xml, etc. In part 1, I anticipated that part 2 would add parsing some other instructions than mul so I did implement it so. There is an Instruction structure that specifies: Then &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2024\/12\/03\/advent-of-code-day-3-mull-it-over\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Advent of Code Day 3 &#8211; Mull It Over&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[2],"tags":[136,48,149,70,46],"class_list":["post-1113","post","type-post","status-publish","format-standard","hentry","category-coding","tag-advent-of-code","tag-algorithms","tag-aoc2024","tag-programming","tag-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/comments?post=1113"}],"version-history":[{"count":1,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1113\/revisions"}],"predecessor-version":[{"id":1114,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1113\/revisions\/1114"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}