day one in go
This commit is contained in:
		
							
								
								
									
										118
									
								
								01/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										118
									
								
								01/main.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,118 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"bufio"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func lookahead(line string, i, peek int) string {
 | 
				
			||||||
 | 
						if len(line) > i+peek-1 {
 | 
				
			||||||
 | 
							return line[i : i+peek]
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return ""
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						start := time.Now()
 | 
				
			||||||
 | 
						partOne()
 | 
				
			||||||
 | 
						duration := time.Since(start)
 | 
				
			||||||
 | 
						partTwo()
 | 
				
			||||||
 | 
						duration2 := time.Since(start)
 | 
				
			||||||
 | 
						fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func makeScanner(test bool) *bufio.Scanner {
 | 
				
			||||||
 | 
						var f *os.File
 | 
				
			||||||
 | 
						if test {
 | 
				
			||||||
 | 
							f, _ = os.Open("inputs/testinput")
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							f, _ = os.Open("inputs/input")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						reader := bufio.NewReader(f)
 | 
				
			||||||
 | 
						return bufio.NewScanner(reader)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func partOne() {
 | 
				
			||||||
 | 
						scanner := makeScanner(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sum := 0
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							line := scanner.Text()
 | 
				
			||||||
 | 
							var first, last rune
 | 
				
			||||||
 | 
							for _, c := range line {
 | 
				
			||||||
 | 
								switch c {
 | 
				
			||||||
 | 
								case '1', '2', '3', '4', '5', '6', '7', '8', '9':
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = c
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = c
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							sum += 10*(int(first)-48) + int(last) - 48 // direct ASCII translation
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(sum)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func partTwo() {
 | 
				
			||||||
 | 
						scanner := makeScanner(false)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						sum := 0
 | 
				
			||||||
 | 
						for scanner.Scan() {
 | 
				
			||||||
 | 
							line := scanner.Text()
 | 
				
			||||||
 | 
							var first, last rune
 | 
				
			||||||
 | 
							for i, c := range line {
 | 
				
			||||||
 | 
								switch {
 | 
				
			||||||
 | 
								case c == '1' || lookahead(line, i, 3) == "one":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '1'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '1'
 | 
				
			||||||
 | 
								case c == '2' || lookahead(line, i, 3) == "two":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '2'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '2'
 | 
				
			||||||
 | 
								case c == '3' || lookahead(line, i, 5) == "three":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '3'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '3'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case c == '4' || lookahead(line, i, 4) == "four":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '4'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '4'
 | 
				
			||||||
 | 
								case c == '5' || lookahead(line, i, 4) == "five":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '5'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '5'
 | 
				
			||||||
 | 
								case c == '6' || lookahead(line, i, 3) == "six":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '6'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '6'
 | 
				
			||||||
 | 
								case c == '7' || lookahead(line, i, 5) == "seven":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '7'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '7'
 | 
				
			||||||
 | 
								case c == '8' || lookahead(line, i, 5) == "eight":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '8'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '8'
 | 
				
			||||||
 | 
								case c == '9' || lookahead(line, i, 4) == "nine":
 | 
				
			||||||
 | 
									if first == 0 {
 | 
				
			||||||
 | 
										first = '9'
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									last = '9'
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							sum += 10*(int(first)-48) + int(last) - 48
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						fmt.Println(sum)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1,5 +1,5 @@
 | 
				
			|||||||
# AOC 2023
 | 
					# AOC 2023
 | 
				
			||||||
 | 
					
 | 
				
			||||||
In Chez Scheme? Perhaps. Go as a fallback.
 | 
					In Chicken Scheme? Perhaps. Go as a fallback.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
<https://adventofcode.com/2023/>
 | 
					<https://adventofcode.com/2023/>
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user