day 15
This commit is contained in:
		
							parent
							
								
									d93dc91117
								
							
						
					
					
						commit
						4fdfb188f5
					
				| 
						 | 
				
			
			@ -4,7 +4,8 @@ members = [
 | 
			
		|||
    "day-*"
 | 
			
		||||
]
 | 
			
		||||
exclude = [
 | 
			
		||||
    "day-04"
 | 
			
		||||
    "day-04",
 | 
			
		||||
    "day-15"
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
[workspace.dependencies]
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,23 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
var input string = "rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7"
 | 
			
		||||
 | 
			
		||||
func TestPart1(t *testing.T) {
 | 
			
		||||
	expected := 1320
 | 
			
		||||
	actual := part1(input)
 | 
			
		||||
 | 
			
		||||
	if actual != expected {
 | 
			
		||||
		t.Errorf("part1 = %d, expected %d", actual, expected)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestPart2(t *testing.T) {
 | 
			
		||||
	expected := 145
 | 
			
		||||
	actual := part2(input)
 | 
			
		||||
 | 
			
		||||
	if actual != expected {
 | 
			
		||||
		t.Errorf("part2 = %d, expected %d", actual, expected)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,3 @@
 | 
			
		|||
module adventofcode/2023/day-15
 | 
			
		||||
 | 
			
		||||
go 1.21.5
 | 
			
		||||
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
						 | 
				
			
			@ -0,0 +1,19 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	data, err := os.ReadFile("./input.txt")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		panic(err)
 | 
			
		||||
	}
 | 
			
		||||
	input := string(data)
 | 
			
		||||
 | 
			
		||||
	p1 := part1(input)
 | 
			
		||||
	fmt.Printf("part1: %d\n", p1)
 | 
			
		||||
	p2 := part2(input)
 | 
			
		||||
	fmt.Printf("part2: %d\n", p2)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,20 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func part1(input string) int {
 | 
			
		||||
	parts := strings.Split(input, ",")
 | 
			
		||||
	sum := 0
 | 
			
		||||
	for _, p := range parts {
 | 
			
		||||
		lsum := 0
 | 
			
		||||
		for _, c := range p {
 | 
			
		||||
			lsum += int(c)
 | 
			
		||||
			lsum *= 17
 | 
			
		||||
			lsum %= 256
 | 
			
		||||
		}
 | 
			
		||||
		sum += lsum
 | 
			
		||||
	}
 | 
			
		||||
	return sum
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,63 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"slices"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type lens struct {
 | 
			
		||||
	focal_length int
 | 
			
		||||
	label        string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func part2(input string) int {
 | 
			
		||||
	instructions := strings.Split(input, ",")
 | 
			
		||||
	boxes := make([][]lens, 256)
 | 
			
		||||
	for _, instruction := range instructions {
 | 
			
		||||
		if strings.Contains(instruction, "=") {
 | 
			
		||||
			split := strings.Split(instruction, "=")
 | 
			
		||||
			label := split[0]
 | 
			
		||||
			box_index := hash(label)
 | 
			
		||||
			focal_length, _ := strconv.Atoi(split[1])
 | 
			
		||||
			found_box := boxes[box_index]
 | 
			
		||||
			lens_index := slices.IndexFunc(found_box, func(l lens) bool {
 | 
			
		||||
				return l.label == label
 | 
			
		||||
			})
 | 
			
		||||
			if lens_index >= 0 {
 | 
			
		||||
				found_box[lens_index] = lens{focal_length, label}
 | 
			
		||||
				boxes[box_index] = found_box
 | 
			
		||||
			} else {
 | 
			
		||||
				boxes[box_index] = append(found_box, lens{focal_length, label})
 | 
			
		||||
			}
 | 
			
		||||
		} else if strings.Contains(instruction, "-") {
 | 
			
		||||
			split := strings.Split(instruction, "-")
 | 
			
		||||
			label := split[0]
 | 
			
		||||
			box_index := hash(label)
 | 
			
		||||
			found_box := boxes[box_index]
 | 
			
		||||
			boxes[box_index] = slices.DeleteFunc(found_box, func(l lens) bool {
 | 
			
		||||
				return l.label == label
 | 
			
		||||
			})
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	sum := 0
 | 
			
		||||
 | 
			
		||||
	for box_number, box := range boxes {
 | 
			
		||||
		for slot_number, lens := range box {
 | 
			
		||||
			sum += ((box_number + 1) * (slot_number + 1) * lens.focal_length)
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return sum
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func hash(input string) int {
 | 
			
		||||
	sum := 0
 | 
			
		||||
	for _, c := range input {
 | 
			
		||||
		sum += int(c)
 | 
			
		||||
		sum *= 17
 | 
			
		||||
		sum %= 256
 | 
			
		||||
	}
 | 
			
		||||
	return sum
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue