this post was submitted on 01 Dec 2024
16 points (100.0% liked)

programming

258 readers
3 users here now

  1. Post about programming, interesting repos, learning to program, etc. Let's try to keep free software posts in the c/libre comm unless the post is about the programming/is to the repo.

  2. Do not doxx yourself by posting a repo that is yours and in any way leads to your personally identifying information. Use reports if necessary to alert mods to a potential doxxing.

  3. Be kind, keep struggle sessions focused on the topic of programming.

founded 2 years ago
MODERATORS
16
Advent of Code 2024 Day 1 (adventofcode.com)
submitted 7 months ago* (last edited 7 months ago) by zongor@hexbear.net to c/programming@hexbear.net
 

Alright so im going to force myself to create a post for this daily and maybe this will help force me to actually learn Elixir this year.

Please use Spoiler tags for your code snippets if they reveal answers.

Day 1 was interesting. Elixir is different, but it kinda reminds me of doing functional JavaScript but is wayyyyy nicer. It has so many tools to do stuff and the pattern matching is very powerful.

If anyone else wants me to tag them I can do that but I think @Enjoyer_of_Games@hexbear.net was the only one.

Heres my solution for Part 1:

spoiler

"input"    
    |> File.read!()
    |> String.split("\n")
    |> Enum.map(&String.split/1)
    |> Enum.filter(fn list -> length(list) == 2 end)
    |> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
    |> Enum.unzip()
    |> Tuple.to_list()
    |> Enum.map(&Enum.sort/1)
    |> Enum.zip()
    |> Enum.map(fn {a, b} -> abs(a - b) end)
    |> Enum.sum()

~~Ill post Part 2 later if I get time.~~ I had time, heres part 2:

spoiler

"input"    
    |> File.read!()
    |> String.split("\n")
    |> Enum.map(&String.split/1)
    |> Enum.filter(fn list -> length(list) == 2 end)
    |> Enum.map(fn [a, b] -> {String.to_integer(a), String.to_integer(b)} end)
    |> Enum.unzip()
    |> Tuple.to_list()
    |> Enum.map(&Enum.sort/1)
    |> (fn [list1, list2] -> {list1, Enum.frequencies(list2)} end).()
    |> (fn {list1, freqs} ->
          list1
          |> Enum.map(&{&1, Map.get(freqs, &1, 0)})
          |> Enum.filter(fn {_, count} -> count != 0 end)
          |> Enum.map(fn {value, count} -> value * count end)
        end).()
    |> Enum.sum()

top 9 comments
sorted by: hot top controversial new old
[–] iByteABit@hexbear.net 4 points 7 months ago (1 children)

Started mine on Rust, there's this really cool Github template for it too that automates a lot of stuff and provides you with a sort of scaffolding for each challenge.

[–] gay_king_prince_charles@hexbear.net 3 points 7 months ago (1 children)

Can you share? I'm also doing mine in Rust

[–] Speaker@hexbear.net 3 points 7 months ago (1 children)

Currently writing an implementation of cryptographic secret Santa, so I'm getting a late start on Advent, but would not mind being tagged so I remember to post them when I get to them.

[–] zongor@hexbear.net 3 points 7 months ago
[–] gay_king_prince_charles@hexbear.net 3 points 7 months ago (1 children)

Can you add me to the tags?

[–] zongor@hexbear.net 3 points 7 months ago
[–] kota@hexbear.net 2 points 7 months ago

Guess I'm a bit late on day one. Wound up doing mine in go, but maybe if I'm motivated I'll use something cooler later. Those elixir solutions are rad!

part 1

func main() {
	b, err := os.ReadFile("input.txt")
	if err != nil {
		log.Fatalln(err)
	}

	d, err := distance(string(b))
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(d)
}

func distance(input string) (int, error) {
	var left []int
	var right []int
	for _, line := range strings.Split(input, "\n") {
		fields := strings.Fields(line)
		if len(fields) != 2 {
			continue
		}

		l, err := strconv.Atoi(fields[0])
		if err != nil {
			return 0, err
		}
		left = append(left, l)

		r, err := strconv.Atoi(fields[1])
		if err != nil {
			return 0, err
		}
		right = append(right, r)
	}

	slices.Sort(left)
	slices.Sort(right)

	var total int
	for i := 0; i < len(left); i++ {
		total += int(math.Abs(float64(left[i] - right[i])))
	}

	return total, nil
}

part 2

func main() {
	b, err := os.ReadFile("input.txt")
	if err != nil {
		log.Fatalln(err)
	}

	s, err := similarity(string(b))
	if err != nil {
		log.Fatalln(err)
	}
	fmt.Println(s)
}

func similarity(input string) (int, error) {
	collisions := make(map[int]int)
	var left []int
	var right []int
	for _, line := range strings.Split(input, "\n") {
		fields := strings.Fields(line)
		if len(fields) != 2 {
			continue
		}

		l, err := strconv.Atoi(fields[0])
		if err != nil {
			return 0, err
		}
		left = append(left, l)
		collisions[l] = 0

		r, err := strconv.Atoi(fields[1])
		if err != nil {
			return 0, err
		}
		right = append(right, r)
	}

	for _, r := range right {
		c := collisions[r]
		collisions[r] = c + 1
	}

	var total int
	for _, l := range left {
		v := collisions[l]
		total += l * v
	}

	return total, nil
}