Created
December 5, 2021 06:30
-
-
Save Nv7-GitHub/a268a2b47e785b5549caf47fff7ef259 to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
_ "embed" | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
//go:embed input.txt | |
var input string | |
func main() { | |
last := -1 | |
bigger := 0 | |
lines := strings.Split(input, "\n") | |
for _, line := range lines { | |
num, err := strconv.Atoi(line) | |
if err != nil { | |
panic(err) | |
} | |
if last != -1 && num > last { | |
bigger++ | |
} | |
last = num | |
} | |
fmt.Println(bigger) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
_ "embed" | |
"fmt" | |
"strconv" | |
"strings" | |
) | |
//go:embed input.txt | |
var input string | |
func main() { | |
lines := strings.Split(input, "\n") | |
vals := make([]int, len(lines)) | |
// parse | |
for i, line := range lines { | |
num, err := strconv.Atoi(line) | |
if err != nil { | |
panic(err) | |
} | |
vals[i] = num | |
} | |
// calculate windows | |
sums := make([]int, 0) | |
for len(vals) >= 3 { | |
sums = append(sums, vals[0]+vals[1]+vals[2]) | |
vals = vals[1:] | |
} | |
// calculate diffs | |
out := 0 | |
for i, val := range sums[1:] { | |
if val > sums[i] { | |
out++ | |
} | |
} | |
fmt.Println(out) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment