advent-of-code

Perserverance, or the lack thereof

git clone git://git.shimmy1996.com/advent-of-code.git

main.go (1190B)

    1 package main
    2 
    3 import (
    4 	"bufio"
    5 	"fmt"
    6 	"os"
    7 )
    8 
    9 func readInput(filename string) (res [][]string) {
   10 	file, _ := os.Open(filename)
   11 	defer file.Close()
   12 
   13 	scanner := bufio.NewScanner(file)
   14 	group_ans := []string{}
   15 	for scanner.Scan() {
   16 		line := scanner.Text()
   17 		if line == "" {
   18 			res = append(res, group_ans)
   19 			group_ans = []string{}
   20 		} else {
   21 			group_ans = append(group_ans, line)
   22 		}
   23 	}
   24 	res = append(res, group_ans)
   25 	return
   26 }
   27 
   28 func main() {
   29 	input := readInput("./input.txt")
   30 	// 6457
   31 	fmt.Println(part1(input))
   32 	// 3260
   33 	fmt.Println(part2(input))
   34 }
   35 
   36 func part1(input [][]string) (yes_count int) {
   37 	for _, group_ans := range input {
   38 		answered_count := make(map[rune]int)
   39 		for _, person_ans := range group_ans {
   40 			for _, ans := range person_ans {
   41 				answered_count[ans] += 1
   42 			}
   43 		}
   44 		yes_count += len(answered_count)
   45 	}
   46 	return
   47 }
   48 
   49 func part2(input [][]string) (yes_count int) {
   50 	for _, group_ans := range input {
   51 		answered_count := make(map[rune]int)
   52 		for _, person_ans := range group_ans {
   53 			for _, ans := range person_ans {
   54 				answered_count[ans] += 1
   55 			}
   56 		}
   57 		for _, count := range answered_count {
   58 			if count == len(group_ans) {
   59 				yes_count += 1
   60 			}
   61 		}
   62 	}
   63 	return
   64 }