day 1 part 2
This commit is contained in:
		
							parent
							
								
									44e82799b8
								
							
						
					
					
						commit
						785a00400f
					
				
					 5 changed files with 53 additions and 10 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -1 +1,2 @@ | ||||||
| .vscode | .vscode | ||||||
|  | session | ||||||
|  | @ -15,7 +15,7 @@ func NewClient(session string) client { | ||||||
| 	return client{session} | 	return client{session} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func (c *client) Day(day int) []byte { | func (c *client) Day(day int) string { | ||||||
| 	client := http.Client{} | 	client := http.Client{} | ||||||
| 	req, err := http.NewRequest("GET", fmt.Sprintf("https://adventofcode.com/2021/day/%v/input", day), nil) | 	req, err := http.NewRequest("GET", fmt.Sprintf("https://adventofcode.com/2021/day/%v/input", day), nil) | ||||||
| 
 | 
 | ||||||
|  | @ -37,5 +37,5 @@ func (c *client) Day(day int) []byte { | ||||||
| 		log.Fatalln(err) | 		log.Fatalln(err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	return body | 	return string(body) | ||||||
| } | } | ||||||
|  |  | ||||||
							
								
								
									
										21
									
								
								d1/d1.go
									
										
									
									
									
								
							
							
						
						
									
										21
									
								
								d1/d1.go
									
										
									
									
									
								
							|  | @ -20,3 +20,24 @@ func D1(scans string) int64 { | ||||||
| 
 | 
 | ||||||
| 	return int64(downs) | 	return int64(downs) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func D1P2(scans string) int64 { | ||||||
|  | 	depths := strings.Split(strings.TrimSpace(scans), "\n") | ||||||
|  | 	downs := 0 | ||||||
|  | 
 | ||||||
|  | 	for i := 3; i < len(depths); i++ { | ||||||
|  | 		fourth, _ := strconv.Atoi(strings.TrimSpace(depths[i])) | ||||||
|  | 		third, _ := strconv.Atoi(strings.TrimSpace(depths[i-1])) | ||||||
|  | 		second, _ := strconv.Atoi(strings.TrimSpace(depths[i-2])) | ||||||
|  | 		first, _ := strconv.Atoi(strings.TrimSpace(depths[i-3])) | ||||||
|  | 
 | ||||||
|  | 		prev := first + second + third | ||||||
|  | 		next := second + third + fourth | ||||||
|  | 
 | ||||||
|  | 		if next > prev { | ||||||
|  | 			downs++ | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	return int64(downs) | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -23,3 +23,20 @@ func TestD1(t *testing.T) { | ||||||
| 
 | 
 | ||||||
| 	assert.EqualValues(t, 7, actual) | 	assert.EqualValues(t, 7, actual) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func TestD1P2(t *testing.T) { | ||||||
|  | 	input := ` | ||||||
|  | 		199 | ||||||
|  | 		200 | ||||||
|  | 		208 | ||||||
|  | 		210 | ||||||
|  | 		200 | ||||||
|  | 		207 | ||||||
|  | 		240 | ||||||
|  | 		269 | ||||||
|  | 		260 | ||||||
|  | 		263 | ||||||
|  | 	` | ||||||
|  | 
 | ||||||
|  | 	assert.EqualValues(t, 5, D1P2(input)) | ||||||
|  | } | ||||||
|  |  | ||||||
							
								
								
									
										18
									
								
								main.go
									
										
									
									
									
								
							
							
						
						
									
										18
									
								
								main.go
									
										
									
									
									
								
							|  | @ -1,8 +1,8 @@ | ||||||
| package main | package main | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"bufio" |  | ||||||
| 	"fmt" | 	"fmt" | ||||||
|  | 	"log" | ||||||
| 	"os" | 	"os" | ||||||
| 	"strings" | 	"strings" | ||||||
| 
 | 
 | ||||||
|  | @ -13,11 +13,15 @@ import ( | ||||||
| 
 | 
 | ||||||
| func main() { | func main() { | ||||||
| 
 | 
 | ||||||
| 	reader := bufio.NewReader(os.Stdin) | 	session, err := os.ReadFile("session") | ||||||
| 	fmt.Print("Enter your session: ") |  | ||||||
| 	session, _ := reader.ReadString('\n') |  | ||||||
| 	client := client.NewClient(strings.TrimSpace(session)) |  | ||||||
| 
 | 
 | ||||||
| 	fmt.Printf("day1: %v\n", d1.D1(string(client.Day(1)))) | 	if err != nil { | ||||||
| 	fmt.Printf("day2: %v\n", d2.D2(string(client.Day(2)))) | 		log.Fatal(err) | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	client := client.NewClient(strings.TrimSpace(string(session))) | ||||||
|  | 
 | ||||||
|  | 	fmt.Printf("day 1: %v\n", d1.D1(client.Day(1))) | ||||||
|  | 	fmt.Printf("day 1 part 2: %v\n", d1.D1P2(client.Day(1))) | ||||||
|  | 	fmt.Printf("day 2: %v\n", d2.D2(client.Day(2))) | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		
		Reference in a new issue