Files
advent-of-code-2021/day-1/main.go
T

118 lines
2.2 KiB
Go
Raw Normal View History

2021-12-01 13:42:07 -05:00
/* Day 1: Sonar Sweep */
package main
import (
2021-12-01 14:32:33 -05:00
"AoC2021/utils/timer"
2021-12-01 13:42:07 -05:00
"bufio"
"fmt"
"os"
"strconv"
)
2021-12-01 15:31:51 -05:00
var DEBUG = true
var WIDTH = 100
2021-12-01 13:42:07 -05:00
func check(e error) {
if e != nil {
fmt.Println("Oh snap!")
panic(e)
}
}
func main() {
fmt.Println("--- Day 1: Sonar Sweep ---")
2021-12-01 14:32:33 -05:00
timer.Start()
2021-12-01 13:42:07 -05:00
part1()
2021-12-01 14:32:33 -05:00
timer.Tick()
2021-12-01 13:42:07 -05:00
fmt.Println()
part2()
2021-12-01 14:32:33 -05:00
timer.Tick()
2021-12-01 13:42:07 -05:00
fmt.Println()
}
func part1() {
fmt.Println("* Part 1 *")
fmt.Println(" Goal: Count the number of times a depth measurement increases from the previous measurement.")
fmt.Println(" Answer: How many measurements are larger than the previous measurement?")
input, err := os.Open("./input.txt")
2021-12-01 15:31:51 -05:00
check(err)
2021-12-08 16:31:05 -05:00
defer input.Close()
2021-12-01 13:42:07 -05:00
scanner := bufio.NewScanner(input)
lastValue := -1
measurements := 0
increases := 0
for scanner.Scan() {
value, err := strconv.Atoi(scanner.Text())
check(err)
measurements++
if lastValue != -1 && lastValue < value {
increases++
}
2021-12-01 15:31:51 -05:00
fmt.Print(GetPip(lastValue, lastValue, value, measurements))
2021-12-01 13:42:07 -05:00
lastValue = value
}
2021-12-01 14:32:33 -05:00
fmt.Printf("Read %d measurements resulting in %d increases\n", measurements, increases)
2021-12-01 13:42:07 -05:00
}
func part2() {
fmt.Println("* Part 2 *")
fmt.Println(" Goal: Count the number of times the sum of measurements in this sliding window increases")
fmt.Println(" Answer: How many sums are larger than the previous sum?")
input, err := os.Open("./input.txt")
2021-12-01 15:31:51 -05:00
check(err)
2021-12-08 16:31:05 -05:00
defer input.Close()
2021-12-01 13:42:07 -05:00
scanner := bufio.NewScanner(input)
w1, w2, w3 := -1, -1, -1
measurements := 0
increases := 0
for scanner.Scan() {
value, err := strconv.Atoi(scanner.Text())
check(err)
measurements++
previous := w1 + w2 + w3
current := w2 + w3 + value
if w1 != -1 && current > previous {
increases++
}
2021-12-01 15:31:51 -05:00
fmt.Print(GetPip(w1, previous, current, measurements))
2021-12-01 13:42:07 -05:00
// shift over the window
w1, w2, w3 = w2, w3, value
}
2021-12-01 14:32:33 -05:00
fmt.Printf("Read %d measurements resulting in %d increases\n", measurements, increases)
2021-12-01 13:42:07 -05:00
}
2021-12-01 15:31:51 -05:00
func GetPip(first int, previous int, current int, total int) string {
retVal := ""
2021-12-01 13:42:07 -05:00
if !DEBUG {
2021-12-01 15:31:51 -05:00
return retVal
2021-12-01 13:42:07 -05:00
}
2021-12-01 15:31:51 -05:00
switch {
case first != -1 && current > previous:
retVal = "+"
case first != -1 && current < previous:
retVal = "-"
default:
retVal = "."
2021-12-01 13:42:07 -05:00
}
if total%WIDTH == 0 {
2021-12-01 15:31:51 -05:00
retVal += "\n"
2021-12-01 13:42:07 -05:00
}
2021-12-01 15:31:51 -05:00
return retVal
2021-12-01 13:42:07 -05:00
}