Day 13.
This commit is contained in:
		
							parent
							
								
									2107b9c990
								
							
						
					
					
						commit
						a01ab60060
					
				
							
								
								
									
										3
									
								
								data.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								data.go
									
									
									
									
									
								
							| @ -39,3 +39,6 @@ var Day11Data string | ||||
| 
 | ||||
| //go:embed day_12/data.txt | ||||
| var Day12Data string | ||||
| 
 | ||||
| //go:embed day_13/data.txt | ||||
| var Day13Data string | ||||
|  | ||||
							
								
								
									
										1279
									
								
								day_13/data.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1279
									
								
								day_13/data.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										15
									
								
								day_13/example_data.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								day_13/example_data.txt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| Button A: X+94, Y+34 | ||||
| Button B: X+22, Y+67 | ||||
| Prize: X=8400, Y=5400 | ||||
| 
 | ||||
| Button A: X+26, Y+66 | ||||
| Button B: X+67, Y+21 | ||||
| Prize: X=12748, Y=12176 | ||||
| 
 | ||||
| Button A: X+17, Y+86 | ||||
| Button B: X+84, Y+37 | ||||
| Prize: X=7870, Y=6450 | ||||
| 
 | ||||
| Button A: X+69, Y+23 | ||||
| Button B: X+27, Y+71 | ||||
| Prize: X=18641, Y=10279 | ||||
							
								
								
									
										80
									
								
								day_13/solution.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										80
									
								
								day_13/solution.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | ||||
| package day_13 | ||||
| 
 | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"strings" | ||||
| ) | ||||
| 
 | ||||
| type Arcade struct { | ||||
| 	AX, AY         int | ||||
| 	BX, BY         int | ||||
| 	PrizeX, PrizeY int | ||||
| } | ||||
| 
 | ||||
| // task:https://adventofcode.com/2024/day/13 | ||||
| func SolveBasic(input string) int { | ||||
| 	arcades := loadArcades(input) | ||||
| 
 | ||||
| 	sum := 0 | ||||
| 	for _, arcade := range arcades { | ||||
| 		if price, solvable := solve(arcade); solvable { | ||||
| 			sum += price | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return sum | ||||
| } | ||||
| 
 | ||||
| // task:https://adventofcode.com/2024/day/12#part2 | ||||
| func SolveComplex(input string) int { | ||||
| 	arcades := fixPrizes(loadArcades(input)) | ||||
| 
 | ||||
| 	sum := 0 | ||||
| 	for _, arcade := range arcades { | ||||
| 		if price, solvable := solve(arcade); solvable { | ||||
| 			sum += price | ||||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	return sum | ||||
| } | ||||
| 
 | ||||
| func fixPrizes(arcades []Arcade) []Arcade { | ||||
| 	for i := range arcades { | ||||
| 		arcades[i].PrizeX += 10000000000000 | ||||
| 		arcades[i].PrizeY += 10000000000000 | ||||
| 	} | ||||
| 	return arcades | ||||
| } | ||||
| 
 | ||||
| func solve(arcade Arcade) (int, bool) { | ||||
| 	a1 := arcade.AX | ||||
| 	b1 := arcade.BX | ||||
| 	a2 := arcade.AY | ||||
| 	b2 := arcade.BY | ||||
| 	c1 := arcade.PrizeX | ||||
| 	c2 := arcade.PrizeY | ||||
| 
 | ||||
| 	A := (c1*b2 - b1*c2) / (a1*b2 - b1*a2) | ||||
| 	B := (c2 - a2*A) / b2 | ||||
| 
 | ||||
| 	if A*arcade.AX+B*arcade.BX == arcade.PrizeX && | ||||
| 		A*arcade.AY+B*arcade.BY == arcade.PrizeY { | ||||
| 		return A*3 + B, true | ||||
| 	} | ||||
| 	return 0, false | ||||
| } | ||||
| 
 | ||||
| func loadArcades(input string) []Arcade { | ||||
| 	var arcades []Arcade | ||||
| 
 | ||||
| 	for _, arcadeLine := range strings.Split(input, "\n\n") { | ||||
| 		lines := strings.Split(arcadeLine, "\n") | ||||
| 		arcade := Arcade{} | ||||
| 		fmt.Sscanf(lines[0], "Button A: X+%d, Y+%d", &arcade.AX, &arcade.AY) | ||||
| 		fmt.Sscanf(lines[1], "Button B: X+%d, Y+%d", &arcade.BX, &arcade.BY) | ||||
| 		fmt.Sscanf(lines[2], "Prize: X=%d, Y=%d", &arcade.PrizeX, &arcade.PrizeY) | ||||
| 		arcades = append(arcades, arcade) | ||||
| 	} | ||||
| 	return arcades | ||||
| } | ||||
							
								
								
									
										15
									
								
								day_13/solution_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										15
									
								
								day_13/solution_test.go
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,15 @@ | ||||
| package day_13_test | ||||
| 
 | ||||
| import ( | ||||
| 	"advent_of_code_2024/day_13" | ||||
| 	"advent_of_code_2024/helpers" | ||||
| 	_ "embed" | ||||
| 	"testing" | ||||
| ) | ||||
| 
 | ||||
| //go:embed example_data.txt | ||||
| var ExampleData string | ||||
| 
 | ||||
| func TestBasicSolutionExample(t *testing.T) { | ||||
| 	helpers.Check(t, day_13.SolveBasic, helpers.Format(ExampleData), 480) | ||||
| } | ||||
| @ -13,6 +13,7 @@ import ( | ||||
| 	"advent_of_code_2024/day_10" | ||||
| 	"advent_of_code_2024/day_11" | ||||
| 	"advent_of_code_2024/day_12" | ||||
| 	"advent_of_code_2024/day_13" | ||||
| ) | ||||
| 
 | ||||
| var solutions = map[string]Solution{ | ||||
| @ -41,4 +42,6 @@ var solutions = map[string]Solution{ | ||||
| 	"12-basic":        {day_12.SolveBasic, Day12Data}, | ||||
| 	"12-complex":      {day_12.SolveComplex, Day12Data}, | ||||
| 	"12-complex-fast": {day_12.SolveComplexFast, Day12Data}, | ||||
| 	"13-basic":        {day_13.SolveBasic, Day13Data}, | ||||
| 	"13-complex":      {day_13.SolveComplex, Day13Data}, | ||||
| } | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user