{"id":1116,"date":"2024-12-03T18:38:20","date_gmt":"2024-12-03T16:38:20","guid":{"rendered":"https:\/\/www.juustila.com\/antti\/?p=1116"},"modified":"2024-12-03T18:43:11","modified_gmt":"2024-12-03T16:43:11","slug":"advent-of-code-day-3-revisited","status":"publish","type":"post","link":"https:\/\/www.juustila.com\/antti\/2024\/12\/03\/advent-of-code-day-3-revisited\/","title":{"rendered":"Advent of Code Day 3 revisited"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">So, originally I did this the old school style, parsing the input string character by character, managing the state of processing while discovering the elements from the input string.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Tonight I reimplemented part 1 using <a href=\"https:\/\/hachyderm.io\/tags\/Swift\">#Swift<\/a> RegexBuilder. I\u2019ve never been comfortable in using regular expressions (regex) but wanted to give a try. <\/p>\n\n\n<div class=\"wp-block-image is-style-default\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"971\" height=\"1024\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/Nayttokuva-2024-12-03-kello-18.12.52-971x1024.png\" alt=\"Swift RegexBuilder code snippet showing how to pick up the mul instructions with parameters from the day's puzzle input.\" class=\"wp-image-1118\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/Nayttokuva-2024-12-03-kello-18.12.52-971x1024.png 971w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/Nayttokuva-2024-12-03-kello-18.12.52-284x300.png 284w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/Nayttokuva-2024-12-03-kello-18.12.52-768x810.png 768w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/Nayttokuva-2024-12-03-kello-18.12.52.png 988w\" sizes=\"auto, (max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 1362px) 62vw, 840px\" \/><figcaption class=\"wp-element-caption\">Snippet of RegexBuilder code showing how to specify what elements to pick from the input string.<\/figcaption><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Got it working nicely after some hiccups, as usual &#8211; wondered why the sum of mul operations was smaller than expected, and it was because I had <code>Repeat(1...2)<\/code> instead of <code>Repeat(1...3)<\/code> in the code. Didn&#8217;t notice that some <code>mul<\/code> parameters were three numbers, since in puzzle examples there were only two numbers. So the regex was only picking first two numbers at most from the input :facepalm: <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Learned something new. Maybe regexes are not so terrifying things after all&#8230;<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"500\" height=\"563\" src=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/ProgrammersRegex.png\" alt=\"An illustration depicting a muscular figure reminiscent of a Greek warrior, labeled &quot;PROGRAMMERS,&quot; with an arrow lodged in its leg labeled &quot;REGEX.&quot; The style is simplistic and humorous, emphasizing the struggles programmers face with regular expressions.\" class=\"wp-image-1117\" srcset=\"https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/ProgrammersRegex.png 500w, https:\/\/www.juustila.com\/antti\/wp-content\/uploads\/2024\/12\/ProgrammersRegex-266x300.png 266w\" sizes=\"auto, (max-width: 500px) 85vw, 500px\" \/><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>So, originally I did this the old school style, parsing the input string character by character, managing the state of processing while discovering the elements from the input string. Tonight I reimplemented part 1 using #Swift RegexBuilder. I\u2019ve never been comfortable in using regular expressions (regex) but wanted to give a try. Got it working &hellip; <a href=\"https:\/\/www.juustila.com\/antti\/2024\/12\/03\/advent-of-code-day-3-revisited\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Advent of Code Day 3 revisited&#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,149,70,150,46],"class_list":["post-1116","post","type-post","status-publish","format-standard","hentry","category-coding","tag-advent-of-code","tag-aoc2024","tag-programming","tag-regex","tag-swift"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1116","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=1116"}],"version-history":[{"count":2,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1116\/revisions"}],"predecessor-version":[{"id":1121,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/posts\/1116\/revisions\/1121"}],"wp:attachment":[{"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/media?parent=1116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/categories?post=1116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.juustila.com\/antti\/wp-json\/wp\/v2\/tags?post=1116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}