mirror of
				https://github.com/onyx-and-iris/aoc2024.git
				synced 2025-10-31 04:51:46 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			31 lines
		
	
	
		
			370 B
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			31 lines
		
	
	
		
			370 B
		
	
	
	
		
			Go
		
	
	
	
	
	
| package two
 | |
| 
 | |
| import (
 | |
| 	"bytes"
 | |
| )
 | |
| 
 | |
| const empty = -1
 | |
| 
 | |
| func Solve(buf []byte) (int, error) {
 | |
| 	r := bytes.NewReader(buf)
 | |
| 	expandedRaw, err := parseLines(r)
 | |
| 	if err != nil {
 | |
| 		return 0, err
 | |
| 	}
 | |
| 
 | |
| 	disk := newDisk(expandedRaw)
 | |
| 	disk.defragment()
 | |
| 	disk.write()
 | |
| 
 | |
| 	var sum int
 | |
| 	for i, n := range disk.data {
 | |
| 		if n == empty {
 | |
| 			continue
 | |
| 		}
 | |
| 
 | |
| 		sum += i * n
 | |
| 	}
 | |
| 
 | |
| 	return sum, nil
 | |
| }
 |