Day 1 Complete

This commit is contained in:
Luke Else 2021-12-01 15:41:32 +00:00
parent 21a2be1d52
commit fb67c6195c
5 changed files with 2096 additions and 0 deletions

44
day1/1a/day1a.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
fmt.Println(countGreater(returnContent("../input")))
}
func countGreater(input *[]int) (count int) {
//Find how many elements have increased from previous
for i := 1; i < len(*input); i++ {
if (*input)[i] > (*input)[i-1] {
count++
}
}
return
}
func returnContent(path string) *[]int {
//read file and return it as an array of integers
file, err := os.Open(path)
var content []int
if err != nil {
fmt.Println("Unlucky, the file didn't open")
return &content
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
value, _ := strconv.Atoi(scanner.Text())
content = append(content, value)
}
return &content
}

3
day1/1a/go.mod Normal file
View File

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

46
day1/1b/day1b.go Normal file
View File

@ -0,0 +1,46 @@
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func main() {
fmt.Println(countGreater(returnContent("../input")))
}
func countGreater(input *[]int) (count int) {
//Find how many elements have increased from previous
for i := 3; i < len(*input); i++ {
sum1 := (*input)[i-1] + (*input)[i-2] + (*input)[i-3]
sum2 := (*input)[i-1] + (*input)[i-2] + (*input)[i]
if sum2 > sum1 {
count++
}
}
return
}
func returnContent(path string) *[]int {
//read file and return it as an array of integers
file, err := os.Open(path)
var content []int
if err != nil {
fmt.Println("Unlucky, the file didn't open")
return &content
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
value, _ := strconv.Atoi(scanner.Text())
content = append(content, value)
}
return &content
}

3
day1/1b/go.mod Normal file
View File

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

2000
day1/input Normal file

File diff suppressed because it is too large Load Diff