this post was submitted on 01 Dec 2024
51 points (98.1% liked)

Advent Of Code

981 readers
28 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 1: Historian Hysteria

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://blocks.programming.dev/ if you prefer sending it through a URL

FAQ

top 39 comments
sorted by: hot top controversial new old
[–] [email protected] 7 points 3 weeks ago

Haskell

Plenty of scope for making part 2 faster, but I think simple is best here. Forgot to sort the lists in the first part, which pushed me waaay off the leaderboard.

import Data.List

main = do
  [as, bs] <- transpose . map (map read . words) . lines <$> readFile "input01"
  print . sum $ map abs $ zipWith (-) (sort as) (sort bs)
  print . sum $ map (\a -> a * length (filter (== a) bs)) as
[–] [email protected] 7 points 3 weeks ago* (last edited 3 weeks ago)

Uiua

For entertainment purposes only, I'll be trying a solution in Uiua each day until it all gets too much for me...

$ 3   4
$ 4   3
$ 2   5
$ 1   3
$ 3   9
$ 3   3
βŠœβˆ˜βŠΈβ‰ @\n     # Partition at \n.
⊜(β†βˆ΅β‹•)βŠΈβ‰ @\s # Partition at space, parse ints, sort.

&p/+/(⌡-). # Part1 : Get abs differences, sum, print.

&p/+Γ—βŸœ(/+β‰β‰‘βŒ•)Β°βŠ‚ # Part 2 : Count instances, mul out, sum, print.
[–] [email protected] 4 points 3 weeks ago

Rust

Right IDs are directly read into a hash map counter.

use std::str::FromStr;
use std::collections::HashMap;

fn part1(input: String) {
    let mut left = Vec::new();
    let mut right = Vec::new();
    for line in input.lines() {
        let mut parts = line.split_whitespace()
            .map(|p| u32::from_str(p).unwrap());
        left.push(parts.next().unwrap());
        right.push(parts.next().unwrap());
    }
    left.sort_unstable();
    right.sort_unstable();
    let diff: u32 = left.iter().zip(right)
        .map(|(l, r)| l.abs_diff(r))
        .sum();
    println!("{diff}");
}

fn part2(input: String) {
    let mut left = Vec::new();
    let mut right: HashMap<u32, u32> = HashMap::new();
    for line in input.lines() {
        let mut parts = line.split_whitespace()
            .map(|p| u32::from_str(p).unwrap());
        left.push(parts.next().unwrap());
        *right.entry(parts.next().unwrap()).or_default() += 1;
    }
    let similar: u32 = left.iter()
        .map(|n| n * right.get(n).copied().unwrap_or_default())
        .sum();
    println!("{similar}");
}

util::aoc_main!();
[–] [email protected] 4 points 3 weeks ago* (last edited 3 weeks ago)

Nim

I've got my first sub-1000 rank today (998 for part 2). Yay!
Simple and straightforward challenge, very fitting for 1st day. Gonna enjoy it while it lasts.

proc solve(input: string): AOCSolution[int, int] =
  var l1,l2: seq[int]
  for line in input.splitLines():
    let pair = line.splitWhitespace()
    l1.add parseInt(pair[0])
    l2.add parseInt(pair[1])
  l1.sort()
  l2.sort()

  block p1:
    for i in 0..l1.high:
      result.part1 += abs(l1[i] - l2[i])

  block p2:
    for n in l1:
      result.part2 += n * l2.count(n)

Codeberg repo

[–] [email protected] 4 points 3 weeks ago* (last edited 3 weeks ago)

TypeScript

Solution

import { AdventOfCodeSolutionFunction } from "./solutions";

function InstancesOf(sorted_array: Array<number>, value: number) {
    const index = sorted_array.indexOf(value);
    if(index == -1)
        return 0;

    let sum = 1;

    for (let array_index = index + 1; array_index < sorted_array.length; array_index++) {
        if(sorted_array[array_index] != value)
            break;

        sum += 1;
    }

    return sum;
}

