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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/22323136
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465
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 }
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.)
Thanks! I like the
Pair
destruction andzip().sumOf()
approach. I’m relatively new to Kotlin, so this is a good learning experience. 😅
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 }
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
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])); }); }
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 ;
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}")
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); }
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
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.
Raku
I’m trying warm up to Raku again.
Solution
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.
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
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
I’ve honestly forgotten how fucking cool the pipe operator is