adventofcode/2018/day01/day01.go

61 lines
859 B
Go
Raw Permalink Normal View History

2018-12-01 23:18:31 +00:00
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
)
func main() {
inp := StdinToStringSlice()
part1(inp)
part2(inp)
}
func part1(inp []string) {
freq := 0
for _, v := range inp {
freq += Atoi(v)
}
fmt.Println("Part 1:", freq)
}
func part2(inp []string) {
fmt.Println("Part 2:", findRepeat(inp))
}
func findRepeat(inp []string) int {
vals := make(map[int]bool)
freq := 0
for true {
for _, v := range inp {
freq += Atoi(v)
if vals[freq] {
return freq
}
vals[freq] = true
}
}
return 0
}
func StdinToStringSlice() []string {
var input []string
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
input = append(input, scanner.Text())
}
return input
}
func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi")
}
return ret
}