github.com/vpayno/adventofcode-2022-golang-workspace@v0.0.0-20230605190011-dbafed5593de/internal/day04/app.go (about) 1 // Package day04 is the module with the cli logic for the application. 2 package day04 3 4 import ( 5 "bufio" 6 7 "github.com/vpayno/adventofcode-2022-golang-workspace/internal/aocshared" 8 ) 9 10 // Run is called my the gain function. It's basically the main function of the app. 11 func Run(conf Config) error { 12 file, err := aocshared.GetFile(conf.inputFileName) 13 if err != nil { 14 return err 15 } 16 17 scanner := aocshared.GetScanner(file) 18 19 data, err := loadData(scanner) 20 if err != nil { 21 return err 22 } 23 24 fullyContainedSum := getFullyContainedCount(data) 25 26 aocshared.ShowResult(fullyContainedSum) 27 28 partiallyContainedSum := getPartiallyContainedCount(data) 29 30 aocshared.ShowResult(partiallyContainedSum) 31 32 return nil 33 } 34 35 func loadData(file *bufio.Scanner) (pairs, error) { 36 data := pairs{} 37 38 for file.Scan() { 39 line := file.Text() 40 41 if line == "" { 42 continue 43 } 44 45 p := pair{} 46 err := p.addPair(line) 47 if err != nil { 48 return data, err 49 } 50 51 data = append(data, p) 52 } 53 54 return data, nil 55 } 56 57 func getFullyContainedCount(data pairs) int { 58 var count int 59 60 for _, p := range data { 61 if p.isFullyContained(1) || p.isFullyContained(2) { 62 count++ 63 } 64 } 65 66 return count 67 } 68 69 func getPartiallyContainedCount(data pairs) int { 70 var count int 71 72 for _, p := range data { 73 if p.isPartiallyContained(1) || p.isPartiallyContained(2) { 74 count++ 75 } 76 } 77 78 return count 79 }