AOC 2024, Day 01: Tackling Historian Hysteria¶
Advent of Code 2024 is here, and as always on Day 1, it starts with a simpler challenge: Historian Hysteria. In this post, we will primarily provide solutions in both C# and F#, showcasing the beauty of solving problems in multiple languages.
Elf Shenanigans¶
In Day 1 of Advent of Code 2024, we are given two lists of location IDs. The goal is to:
- Find the total distance between the two lists after sorting and comparing corresponding pairs.
- Calculate a similarity score based on how often each number in the first list appears in the second.
Part 1: Distance Between Sorted Lists¶
To solve Part 1, we need to:
- Sort both lists.
- Compare each corresponding pair of numbers.
- Sum the absolute differences between the pairs.
Part 2: Calculating the Similarity Score¶
For Part 2, we calculate a similarity score by:
- Adding up each number from the left list.
- Multiplying each number by how many times it appears in the right list.
Santa's Ingenious Solutions¶
using AdventOfCode.Lib;
namespace AdventOfCode.Y2024;
[AocPuzzle(2024, 1, "Historian Hysteria", "C#")]
public sealed class Day01 : IAocDay<int>
{
public static int Part1(AocInput input) => input.AllLines
.Pipe(lines =>
{
var left = Instructions(lines, 0);
var right = Instructions(lines, 1);
return left.Zip(right, (l, r) => Math.Abs(l - r)).Sum();
});
public static int Part2(AocInput input) => input.AllLines
.Pipe(lines =>
{
var left = Instructions(lines, 0);
var counts = Instructions(lines, 1).CountBy(i => i).ToDictionary();
return left.Sum(id => counts.GetValueOrDefault(id) * id);
});
private static IEnumerable<int> Instructions(IEnumerable<string> lines, int col) =>
from line in lines
let nums = line.Split(" ").Select(int.Parse).ToArray()
orderby nums[col]
select nums[col];
}
open AdventOfCode.Lib
[<AocPuzzle(2024, 1, "Historian Hysteria", "F#")>]
module Fay01 =
let instructions (lines: string seq) (col: int) =
lines
|> Seq.map (fun line -> line.Split " " |> Array.map int)
|> Seq.sortBy (fun nums -> nums[col])
|> Seq.map (fun nums -> nums[col])
let part1 (input: AocInput) =
let column = instructions input.Lines
let left = column 0
let right = column 1
Seq.zip left right |> Seq.map (fun x -> abs (fst x - snd x)) |> Seq.sum
let part2 (input: AocInput) =
let column = instructions input.Lines
let left = column 0
let counts = column 1 |> Seq.countBy id |> Map.ofSeq
let getCount id =
Map.tryFind id counts |> Option.defaultValue 0
left |> Seq.sumBy (fun id -> getCount id * id)
from collections import Counter
def instructions(lines: list[str], col: int) -> list[int]:
return sorted(int(line.split(" ")[col]) for line in lines)
def part1(input: AocInput) -> int:
return sum(abs(l - r) for l, r in zip(instructions(input.lines, 0), instructions(input.lines, 1)))
def part2(input: AocInput) -> int:
left = instructions(input.lines, 0)
counts = Counter(instructions(input.lines, 1))
return sum([counts[id] * id for id in left])
Santa's & Elves' Reflections¶
Day 1 of Advent of Code 2024 introduced an interesting puzzle, involving sorting and comparing lists of historical location IDs.
Check out the repository for solutions: AdventOfCode & AdventOfCode.Lib.