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

  • @[email protected]
    link
    fedilink
    English
    2
    edit-2
    7 months ago

    Raku

    I’m trying warm up to Raku again.

    Solution

    github

    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]
    link
    fedilink
    27 months 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]
    link
    fedilink
    37 months 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]
    link
    fedilink
    27 months ago

    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]
      link
      fedilink
      27 months ago

      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]
    link
    fedilink
    1
    edit-2
    7 months 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]
    link
    fedilink
    17 months 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]
    link
    fedilink
    37 months ago

    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]
      link
      fedilink
      English
      1
      edit-2
      7 months ago

      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]
        link
        fedilink
        17 months 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]
    link
    fedilink
    English
    37 months 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]
    link
    fedilink
    7
    edit-2
    7 months 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]
    link
    fedilink
    1
    edit-2
    6 months 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]
    link
    fedilink
    16 months ago

    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
    
  • Sleepless One
    link
    fedilink
    English
    2
    edit-2
    7 months 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]
    link
    fedilink
    37 months 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

  • TunaCowboy
    link
    fedilink
    1
    edit-2
    7 months 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()