aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

1.go (560B) [raw]


      1 package main
      2 
      3 import (
      4 	"bufio"
      5 	"fmt"
      6 	"log"
      7 	"os"
      8 	"unicode"
      9 )
     10 
     11 func main() {
     12 	f, err := os.Open("input.txt")
     13 	if err != nil {
     14 		log.Fatal(err)
     15 	}
     16 	defer f.Close()
     17 
     18 	sum := 0
     19 	scanner := bufio.NewScanner(f)
     20 	for scanner.Scan() {
     21 		line := scanner.Text()
     22 		first := -1
     23 		last := -1
     24 		for _, c := range line {
     25 			if unicode.IsNumber(c) {
     26 				n := int(c - '0')
     27 				if first == -1 {
     28 					first = n
     29 					last = n
     30 				} else {
     31 					last = n
     32 				}
     33 			}
     34 		}
     35 		sum += 10*first + last
     36 	}
     37 	fmt.Println(sum)
     38 
     39 	if err := scanner.Err(); err != nil {
     40 		log.Fatal(err)
     41 	}
     42 }