export const solution_1: AdventOfCodeSolutionFunction = (input) => {
    const left: Array<number> = [];
    const right: Array<number> = [];

    const lines = input.split("\n");

    for (let index = 0; index < lines.length; index++) {
        const element = lines[index].trim();
        if(!element)
            continue;

        const leftRight = element.split("   ");
        left.push(Number(leftRight[0]));
        right.push(Number(leftRight[1]));
    }

    const numSort = (a: number, b: number) => a - b;
    left.sort(numSort);
    right.sort(numSort);

    let sum = 0;
    for (let index = 0; index < left.length; index++) {
        const leftValue = left[index];
        const rightValue = right[index];

        sum += Math.abs(leftValue - rightValue);
    }

    const part1 = `Part 1: ${sum}`;

    sum = 0;
    for (let index = 0; index < left.length; index++) {
        sum += left[index] * InstancesOf(right, left[index]);
    }

    const part2 = `Part 2: ${sum}`;

    return `${part1}\n${part2}`;
};

Not the most elegant solution but it works. Decided to reuse the array since it is sorted for both sides.

[–] [email protected] 3 points 3 weeks ago (1 children)

Kotlin

No πŸ’œ for Kotlin here?

import kotlin.math.abs

fun part1(input: String): Int {
    val diffs: MutableList<Int> = mutableListOf()
    val pair = parse(input)
    pair.first.sort()
    pair.second.sort()
    pair.first.forEachIndexed { idx, num ->
        diffs.add(abs(num - pair.second[idx]))
    }
    return diffs.sum()
}

fun part2(input: String): Int {
    val pair = parse(input)
    val frequencies = pair.second.groupingBy { it }.eachCount()
    var score = 0
    pair.first.forEach { num ->
        score += num * frequencies.getOrDefault(num, 0)
    }
    return score
}

private fun parse(input: String): Pair<MutableList<Int>, MutableList<Int>> {
    val left: MutableList<Int> = mutableListOf()
    val right: MutableList<Int> = mutableListOf()
    input.lines().forEach { line ->
        if (line.isNotBlank()) {
            val parts = line.split("\\s+".toRegex())
            left.add(parts[0].toInt())
            right.add(parts[1].toInt())
        }
    }
    return left to right
}
[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

I have another Kotlin (albeit similar) solution:

import kotlin.math.abs

fun main() {

    fun getLists(input: List<String>): Pair<List<Int>, List<Int>> {
        val unsortedPairs = input.map {
            it.split("   ").map { it.toInt() }
        }

        val listA = unsortedPairs.map { it.first() }
        val listB = unsortedPairs.map { it.last() }
        return Pair(listA, listB)
    }

    fun part1(input: List<String>): Int {
        val (listA, listB) = getLists(input)

        return listA.sorted().zip(listB.sorted()).sumOf { abs(it.first - it.second) }
    }

    fun part2(input: List<String>): Int {
        val (listA, listB) = getLists(input)

        return listA.sumOf { number ->
            number * listB.count { it == number }
        }
    }

    // Or read a large test input from the `src/Day01_test.txt` file:
    val testInput = readInput("Day01_test")
    check(part1(testInput) == 11)
    check(part2(testInput) == 31)

    // Read the input from the `src/Day01.txt` file.
    val input = readInput("Day01")
    part1(input).println()
    part2(input).println()
}

It's a bit more compact. (If you take out the part that actually calls the functions on the (test-)input.)

[–] [email protected] 2 points 3 weeks ago

Thanks! I like the Pair destruction and zip().sumOf() approach. I'm relatively new to Kotlin, so this is a good learning experience. πŸ˜…

[–] [email protected] 3 points 3 weeks ago (1 children)

Solution in C

Part 1 is a sort and a quick loop. Part 2 could be efficient with a lookup table but it was practically instant with a simple non-memoized scan so left it that way.

[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago) (1 children)

You are using some interesting techniques there. I never imaged you could use the result of == for adding to a counter.

But how did you handle duplicates in part 2?

[–] [email protected] 1 points 3 weeks ago

I’m not sure if I understand the question correctly but for every number in the left array I count in the right array. That means duplicate work but shrug πŸ˜…

[–] [email protected] 3 points 3 weeks ago

C#

public class Day01 : Solver
{
  private ImmutableArray<int> left;
  private ImmutableArray<int> right;

  public void Presolve(string input)
  {
    var pairs = input.Trim().Split("\n").Select(line => Regex.Split(line, @"\s+"));
    left = pairs.Select(item => int.Parse(item[0])).ToImmutableArray();
    right = pairs.Select(item => int.Parse(item[1])).ToImmutableArray();
  }

  public string SolveFirst() => left.Sort().Zip(right.Sort()).Select((pair) => int.Abs(pair.First - pair.Second)).Sum().ToString();

  public string SolveSecond() => left.Select((number) => number * right.Where(v => v == number).Count()).Sum().ToString();
}
[–] [email protected] 3 points 3 weeks ago

I’m quite inexperienced as a programmer, I learned most of the basic concepts from playing human resource machine and 7 billion humans. After mucking about writing some CLI utilities in Perl and python, I’ve decided to give rust a go.

Part 1

Part 2

[–] [email protected] 3 points 3 weeks ago

Smalltalk

day1p12: input    
	| list1 list2 nums dist sim |
	
	list1 := OrderedCollection new.
	list2 := OrderedCollection new.
	
	input linesDo: [ :l |
		nums := l substrings collect: [ :n | n asInteger ].
		list1 add: (nums at: 1).
		list2 add: (nums at: 2).
	].

	list1 sort.
	list2 sort.
	
	dist := 0.
	list1 with: list2 do: [ :a :b | dist := dist + (a - b) abs ].
	
	sim := list1 sumNumbers: [ :x | x * (list2 occurrencesOf: x) ].
	
	^ Array with: dist with: sim.
[–] [email protected] 3 points 3 weeks ago

Python

Part 1

left_list = []
right_list = []

for line in file:
    split_line = line.split()
    left_list.append(int(split_line[0]))
    right_list.append(int(split_line[1]))

sorted_left = sorted(left_list)
sorted_right = sorted(right_list)
distance = []
for left, right in zip(sorted_left, sorted_right):
    distance.append(abs(left - right))

total = sum(distance)

print(total)

Part 2

file = open('input.txt', 'r')
left_list = []
right_list = []

for line in file:
    split_line = line.split()
    left_list.append(int(split_line[0]))
    right_list.append(int(split_line[1]))

sim_score = 0
for item in left_list:
    sim = right_list.count(item)
    sim_score += (sim * item)

print(sim_score)

I am sure there were better ways to do this, this was just the first way in my head, in the order it appeared

[–] [email protected] 3 points 3 weeks ago

Not going to push hard on these first days (fever being a reason), so I slept in quite a bit before looking at the problem.

C#

List<int> _LeftList = new List<int>();
List<int> _RightList = new List<int>();

// Fed via File.ReadLines(...).Select(l => l.Trim())
public void Input(IEnumerable<string> lines)
{
  foreach (var line in lines)
  {
    var split = line.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(s => int.Parse(s));
    _LeftList.Add(split.First());
    _RightList.Add(split.Last());
  }
}

public void Part1()
{
  Console.WriteLine($"Sum: {_LeftList.Order().Zip(_RightList.Order()).Select(v => Math.Abs(v.First - v.Second)).Sum()}");
}
public void Part2()
{
  Console.WriteLine($"Sum: {_LeftList.Select(l => _RightList.Where(i => i == l).Count() * l).Sum()}");
}

[–] [email protected] 2 points 3 weeks ago

I'm late to the party, as usual. Damned timezones. This year I'm going to tackle with a small handful of languages, but primarily Elixir and Gleam. This is my first time trying this languages in earnest, so expect some terrible, inefficient and totally unidiomatic code!
Here's day one:

Elixir

part_one =
  File.read!("input.in")
  |> String.split("\n", trim: true)
  |> Enum.map(fn line ->
    line
    |> String.split()
    |> Enum.map(&String.to_integer/1)
  end)
  |> Enum.reduce({[], []}, fn [first, second], {list1, list2} ->
    {[first | list1], [second | list2]}
  end)
  |> then(fn {list1, list2} ->
    {Enum.sort(list1), Enum.sort(list2)}
  end)
  |> then(fn {list1, list2} ->
    Enum.zip(list1, list2)
    |> Enum.map(fn {x, y} -> abs(x - y) end)
  end)
  |> Enum.sum()

part_two =
  File.read!("input.in")
  |> String.split("\n", trim: true)
  |> Enum.map(fn line ->
    line
    |> String.split()
    |> Enum.map(&String.to_integer/1)
  end)
  |> Enum.reduce({[], []}, fn [first, second], {list1, list2} ->
    {[first | list1], [second | list2]}
  end)
  |> then(fn {list1, list2} ->
    Enum.map(list1, fn line ->
      line * Enum.count(list2, fn x -> x === line end)
    end)
    |> Enum.sum()
  end)

IO.inspect(part_one)
IO.inspect(part_two)
[–] [email protected] 2 points 3 weeks ago* (last edited 3 weeks ago)

Rust

I'm doing it in Rust again this year. I stopped keeping up with it after day 3 last year, so let's hope I last longer this time around.

Solution Spoiler Alert

use std::collections::HashMap;

use crate::utils::read_lines;

pub fn solution1() {
    let (mut id_list1, mut id_list2) = get_id_lists();

    id_list1.sort();
    id_list2.sort();

    let total_distance = id_list1
        .into_iter()
        .zip(id_list2)
        .map(|(left, right)| (left - right).abs())
        .sum::<i32>();

    println!("Total distance = {total_distance}");
}

pub fn solution2() {
    let (id_list1, id_list2) = get_id_lists();

    let id_count_map = id_list2
        .into_iter()
        .fold(HashMap::<_, i32>::new(), |mut map, id| {
            *map.entry(id).or_default() += 1i32;

            map
        });

    let similarity_score = id_list1
        .into_iter()
        .map(|id| id * id_count_map.get(&id).copied().unwrap_or_default())
        .sum::<i32>();

    println!("Similarity score = {similarity_score}");
}

fn get_id_lists() -> (Vec<i32>, Vec<i32>) {
    read_lines("src/day1/input.txt")
        .map(|line| {
            let mut ids = line.split_whitespace().map(|id| {
                id.parse::<i32>()
                    .expect("Ids from input must be valid integers")
            });

            (
                ids.next().expect("First Id on line must be present"),
                ids.next().expect("Second Id on line must be present"),
            )
        })
        .unzip()
}


I'm keeping my solutions up on GitHub.

[–] [email protected] 2 points 3 weeks ago

Go

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
	"strings"
)

