• 8 Posts
  • 70 Comments
Joined 7 months ago
cake
Cake day: May 28th, 2024

help-circle





  • 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}")
    
    




  • 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
    }
    

  • Elixir

    First time writing Elixir. It’s probably janky af.

    I’ve had some help from AI to get some pointers along the way. I’m not competing in any way, just trying to learn and have fun.

    ~~Part 2 is currently not working, and I can’t figure out why. I’m trying to just remove everything from “don’t()” to “do()” and just pass the rest through the working solution for part 1. Should work, right?

    Any pointers?~~

    edit; working solution:

    defmodule Three do
      def get_input do
        File.read!("./input.txt")
      end
    
      def extract_operations(input) do
        Regex.scan(~r/mul\((\d{1,3}),(\d{1,3})\)/, input)
        |> Enum.map(fn [_op, num1, num2] ->
          num1 = String.to_integer(num1)
          num2 = String.to_integer(num2)
          [num1 * num2]
        end)
      end
    
      def sum_products(ops) do
        List.flatten(ops)
        |> Enum.filter(fn x -> is_integer(x) end)
        |> Enum.sum()
      end
    
      def part1 do
        extract_operations(get_input())
        |> sum_products()
      end
    
      def part2 do
        String.split(get_input(), ~r/don\'t\(\)[\s\S]*?do\(\)/)
        |> Enum.map(&extract_operations/1)
        |> sum_products()
      end
    end
    
    IO.puts("part 1: #{Three.part1()}")
    IO.puts("part 2: #{Three.part2()}")
    
    






  • I like it a lot, but as you say it takes a lot of getting used to and practice. I don’t think my WPM is near what it is on a regular keyboard, but we’re talking a few weeks of use versus 30 years of muscle memory … I’m gradually switching over to the Glove. Funnily enough I write better on it when I’m not thinking about it at all.