Skip Navigation

morrowind

@morrowind.lemmy.ml.ap.tdpl.io

4 Posts13 Comments0 Memberships

Joined November 2026

If you're here, there's still hope for the internet

Don't let it fall

🌉 bridged from @morrowind@lemmy.ml by Tidepool

  • advent_of_code 🍽 - 2026 DAY 2 SOLUTIONS - 🍽

    Iterating on your solution in crystal (should be readable even if you're not familiar with crystal), I've found an even cleaner solution, that requires no passing back to part 1, nor any string construction:

     ruby
    def find_longest(input : Array(Char))
    	last = input.max_of { |c| "rvbgyo".index!(c) } # assumes non-empty input of only roygbv letters
    	
    	6 * input.size - last
    end

    runs in about 10us, excluding io, though it's possible to get it under 1us by skipping the index call and Array(Char) allocation

    1 0
  • advent_of_code 🍽 - 2026 DAY 2 SOLUTIONS - 🍽

    That's actually hella clever, now I feel dump for doing it iteratively.

    I understand the logic behind your part 2, how you'd always end up with groups of repeated letters, going backwards, but I'm confused by your statement "slightly odd sorting of the input". What does this have to do with sorting?

    3 1
  • advent_of_code 🍽 - 2026 DAY 2 SOLUTIONS - 🍽

    Crystal

    Judging by other comments here there may be a better way to do part two

     ruby
    PATTERN = "roygbv".chars
    input = File.read("input.txt")
    
    p input.chars.unshift('r').each_cons_pair.sum {|p| dist p} # part 1
    
    p find_longest input # part 2
    
    def dist(chars : Tuple(Char, Char))
    	cur_i = PATTERN.index!(chars[0])
    	target_i = PATTERN.index!(chars[1])
    
    	return target_i - cur_i + (cur_i >= target_i ? 6 : 0)
    end
    
    
    def find_longest(input : String)
    	available = input.chars.tally
    	sum = 0
    	current = 'r'
    
    	input.size.times do
    		i = PATTERN.index!(current)
    
    		(1..6).reverse_each do |j|
    			next_way = PATTERN[(i + j) % 6]
    			found = available.fetch next_way, 0
    
    			next if found <= 0
    			
    			available[next_way] = found - 1
    			sum += j
    			current = next_way
    			break
    		end
    	end
    
    	return sum
    end
    3 0
  • advent_of_code 🧸 - 2026 DAY 1 SOLUTIONS - 🧸

    ProseScript

     
    day one involves getting the product of the most and least common three ids in a list.
    
    start by reading the input
    Const FS = require("fs")
    Const Input = FS.readFileSync("input.txt", "utf-8");
    
    define a handy Function to get the Product of ([Number, Number][] an Array of pairs) {
      Return Array.reduce(Accumulator [Value] => Accumulator * Value, initial value of 1)
    }
    
    
    Function Solve lets us pass in th real or test (String Input) {
    
      to figure how common each id is, we need a 
      Const Tally of each = which we'll store in a New Map< from the id Number, to its count Number >()
    
      For Const Line Of Input.trim().split(on newlines including windows carriage returns /\r?\n/) {
        Const N = Number(convert Line)
    
        Tally.set(the count at N, to (Tally.get the current count at (N) ?? or start at 0) incremented + 1)
      }
    
      now to get most/least common we need them 
      Const Sorted = so [expand ...Tally into an array bc maps are unordered] then
        .sort(A B => A[1] - B[1] gives a negative when first is greater; which makes it descending)
    
      Return [
        part one: Product(Sorted.slice(0, 3)), most common
        part two: Product(Sorted.slice(-3)) least common
      ]
    };
    
    Console.log(Solve(TEST));
    Console.log(Solve(Input));
    3 0
  • advent_of_code 🧸 - 2026 DAY 1 SOLUTIONS - 🧸

    Odin

     odin
    package main
    
    import "core:fmt"
    import "core:os"
    import "core:strings"
    import "core:strconv"
    import "core:slice"
    
    Entry :: struct { value, count: int }
    
    main :: proc() {
    	bytes, err := os.read_entire_file("input.txt", context.allocator)
    	assert(err == nil)
    	defer delete(bytes)
    
    	input := string(bytes)
    
    	counts := make(map[int]int)
    	defer delete(counts)
    
    	for num in strings.split_lines(input) {
    		converted := strconv.parse_int(num) or_continue
    		counts[converted] += 1
    	}
    
    	entries := make([dynamic]Entry)
    	defer delete(entries)
    
    	for key, val in counts { append(&entries, Entry{key, val}) }
    
    	slice.sort_by(entries[:], proc(a, b: Entry) -> bool {
    		return a.count > b.count 
    	})
    
    	fmt.println("Entries:", entries)
    
    	prod := 1
    	for e in entries[:3] {prod *= e.value}
    	fmt.println("Part 1:", prod)
    
    	prod = 1
    	for e in entries[len(entries)-3:] {prod *= e.value}
    	fmt.println("Part 2:", prod)
    }
    2 0
  • advent_of_code 🧸 - 2026 DAY 1 SOLUTIONS - 🧸

    Roc

     roc
    import "input.txt" as input : Str
    
    main! = |_args| {
    	nums = input.split_on("\n").map_try(U64.from_str)?
    
    	tallies = nums.fold(Dict.empty(),	|tally, num| {
    		tally.update(num, |entry| match entry {
    				Ok(count) => Ok(count + 1)
    				Err(_) => Ok(1)
    			}
    		)
    	})
    
    	.to_list().sort_by(|a| a.1)
    
    	dbg tallies.take_last(3) .map(|t| t.0).fold(1, |acc, num| acc * num)
    	dbg tallies.take_first(3).map(|t| t.0).fold(1, |acc, num| acc * num)
    
    	Ok({})
    }
    2 0
  • advent_of_code 🧸 - 2026 DAY 1 SOLUTIONS - 🧸

    Crystal

    (more coming soon, I haven't had time to do it in a new language yet!)

     ruby
    input = File.read("input.txt")
    
    tallies = input.lines.map(&.to_i).tally.to_a.sort!{|a, b| b[1] <=> a[1]}
    
    p tallies[0..2].product(&.[0]) # part 1
    
    p tallies[-3..].product(&.[0]) # part 2
    2 0
  • advent_of_code What language are people using this year?

    I'm not opposed to low level languages when low level manipulation is the problem is what I'm solving. I've tried C and mostly enjoyed it except for the all the memory errors. Tried Rust and it felt the worst of both worlds: lots of abstraction, restriction and lost the feeling of directly manipulating the computer.

    I hate unnecessary restrictions or anything that prevents me from expressing my thoughts as directly as possible.

    But also, to be fair, Odin is as low level as Zig or C, F# lets you drop pretty low level when needed. Roc I can't comment on yet.

    1 0