package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
fmt.Println("Advent of Code 2015 Day 1")
data, err := read_string_input("input.txt")
if err != nil {
fmt.Println (err)
}
floor, basement := solve(data)
fmt.Printf("Part One: %d\n", floor)
fmt.Printf("Part Two: %d\n", basement)
}
func read_string_input(path string) ([]string, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
return lines, scanner.Err()
}
func solve(puzzle []string) (int, int) {
s := strings.Split(puzzle[0], "")
var floor int = 0
var basement int = 0
for i, c := range s {
if c == "(" {
floor = floor + 1
} else {
floor = floor - 1
}
if floor == -1 {
if basement == 0 {
basement = i + 1 // since you start out in position 1
}
}
}
return floor, basement
}