2020/day01/main.go

71 lines
1.2 KiB
Go
Raw Normal View History

2020-12-01 18:55:03 +00:00
package main
import (
"io/ioutil"
"log"
"strconv"
"strings"
)
func main() {
content, err := ioutil.ReadFile("input.txt")
if err != nil {
log.Print(err)
return
}
lines := strings.Split(string(content), "\r\n")
numbers := make([]int, len(lines))
for i := 0; i < len(numbers); i++ {
number, err := strconv.Atoi(lines[i])
if err != nil {
log.Print(err)
return
}
numbers[i] = number
}
var num1 int
var num2 int
twoNumLoop:
for i := 0; i < len(numbers); i++ {
for j := i + 1; j < len(numbers); j++ {
if numbers[i]+numbers[j] == 2020 {
num1 = numbers[i]
num2 = numbers[j]
break twoNumLoop
}
}
}
log.Printf("First Number: %d", num1)
log.Printf("Second Number: %d", num2)
log.Printf("%d * %d = %d", num1, num2, num1*num2)
var num3 int
threeNumLoop:
for i := 0; i < len(numbers); i++ {
for j := i + 1; j < len(numbers); j++ {
for k := j + 1; k < len(numbers); k++ {
if numbers[i]+numbers[j]+numbers[k] == 2020 {
num1 = numbers[i]
num2 = numbers[j]
num3 = numbers[k]
break threeNumLoop
}
}
}
}
log.Printf("First Number: %d", num1)
log.Printf("Second Number: %d", num2)
log.Printf("Third Number: %d", num3)
log.Printf("%d * %d * %d = %d", num1, num2, num3, num1*num2*num3)
}