Day 8a complete

This commit is contained in:
2021-12-08 19:09:16 +00:00
parent e0c963abeb
commit ec060003c4
4 changed files with 264 additions and 0 deletions

3
day8/day 8a/go.mod Normal file
View File

@ -0,0 +1,3 @@
module PWD
go 1.17

51
day8/day 8a/main.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
content := returnContent("../input")
//content := returnContent("testInput")
matches := 0
for _, line := range *content {
split := strings.Split(line, " | ")
rhs := split[1]
digits := strings.Fields(rhs)
for _, digit := range digits {
segments := len(digit)
if segments == 2 || segments == 4 || segments == 3 || segments == 7 {
matches++
}
}
}
fmt.Println(matches)
}
func returnContent(path string) *[]string {
//read file and return it as an array of integers
file, err := os.Open(path)
var content []string
if err != nil {
fmt.Println("Unlucky, the file didn't open")
return &content
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
content = append(content, scanner.Text())
}
return &content
}

10
day8/day 8a/testInput Normal file
View File

@ -0,0 +1,10 @@
be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb cefbgd gcbe
edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb dgebacf gc
fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb cbg
fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba gadfec cb
aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf bgf bfgea
fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba ca fadegcb
dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef fcge gbcadfe
bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe cdgba cbgef
egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg cgb
gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg bagce