Day 1: Dusty Shed

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://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

  • morrowind@lemmy.ml
    link
    fedilink
    arrow-up
    2
    ·
    19 hours ago

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