rwdf

joined 6 months ago
[–] rwdf 1 points 2 weeks ago

Are you trying to reach a URL on the same host you're ssh-ing to? That would create some interesting effects.

Especially since it works the second time it could mean that the second time you're actually on the host and ssh-ing to the host itself and then curling localhost.

[–] rwdf 1 points 2 weeks ago* (last edited 2 weeks ago) (1 children)

Ah, I see. ~~I guess they get different contexts or something?~~ (Edit: I re-read your post and this does not make any sense :)) What if you chain the ssh command and the curl using &&?

[–] rwdf 1 points 2 weeks ago (1 children)

Someone else suggested the env vars arent being expanded correctly inside the $(curl ...), which could be the culprit ... If a straight up URL works that would indicate that something like that is happening.

That said, I just tried setting an env var called URL="" and curling it, and curl said exit code 2, no URL specified, so something else is going on here.

[–] rwdf 1 points 2 weeks ago (10 children)

Exit code 7 means curl couldn't connect to the host, so I would try just curling a URL you know is valid directly, not setting it as an env var, to see what happens then.

[–] rwdf 1 points 2 weeks ago (12 children)

What exit code so you get from curl?

[–] rwdf 1 points 2 weeks ago* (last edited 2 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}")

[–] rwdf 0 points 2 weeks ago

It was actually a line break that broke the regex. Changing from a "." to "[\s\S]" fixed it.

[–] rwdf 5 points 2 weeks ago (1 children)

Yes, I guess I held myself to too high expectations. I haven't even studied CS and learned programming on my own... I've been trying to do some exercises and a few courses on algorithms but of course that can't be compared to going to university. Thanks for the tip, I won't spend hours in frustration in the coming days for sure. Maybe I should post my attempts even though they are incomplete or fail, to spark discussion.

[–] rwdf 1 points 2 weeks ago (1 children)

Ah, yes, that's it. The lazy solution would be to add a "do()" to the end of the input, right? Haha

[–] rwdf 2 points 2 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
}
[–] rwdf 2 points 2 weeks ago* (last edited 2 weeks ago) (3 children)

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

[–] rwdf 4 points 2 weeks ago (3 children)

Thanks. I felt very deflated after struggling with something seemingly so simple. I was using Go and couldn't figure out part 2, at least without nesting loops three layers deep.

Today I decided to try to learn Elixir instead and allowed myself some assistance from Claude.ai, which was much more fun.

325
Pondering my orb (lemmy.world)
submitted 3 weeks ago by rwdf to c/ergomechkeyboards
 

Got my trackball today.

83
submitted 3 weeks ago* (last edited 3 weeks ago) by rwdf to c/dull_mens_club
 

I was out running last night and I came back after the kid had fallen asleep, so I needed to wait until this morning. It only takes an hour, so I can hang the clothes to dry before going to the office.

42
Going to bed at eight (self.dull_mens_club)
submitted 1 month ago* (last edited 1 month ago) by rwdf to c/dull_mens_club
 

I'm going to read my book for a bit.

 

So I recently got an e-reader and have started collecting e-books for it, but a lot of books seem to only be available through Amazon/Kindle. I don't want anything to do with that company. Where do I even start looking? I have my local library apps and have scoured Project Gutenberg and some similar sites. While this is great for classics and older stuff, I want newer books too, specifically science fiction and fantasy. Have looked at author's websites but they typically link to Amazon or physical copies.

3
Teen Lord (self.fakebandnames)
submitted 2 months ago by rwdf to c/fakebandnames
 

smdh that fool thinks only of himself

4
Virgin Wizard (self.fakebandnames)
submitted 3 months ago by rwdf to c/fakebandnames
3
Sanitary (self.fakebandnames)
submitted 3 months ago by rwdf to c/fakebandnames
 

A south American metal band, I think

view more: next ›