Table of Contents

(task | solution)

As the challenge at AoC is not about the syntax but the algorithm, I decided to try a different approach this year: a new language every day. I am probably not going to explain the code in detail, but I will point out characteristics of each particular language that I think are worth mentioning. Let’s see how far I get. Please don’t take my judgement to serious.

Disclaimer: Of course, I am using AI to get the actual syntax, but not for the algorithm itself. I usually use ClaudeAI for that, as I’ve experienced the best results with the free version there.

On the first day, I’m starting with F# — the sister of C# — which focuses on functional programming.

Setting Up the Development Environment

First, I need to set up my development environment. Since I don’t want to pollute my system with tons of different interpreters, I prefer to work in Docker containers. Above all, this requires the Dev Containers extension. To make VS Code ready for F#, I also need the Ionide extension.

Here is the Dockerfile for a containerized F# environment:

 1# Use official .NET SDK image with F# support
 2FROM mcr.microsoft.com/dotnet/sdk:8.0
 3
 4# Install additional tools
 5RUN apt-get update && \
 6    apt-get install -y \
 7    git \
 8    curl \
 9    vim \
10    sudo
11
12# Create vscode user with sudo permissions
13RUN groupadd -r vscode \
14    && useradd -m -s /bin/bash -g vscode -G sudo vscode \
15    && echo "vscode ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers.d/vscode \
16    && chmod 0440 /etc/sudoers.d/vscode
17
18# Set working directory and ensure proper ownership
19WORKDIR /app
20RUN chown -R vscode:vscode /app
21
22# Ensure .NET tools are in PATH
23ENV PATH="/home/vscode/.dotnet/tools:${PATH}"
24
25# Set the default user to vscode
26USER vscode
27
28# Ensure .NET tools are installed for the vscode user
29RUN dotnet tool list --global || true
30
31# Default command
32CMD ["/bin/bash"]

Developing inside a container requires /.devcontainer/devcontainer.json to look like this:

 1{
 2    "name": "F# Dev Environment",
 3    "build": {
 4        "dockerfile": "../Dockerfile",
 5        "context": ".."
 6    },
 7    "remoteUser": "vscode",
 8    "workspaceFolder": "/app",
 9    "customizations": {
10        "vscode": {
11            "extensions": [
12                "Ionide.ionide-fsharp",
13                "ms-dotnettools.csharp"
14            ]
15        }
16    },
17    "mounts": [
18        "source=${localWorkspaceFolder},target=/app,type=bind,consistency=cached"
19    ],
20    "containerUser": "vscode",
21    "remoteEnv": {
22        "DOTNET_CLI_HOME": "/app"
23    }
24}

Now, after building and running the container with docker-compose build; docker-compose up -d, I can connect to the container by either pressing CMD+SHIFT+P or using the small blue button on the bottom left, selecting the option “Connect to Dev Container.

Day 1 - About differences and products

(task - solution)

The first day consists of two simple tasks, so I won’t go into too much detail:

  1. Order two lists incrementally, then get the difference for each list item and sum all differences up.
  2. For each item on the first list, count the occurrences on the second list, compute the product i * frequency(i), and then get the sum of all products.

Whats up, F#?

8 of 12, would like to stay in touch

Well, I certainly havent had the chance to get familar with F#, but at the first glance I like how you can connect output to adjecent commands using the pipe operator:

1let readListFromFile (filePath: string) =
2    File.ReadAllLines(filePath) 
3    |> Array.map int
4    |> Array.toList

This makes things quiete easy, resulting in a clearer and more concise coding style. Seems like F# is a good choice for hands one problem solving.

See you next day…

Summary

A write-up of the author's solution to Day 1 of the Advent of Code 2024 challenge, implemented in F#. The article details the setup of a containerized F# development environment using Docker and VS Code, briefly describes the day's tasks (differences and products), and shares the author's positive first impressions of F#'s functional programming style.


Main Topics: Advent of Code F# Functional Programming Docker VS Code Problem-Solving

Difficulty: intermediate

Reading Time: approx. 5 minutes