func main() {
	input, _ := os.Open("input.txt")
	defer input.Close()

	left, right := []int{}, []int{}

	scanner := bufio.NewScanner(input)
	for scanner.Scan() {
		line := scanner.Text()
		splitline := strings.Split(line, "   ")
		l, _ := strconv.Atoi(splitline[0])
		r, _ := strconv.Atoi(splitline[1])
		left, right = append(left, l), append(right, r)
	}

	fmt.Printf("part 1 - total diff: %d\n", part1(left, right))
	fmt.Printf("part 2 - new total: %d\n", part2(left, right))
}

func part1(left, right []int) int {
	diff := 0
	sort.Ints(left)
	sort.Ints(right)

	for i, l := range left {
		if l > right[i] {
			diff += (l - right[i])
		} else {
			diff += (right[i] - l)
		}
	}
	return diff
}

func part2(left, right []int) int {
	newTotal := 0

	for _, l := range left {
		matches := 0
		for _, r := range right {
			if l == r {
				matches++
			}
		}
		newTotal += l * matches
	}
	return newTotal
}
[–] [email protected] 2 points 3 weeks ago* (last edited 3 weeks ago)

Raku

I'm trying warm up to Raku again.

Solutiongithub

use v6;

sub MAIN($input) {
    my $file = open $input;

    grammar LocationList {
        token TOP { <row>+%"\n" "\n"* }
        token row { <left=.id> " "+ <right=.id> }
        token id { \d+ }
    }

    my $locations = LocationList.parse($file.slurp);
    my @rows = $locations<row>.map({ (.<left>.Int, .<right>.Int)});
    my $part-one-solution = (@rows[*;0].sort Z- @rows[*;1].sort)Β».abs.sum;
    say "part 1: $part-one-solution";

    my $rbag = bag(@rows[*;1].sort);
    my $part-two-solution = @rows[*;0].map({ $_ * $rbag{$_}}).sum;
    say "part 2: $part-two-solution";
}

I'm happy to see that Lemmy no longer eats Raku code.

[–] [email protected] 2 points 3 weeks ago (1 children)

Solution in ruby

This is my third program in ruby after the ruby tutorial and half of the rails tutorial, so don't expect anything too good from it.

Also i did this today since i had time, i will probably not comment every day.

[–] [email protected] 2 points 3 weeks ago (1 children)

fyi for lines 14-22 you an use .abs instead of checking for negatives and .sum instead of doing it manually. Check my crystal solution to see what I mean

