day2
This commit is contained in:
		
							parent
							
								
									8d6a6df572
								
							
						
					
					
						commit
						44e82799b8
					
				
					 3 changed files with 64 additions and 0 deletions
				
			
		
							
								
								
									
										42
									
								
								d2/d2.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								d2/d2.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,42 @@ | ||||||
|  | package d2 | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"log" | ||||||
|  | 	"strconv" | ||||||
|  | 	"strings" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type position struct { | ||||||
|  | 	x int64 | ||||||
|  | 	y int64 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func D2(input string) int64 { | ||||||
|  | 	position := position{} | ||||||
|  | 	directions := strings.Split(strings.TrimSpace(input), "\n") | ||||||
|  | 
 | ||||||
|  | 	for _, direction := range directions { | ||||||
|  | 		directionAndValue := strings.Split(strings.TrimSpace(direction), " ") | ||||||
|  | 
 | ||||||
|  | 		if len(directionAndValue) != 2 { | ||||||
|  | 			log.Fatal("line has wrong number of elements: ", direction) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		value, err := strconv.Atoi(directionAndValue[1]) | ||||||
|  | 
 | ||||||
|  | 		if err != nil { | ||||||
|  | 			log.Fatal(err) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		switch directionAndValue[0] { | ||||||
|  | 		case "forward": | ||||||
|  | 			position.x += int64(value) | ||||||
|  | 		case "down": | ||||||
|  | 			position.y += int64(value) | ||||||
|  | 		case "up": | ||||||
|  | 			position.y -= int64(value) | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return position.x * position.y | ||||||
|  | } | ||||||
							
								
								
									
										20
									
								
								d2/d2_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								d2/d2_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,20 @@ | ||||||
|  | package d2 | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"testing" | ||||||
|  | 
 | ||||||
|  | 	"github.com/stretchr/testify/assert" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | func TestD2(t *testing.T) { | ||||||
|  | 
 | ||||||
|  | 	input := ` | ||||||
|  | forward 5 | ||||||
|  | down 5 | ||||||
|  | forward 8 | ||||||
|  | up 3 | ||||||
|  | down 8 | ||||||
|  | forward 2` | ||||||
|  | 
 | ||||||
|  | 	assert.EqualValues(t, 150, D2(input)) | ||||||
|  | } | ||||||
							
								
								
									
										2
									
								
								main.go
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								main.go
									
										
									
									
									
								
							|  | @ -8,6 +8,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| 	"github.com/fotonmoton/aoc2021/client" | 	"github.com/fotonmoton/aoc2021/client" | ||||||
| 	"github.com/fotonmoton/aoc2021/d1" | 	"github.com/fotonmoton/aoc2021/d1" | ||||||
|  | 	"github.com/fotonmoton/aoc2021/d2" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
|  | @ -18,4 +19,5 @@ func main() { | ||||||
| 	client := client.NewClient(strings.TrimSpace(session)) | 	client := client.NewClient(strings.TrimSpace(session)) | ||||||
| 
 | 
 | ||||||
| 	fmt.Printf("day1: %v\n", d1.D1(string(client.Day(1)))) | 	fmt.Printf("day1: %v\n", d1.D1(string(client.Day(1)))) | ||||||
|  | 	fmt.Printf("day2: %v\n", d2.D2(string(client.Day(2)))) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue