~ols/advent-of-code-2021

b85cc5edf156a03ec350674e084076661d8cada9 — Oliver Leaver-Smith 7 months ago 822a960 master
add tests
7 files changed, 92 insertions(+), 46 deletions(-)

R day-1.go => day-1/day-1.go
A day-1/day-1_test.go
R inputs/day-1 => day-1/input
R day-2.go => day-2/day-2.go
A day-2/day-2_test.go
R inputs/day-2 => day-2/input
A go.mod
R day-1.go => day-1/day-1.go +14 -25
@@ 8,15 8,10 @@ import (
	"strconv"
)

func partOne() int {
	// count the number of times a depth measurement increases from
	// the previous measurement. (There is no measurement before the
	// first measurement.)

func getReadings() []int {
	var readings []int
	var count int

	file, err := os.Open("inputs/day-1")
	file, err := os.Open("input")
	if err != nil {
		log.Fatal(err)
	}


@@ 30,6 25,15 @@ func partOne() int {
		}
		readings = append(readings, i)
	}
	return readings
}

func partOne(readings []int) int {
	// count the number of times a depth measurement increases from
	// the previous measurement. (There is no measurement before the
	// first measurement.)

	var count int

	for i := range readings {
		if i > 0 {


@@ 42,29 46,13 @@ func partOne() int {
	return count
}

func partTwo() int {
func partTwo(readings []int) int {
	// count the number of times the sum of measurements in this
	// sliding window increases from the previous sum

	var readings []int
	var count int
	var totals []int

	file, err := os.Open("inputs/day-1")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		i, err := strconv.Atoi(scanner.Text())
		if err != nil {
			log.Fatal(err)
		}
		readings = append(readings, i)
	}

	for i := range readings {
		if i < len(readings) - 2 {
			reading := readings[i] + readings[i+1] + readings[i+2]


@@ 84,5 72,6 @@ func partTwo() int {
}

func main() {
	fmt.Printf("Part one: %d\nPart two: %d\n", partOne(), partTwo())
	readings := getReadings()
	fmt.Printf("Part one: %d\nPart two: %d\n", partOne(readings), partTwo(readings))
}

A day-1/day-1_test.go => day-1/day-1_test.go +25 -0
@@ 0,0 1,25 @@
package main

import (
	"testing"
)

func TestPartOne(t *testing.T) {
	readings := []int{199, 200, 208, 210, 200, 207, 240, 269, 260, 263}
	got := partOne(readings)
	want := 7

	if got != want {
		t.Errorf("got %q, wanted %q", got, want)
	}
}

func TestPartTwo(t *testing.T) {
	readings := []int{607, 618, 618, 617, 647, 716, 769, 792}
	got := partTwo(readings)
	want := 5

	if got != want {
		t.Errorf("got %q, wanted %q", got, want)
	}
}

R inputs/day-1 => day-1/input +0 -0
R day-2.go => day-2/day-2.go +25 -21
@@ 9,19 9,29 @@ import (
	"strings"
)

func partOne() int {
func getReadings() []string {
        var readings []string

        file, err := os.Open("input")
        if err != nil {
                log.Fatal(err)
        }
        defer file.Close()

        scanner := bufio.NewScanner(file)
        for scanner.Scan() {
		readings = append(readings, scanner.Text())
        }

        return readings
}

func partOne(readings []string) int {
	var depth int
	var horizontal int

	file, err := os.Open("inputs/day-2")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := strings.Fields(scanner.Text())
	for _, v := range readings {
		line := strings.Fields(v)
		direction := line[0]
		distance, err := strconv.Atoi(line[1])
		if err != nil {


@@ 40,20 50,13 @@ func partOne() int {
	return depth * horizontal
}

func partTwo() int {
func partTwo(readings []string) int {
	var depth int
	var horizontal int
	var aim int

	file, err := os.Open("inputs/day-2")
	if err != nil {
		log.Fatal(err)
	}
	defer file.Close()

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := strings.Fields(scanner.Text())
	for _, v := range readings {
		line := strings.Fields(v)
		direction := line[0]
		distance, err := strconv.Atoi(line[1])
		if err != nil {


@@ 74,5 77,6 @@ func partTwo() int {
}

func main() {
	fmt.Printf("Part one: %d\nPart two: %d\n", partOne(), partTwo())
	readings := getReadings()
	fmt.Printf("Part one: %d\nPart two: %d\n", partOne(readings), partTwo(readings))
}

A day-2/day-2_test.go => day-2/day-2_test.go +25 -0
@@ 0,0 1,25 @@
package main

import (
	"testing"
)

func TestPartOne(t *testing.T) {
	readings := []string{"forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"}
	got := partOne(readings)
	want := 150

	if got != want {
		t.Errorf("got %q, wanted %q", got, want)
	}
}

func TestPartTwo(t *testing.T) {
	readings := []string{"forward 5", "down 5", "forward 8", "up 3", "down 8", "forward 2"}
	got := partTwo(readings)
	want := 900

	if got != want {
		t.Errorf("got %q, wanted %q", got, want)
	}
}

R inputs/day-2 => day-2/input +0 -0
A go.mod => go.mod +3 -0
@@ 0,0 1,3 @@
module git.sr.ht/~ols/advent-of-code-2021

go 1.17