[–] [email protected] 1 points 3 weeks ago

That is good to know, thank you.

[–] [email protected] 2 points 3 weeks ago

Viml

I think viml is a very fun language, i like weird languages lol, so this year im doing it in viml while trying to use as many of the original ed/ex commands as i can (:d, :p, :a, :g, ...)

Part 1

!cp ./puzzle1 ./puzzle1.editing
e ./puzzle1.editing

1,$sort
let row1 = []

g/^\d/let row1 = add(row1, str2nr(expand("<cword>"))) | norm 0dw
1d
1,$sort
g/^\d/execute 'norm cc' .. string(abs(expand("<cword>") - row1[line('.') - 1]))

$a|---ANSWER---
0
.
1,$-1g/^\d/call setline("$", str2nr(getline("$")) + str2nr(expand("<cword>")))

Part 2

read ./puzzle1

let cnt = 0
g/^\d/let cnt += expand("<cword>") *
            \ searchcount(#{pattern: '\s\+' .. expand("<cword>")}).total

echo cnt .. "\n"

w! ./puzzle1.editing

[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago)

J

NB. file handling from https://www.jsoftware.com/help/primer/files.htm
data =: > 0 ". each cutopen toJ fread '1.data'
result1 =: +/ | -/"1 |: ({~ /:)"1 |: data
NB. Lemmy insists on mangling the ampersand, the &amp; is _just_ an ampersand
result2 =: +/ , ((0&amp;{) * (="0 1)/) |: data
[–] [email protected] 1 points 3 weeks ago

TypeScript

This is for part #2 only.

import { readFileSync } from 'fs'

const f = readFileSync('./input.txt', 'utf-8')
const lines = f.split("\n")

let rights = {}
for (const i in lines) {
	if (lines[i] == '') { continue }

	const [, right] = lines[i].split(/\s+/)
	if (rights[right] === undefined) {
		rights[right] = 0
	}

	rights[right]++
}

let ans = 0
for (const i in lines) {
	const [left] = lines[i].split(/\s+/)
	const similarity = rights[left]

	if (similarity) {
		ans += (Number(left) * rights[left])
	}
}

console.dir(ans)

Is it possible to get this more efficient? I would love a way that only required iterating over the list once, but I don't really have the focus to puzzle it out any less than O(2n) (probably more than that, even, if you count reading in the data...).

[–] [email protected] 1 points 3 weeks ago
[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago)

Crystal

f = ARGV[0]? ? File.read_lines("input.txt") : test.lines
list1 = Array(Int32).new(f.size)
list2 = Array(Int32).new(f.size)

f.each do |l|
	nums = l.split.map(&.to_i)
	list1.push(nums[0])
	list2.push(nums[1])
end

list1.sort!
list2.sort!

puts list1.zip(list2).sum{ |l1, l2| (l1 - l2).abs }

puts list1.sum {|x| x * list2.count x}
[–] [email protected] 1 points 3 weeks ago

C#

using System;
using System.Linq;

public record Point(int X, int Y); 

static class Program
{
    static async Task Main(string[] args)
    {
        var data = (await ReadInputFromFile("data.txt")).ToArray();

        var part1Answer = CalculateTotalDifference(data);
        Console.WriteLine($"Part 1 = {part1Answer}");

        var part2Answer = CountFrequencies(data);
        Console.WriteLine($"Part 2 = {part2Answer}");
    }

    public static int CountFrequencies(ICollection<Point> points)
    {
        var freq = points
            .GroupBy(p => p.Y)
            .ToDictionary(g => g.Key, g => g.Count());
        return points
            .Sum(p => freq.GetValueOrDefault(p.X, 0) * p.X);
    }

    public static int CalculateTotalDifference(ICollection<Point> points)
        => points.OrderBy(p => p.X)
            .Zip(
                points.OrderBy(p => p.Y),
                (px, py) => Math.Abs(px.X - py.Y))
            .Sum();

    public static readonly char[] Delimiter = new char[] { ' ' };
    public static async Task<IEnumerable<Point>> ReadInputFromFile(string path)
        => (await File.ReadAllLinesAsync(path))
            .Select(l =>
            {
                var parts = l.Split(
                    Delimiter,
                    StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
                return new Point(int.Parse(parts[0]), int.Parse(parts[1]));
            });
}
[–] [email protected] 1 points 2 weeks ago* (last edited 2 weeks ago)

Uiua

Decided to try and use Uiua for each day this year. At least I'm not the only one to get this idea ^^

Run with example input here

PartOne ← (
  &rs ∞ &fo "input-1.txt"
  ⊜(βŠœβ‹•β‰ @ .)β‰ @\n.
  ≑⍆⍉
  ⌡/-
  /+
)

PartTwo ← (
  &rs ∞ &fo "input-1.txt"
  ⊜(βŠœβ‹•β‰ @ .)β‰ @\n.
  βŠ’βŸœβŠ£β‰
  0
  ⍒(+βŠ™(:βŠ™(Γ—β§»βŠšβ—‘βŒ•)β†˜1⟜⊒)|β‹…(β‰ 0⧻))
  βŠ™(β—Œβ—Œ) # just cleaning up the stack
)

&p "Day 1:"
&pf "Part 1: "
&p PartOne
&pf "Part 2: "
&p PartTwo

[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago)

Elixir

Total noob, but it's fun to learn.

{left, right} =
  File.read!("./input.txt")
  |> String.split("\n", trim: true)
  |> Enum.map(fn line ->
    String.split(line)
    |> Enum.map(&String.to_integer/1)
    |> List.to_tuple()
  end)
  |> Enum.unzip()
  |> then(fn {left, right} ->
    {Enum.sort(left), Enum.sort(right)}
  end)

diffs =
  Enum.zip(left, right)
  |> Enum.map(fn {l, r} -> abs(l - r) end)
  |> Enum.sum()

freqs =
  Enum.filter(right, fn r -> r in left end)
  |> Enum.frequencies()

freqsum =
  Enum.map(left, fn n ->
    freq = Map.get(freqs, n, 0)
    n * freq
  end)
  |> Enum.sum()

IO.puts("part 1: #{diffs}")
IO.puts("part 2: #{freqsum}")

[–] [email protected] 1 points 3 weeks ago

JavaScript

After writing a procedural to-the-point version in C, tried a JavaScript solution too because it's just perfect for list comprehension. The part 2 search is inefficient but the data size is small.

Code

const fs = require("fs");
const U = require("./util");

const pairs = fs
    .readFileSync(process.argv[2] || process.stdin.fd, "utf8")
    .split("\n")
    .filter(x => x != "")
    .map(x => x.split(/ +/).map(Number));

const ls = pairs.map(x => x[0]); ls.sort();
const rs = pairs.map(x => x[1]); rs.sort();

const p1 = U.sum(ls.map((l, i) => Math.abs(l - rs[i])));
const p2 = U.sum(ls.map(l => l * U.count(rs, l)));

console.log("01:", p1, p2);

https://github.com/sjmulder/aoc/blob/master/2024/js/day01.js

[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago)

Factor

: get-input ( -- left-list right-list )
  "vocab:aoc-2024/01/input.txt" utf8 file-lines
  [ split-words harvest ] map unzip
  [ [ string>number ] map ] bi@ ;

: part1 ( -- n )
  get-input
  [ sort ] bi@
  [ - abs ] 2map-sum ;

: part2 ( -- n )
  get-input
  histogram
  '[ dup _ at 0 or * ] map-sum ;

https://github.com/AndydeCleyre/aoc-2024

[–] [email protected] 1 points 3 weeks ago

Haskell

import Control.Arrow
import Control.Monad
import Data.List
import Data.Map

part1 [a, b] = sum $ abs <$> zipWith (-) (sort a) (sort b)
part2 [a, b] = sum $ ap (zipWith (*)) (fmap (flip (findWithDefault 0) (freq b))) a
  where
    freq = fromListWith (+) . fmap (,1)

main = getContents >>= (print . (part1 &&& part2)) . transpose . fmap (fmap read . words) . lines
[–] [email protected] 1 points 3 weeks ago

Zig

const std = @import("std");
const List = std.ArrayList;
const Map = std.AutoHashMap;

const splitSeq = std.mem.splitSequence;
const splitScalar = std.mem.splitScalar;
const parseInt = std.fmt.parseInt;
const print = std.debug.print;
const sort = std.sort.block;

var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();

const Answer = struct {
    distance: u32,
    similarity: u32,
};

fn lessThan(_: void, lhs: []const u8, rhs: []const u8) bool {
    return std.mem.lessThan(u8, lhs, rhs);
}

pub fn solve(input: []const u8) !Answer {
    var rows = splitScalar(u8, input, '\n');
    var left_list = List([]const u8).init(alloc);
    defer left_list.deinit();
    var right_list = List([]const u8).init(alloc);
    defer right_list.deinit();

    // PART 1

    // split the rows into two lists
    while (rows.next()) |row| {
        var sides = splitSeq(u8, row, "   ");
        try left_list.append(sides.next() orelse break);
        try right_list.append(sides.next() orelse break);
    }
    _ = left_list.pop(); // last null

    // sort both lists
    sort([]const u8, left_list.items, {}, lessThan);
    sort([]const u8, right_list.items, {}, lessThan);

    var distance: u32 = 0;
    for (left_list.items, right_list.items) |left, right| {
        distance += @abs(try parseInt(i32, left, 10) - try parseInt(i32, right, 10));
    }

    // PART 2
    var right_scores = Map(i32, u32).init(alloc);
    defer right_scores.deinit();

    // count number of item appearances in the right list
    for (right_list.items) |item| {
        const value = try parseInt(i32, item, 10);
        const result = try right_scores.getOrPut(value);
        if (!result.found_existing) {
            result.value_ptr.* = 1;
        } else {
            result.value_ptr.* += 1;
        }
    }

    // sum up similarity between items in left list and right list scores
    var similarity: u32 = 0;
    for (left_list.items) |item| {
        const value = try parseInt(i32, item, 10);
        const result = right_scores.get(value) orelse 0;
        similarity += @as(u32, @intCast(value)) * result;
    }
    return Answer{ .distance = distance, .similarity = similarity };
}

pub fn main() !void {
    const answer = try solve(@embedFile("input.txt"));
    print("Part 1: {d}\n", .{answer.distance});
    print("Part 2: {d}\n", .{answer.similarity});
}

test "test input" {
    const answer = try solve(@embedFile("test.txt"));
    try std.testing.expectEqual(answer.distance, 11);
    try std.testing.expectEqual(answer.similarity, 31);
}

[–] [email protected] 1 points 3 weeks ago* (last edited 3 weeks ago)

python

I didn't realize it was december until this afternoon. I've generally chosen a new or spartan lang to challenge myself, but I'm going easy mode this year with python and just focusing on meeting the requirement.

solution

import aoc

def setup():
    lines = aoc.get_lines(1)
    l = [int(x.split()[0]) for x in lines]
    r = [int(x.split()[1]) for x in lines]
    return (l, r, 0)

def one():
    l, r, acc = setup()
    for p in zip(sorted(l), sorted(r)):
        acc += abs(p[0] - p[1])
    print(acc)

def two():
    l, r, acc = setup()
    for n in l:
        acc += n * r.count(n)
    print(acc)

one()
two()

[–] [email protected] 1 points 3 weeks ago

Python

ids = """<multiline input string>"""
ids = [pair.split(" ") for pair in ids.split("\n")]
left = [int(t[0]) for t in ids]
right = [int(t[-1]) for t in ids]
left.sort()
right.sort()
print(sum(abs(l - r) for l, r in zip(left, right)))

# 2
s = 0
for l in left:
    s += right.count(l) * l
print(s)

Lost a minute because I forgot about abs .-.

[–] [email protected] 1 points 2 weeks ago (1 children)

Elixir

defmodule AdventOfCode.Solution.Year2024.Day01 do
  use AdventOfCode.Solution.SharedParse

  @impl true
  def parse(input) do
    numbers =
      input
      |> String.split("\n", trim: true)
      |> Enum.map(fn l -> String.split(l, ~r/\s+/) |> Enum.map(&String.to_integer/1) end)

    {Stream.map(numbers, &Enum.at(&1, 0)), Stream.map(numbers, &Enum.at(&1, 1))}
  end

  def part1({left, right}) do
    Enum.zip_reduce(Enum.sort(left), Enum.sort(right), 0, &(&3 + abs(&1 - &2)))
  end

  def part2({left, right}) do
    freq = Enum.frequencies(right)
    left |> Stream.map(&(&1 * Map.get(freq, &1, 0))) |> Enum.sum()
  end
end
[–] [email protected] 1 points 2 weeks ago

I've honestly forgotten how fucking cool the pipe operator is