Samir Parikh · Blog · Git

Originally published on 25 November 2022

As the fall season quickly transitions to winter, that means the next installment of the Advent of Code is quickly approaching. In order to clear some of my mental cobwebs and scrape off the accumulated rust of my Perl programming capabilities, I’ve been going back trying to solve the 2015 edition of the popular coding contest.

I don’t get to program in my regular day job so it’s been challenging trying to retain what I learned from last year’s competition. I figured that tackling some old problems now will give me a head start to solve this year’s challenges with the limited time I have. You can follow my progress on the 2015 problems over on my Git repository.

There are a couple of minor changes I’ve been making to the approach I use to solve the problems. The first one is explained in my repo’s README: For most problems, I’ll slurp the entire contents of the input into a single scalar variable via:

chomp( my $input = do { local $/; <> } );

I can then parse $input however I need to into an array. Usually, it will be line-by-line (my @presents = split /\n/, $input;), but occasiionaly character-by-character (my @directions = split //, $input;). Previously, I used to vary how I ingested my input with each problem. This let me learn new ways to process text files, but ultimately distracted me from the task of solving each day’s challenge.

The second minor change I made was to solve each day’s problem in a separate directory, e.g.:

├── day01
│   ├──
│   └── input
├── day02
│   ├──
│   ├── input
│   └── test
├── day03
│   ├──
│   ├── input
│   └── test

Previously, I would just dump all my Perl files, input files, test files, etc. into one directory. Using separate directories helps me keep things more organized. This by itself isn’t a major improvement but it does help me with the next thing I’ve been experimenting with: breaking out my solutions into modules.

For some of the more complicated solutions that require multiple subroutines, I’ve been learning how to break out my programs into modules in order to keep my “main” files more tidy. An example of this is my solution to Day 18 where the bulk of the solution is tucked away in my file. My goal is to eventually learn more about object-oriented programming and am hoping that learning how to create modules like this will help in that endeavor.

Overall, my goal for solving the Advent of Code challenges is not to top the leader board each day but to learn more about the algorithms required to solve the problems and how to implement them in Perl.

If you have any other suggestions on how to better tackle this year’s event, please let me know!