main.go (920B)
1 package main 2 3 import ( 4 "bufio" 5 "fmt" 6 "os" 7 "strconv" 8 ) 9 10 func readInput(filename string) (res []int) { 11 file, _ := os.Open(filename) 12 defer file.Close() 13 14 scanner := bufio.NewScanner(file) 15 for scanner.Scan() { 16 i, _ := strconv.Atoi(scanner.Text()) 17 res = append(res, i) 18 } 19 return 20 } 21 22 func main() { 23 input := readInput("./input.txt") 24 // 514579 25 fmt.Println(part1(input)) 26 // 116115990 27 fmt.Println(part2(input)) 28 } 29 30 func part1(input []int) int { 31 resids := make(map[int]int) 32 for _, v := range input { 33 if resid, ok := resids[2020-v]; ok { 34 return resid * v 35 } else { 36 resids[v] = v 37 } 38 } 39 return 0 40 } 41 42 func part2(input []int) int { 43 resids := make(map[int]int) 44 for i := 0; i < len(input); i++ { 45 for j := i + 1; j < len(input); j++ { 46 if resid_prod, ok := resids[2020-input[j]]; ok { 47 return resid_prod * input[j] 48 } else { 49 resids[input[i]+input[j]] = input[i] * input[j] 50 } 51 } 52 } 53 return 0 54 }