~ols/advent-of-code-2021

822a96023bf0da622d0201425f9ba6547f4d1ce5 — Oliver Leaver-Smith 6 months ago 7fa3b74
Day two plus directory layout change
4 files changed, 1080 insertions(+), 2 deletions(-)

R day-1/day-1.go => day-1.go
A day-2.go
R day-1/input => inputs/day-1
A inputs/day-2
R day-1/day-1.go => day-1.go +2 -2
@@ 16,7 16,7 @@ func partOne() int {
	var readings []int
	var count int

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


@@ 50,7 50,7 @@ func partTwo() int {
	var count int
	var totals []int

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

A day-2.go => day-2.go +78 -0
@@ 0,0 1,78 @@
package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func partOne() 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())
		direction := line[0]
		distance, err := strconv.Atoi(line[1])
		if err != nil {
			log.Fatal(err)
		}
		switch direction {
		case "forward":
			horizontal = horizontal + distance
		case "up":
			depth = depth - distance
		case "down":
			depth = depth + distance
		}
	}

	return depth * horizontal
}

func partTwo() 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())
		direction := line[0]
		distance, err := strconv.Atoi(line[1])
		if err != nil {
			log.Fatal(err)
		}
		switch direction {
		case "forward":
			horizontal = horizontal + distance
			depth = depth + (aim * distance)
		case "up":
			aim = aim - distance
		case "down":
			aim = aim + distance
		}
	}

	return depth * horizontal
}

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

R day-1/input => inputs/day-1 +0 -0
A inputs/day-2 => inputs/day-2 +1000 -0
@@ 0,0 1,1000 @@
forward 2
forward 6
forward 8
forward 7
down 5
forward 8
forward 9
down 2
forward 6
down 9
forward 1
forward 8
forward 6
forward 7
down 4
down 5
forward 1
up 5
down 7
down 7
down 1
up 2
forward 3
forward 2
forward 2
forward 5
up 5
forward 4
forward 9
forward 6
down 4
down 9
down 2
up 6
forward 9
up 7
forward 7
forward 5
up 3
forward 4
forward 9
up 5
down 3
up 6
down 5
down 4
up 6
forward 9
forward 6
down 9
up 3
down 7
up 1
forward 8
forward 3
forward 8
up 6
forward 7
forward 5
forward 8
up 2
forward 2
forward 7
forward 7
down 1
forward 7
up 7
down 3
forward 9
down 5
down 2
forward 5
forward 1
forward 4
forward 6
up 2
up 7
forward 2
forward 6
forward 7
down 9
up 8
down 9
down 3
up 8
down 3
down 2
up 6
forward 3
forward 9
down 4
forward 5
down 6
up 8
forward 1
down 6
down 6
forward 5
down 6
forward 8
up 7
down 3
forward 7
forward 3
forward 1
forward 4
forward 4
down 3
up 9
up 5
forward 1
down 2
up 4
forward 7
up 4
down 3
down 5
down 8
forward 4
up 8
forward 7
up 3
up 4
up 9
forward 1
forward 1
down 6
forward 1
down 8
up 4
forward 9
forward 9
down 6
forward 9
forward 8
down 2
up 3
up 3
down 9
forward 7
forward 8
down 4
forward 1
up 3
forward 3
down 3
down 9
down 5
up 7
up 2
forward 7
forward 2
forward 5
forward 4
down 7
forward 7
up 1
up 3
down 6
down 4
forward 9
forward 8
down 5
down 4
down 1
down 5
forward 9
forward 8
down 3
forward 5
forward 3
forward 6
down 6
forward 3
up 9
forward 4
down 7
forward 3
forward 7
forward 1
forward 5
down 1
forward 1
down 6
up 7
down 3
forward 2
down 4
forward 6
up 6
forward 8
forward 8
down 5
up 4
forward 7
forward 6
up 4
forward 6
down 1
forward 6
forward 2
up 4
down 6
down 7
forward 4
down 4
forward 1
down 3
forward 5
forward 5
forward 9
forward 3
up 7
down 7
forward 7
forward 5
down 1
down 1
forward 3
down 8
forward 1
forward 2
forward 9
forward 1
forward 3
down 3
up 4
forward 5
down 1
forward 3
up 7
forward 3
forward 6
up 6
up 3
forward 9
forward 5
down 2
up 4
up 3
forward 3
forward 7
down 1
forward 5
forward 5
down 1
forward 4
forward 2
down 1
down 9
down 7
up 1
forward 2
down 2
forward 3
forward 8
forward 4
forward 6
down 4
down 1
forward 5
forward 1
forward 7
down 8
forward 9
down 6
forward 3
up 5
up 1
up 7
down 5
forward 7
forward 5
forward 5
up 1
forward 8
down 8
down 7
forward 9
forward 9
down 3
forward 7
forward 2
down 1
down 6
down 1
forward 7
down 3
forward 1
forward 1
forward 6
forward 6
up 9
down 3
forward 9
down 8
forward 4
up 6
down 4
down 7
forward 5
up 3
forward 1
forward 8
up 6
up 3
down 2
forward 2
forward 5
forward 1
down 8
down 8
down 3
forward 5
forward 4
forward 4
forward 5
up 5
forward 2
forward 5
up 5
forward 6
forward 6
forward 9
up 5
forward 4
up 4
forward 8
down 8
forward 5
forward 2
forward 4
forward 3
forward 1
down 1
down 9
down 2
forward 4
down 3
down 6
forward 2
up 7
forward 6
down 4
up 9
down 1
forward 8
forward 1
forward 1
down 9
down 3
down 2
down 7
up 5
down 7
up 9
down 8
down 7
forward 9
forward 7
up 4
forward 5
up 9
down 4
forward 1
forward 9
down 7
up 9
forward 6
forward 4
up 8
down 2
forward 1
up 6
up 5
down 4
forward 8
down 3
down 5
down 6
up 1
up 9
up 7
up 5
forward 1
forward 3
down 7
forward 9
forward 2
forward 6
down 4
down 7
forward 3
down 1
up 5
forward 3
down 3
down 1
forward 1
forward 4
forward 8
down 4
down 1
forward 3
down 7
up 9
down 8
down 1
forward 2
down 6
down 9
down 9
forward 2
forward 8
up 2
down 5
down 9
forward 1
up 9
down 7
forward 8
down 7
up 4
forward 8
down 8
down 7
forward 6
up 7
down 4
down 9
forward 9
up 8
down 8
down 8
down 8
down 5
forward 2
up 9
down 2
up 7
down 7
down 3
down 6
forward 9
forward 1
down 1
down 5
up 4
down 5
forward 5
up 2
forward 5
down 5
forward 1
forward 9
down 9
forward 3
forward 3
down 8
down 2
down 8
forward 8
forward 7
up 6
down 4
down 5
forward 8
forward 4
forward 7
forward 1
down 9
down 4
down 2
forward 5
down 3
down 7
down 5
forward 8
up 1
down 4
down 7
down 7
forward 2
up 5
forward 5
up 2
up 4
down 9
forward 7
forward 6
forward 6
down 2
forward 7
forward 7
down 7
forward 8
down 2
up 9
down 1
forward 9
down 9
forward 3
down 9
down 2
forward 9
forward 8
down 7
up 2
forward 8
forward 1
up 2
down 7
up 7
down 8
up 1
up 4
up 2
up 3
down 7
forward 1
down 8
down 4
down 2
down 4
up 8
forward 8
down 2
up 5
up 4
forward 7
up 1
forward 3
down 8
down 4
forward 4
down 8
forward 2
down 1
up 9
forward 9
down 4
up 2
down 8
up 9
forward 6
down 7
up 7
forward 9
forward 1
down 8
forward 5
down 9
forward 6
down 9
forward 9
forward 1
down 8
up 4
forward 9
forward 3
down 9
up 8
forward 4
up 8
forward 7
down 7
up 6
down 7
down 2
down 7
forward 3
forward 2
down 6
down 2
down 7
up 4
forward 5
down 5
forward 2
up 3
up 8
forward 8
forward 1
forward 7
down 7
down 2
forward 1
down 7
down 7
up 2
up 7
up 7
forward 4
down 5
forward 5
forward 7
forward 7
down 7
down 8
forward 8
forward 8
up 3
up 9
forward 2
down 7
up 3
up 1
up 1
down 9
up 5
down 6
up 8
up 3
up 5
forward 7
forward 3
forward 8
forward 4
up 1
forward 2
forward 1
up 5
forward 9
forward 8
down 7
up 1
forward 7
down 8
forward 1
forward 9
forward 9
forward 9
forward 8
down 1
forward 8
forward 7
up 9
up 3
forward 8
forward 2
up 2
down 7
down 6
forward 4
forward 3
forward 6
up 7
down 9
forward 1
forward 4
down 1
forward 4
up 3
down 8
forward 1
up 6
forward 8
forward 2
forward 1
forward 8
forward 4
down 7
forward 4
forward 6
down 2
up 4
forward 4
forward 3
down 5
forward 8
forward 4
forward 5
forward 7
forward 6
forward 5
forward 9
down 4
down 9
forward 6
up 7
down 6
down 3
down 2
up 9
forward 7
down 4
down 5
forward 2
forward 3
forward 2
forward 9
forward 7
forward 8
down 9
down 7
down 9
down 7
forward 5
forward 2
down 5
forward 6
down 1
down 2
down 6
forward 9
down 3
up 6
down 4
down 5
forward 3
forward 7
down 8
forward 2
forward 5
down 9
down 3
up 5
down 6
forward 6
up 3
down 6
down 1
down 8
down 5
down 3
forward 3
up 6
up 7
forward 8
forward 9
forward 2
forward 6
forward 2
forward 3
down 7
down 3
down 3
down 6
down 2
forward 4
forward 3
forward 8
up 1
down 9
forward 5
up 3
down 7
down 6
forward 8
forward 1
up 6
forward 3
forward 1
up 9
forward 6
forward 3
down 9
down 4
down 9
forward 5
down 8
down 3
forward 1
forward 1
down 9
down 6
down 3
up 7
down 3
forward 5
down 2
forward 7
forward 2
forward 5
up 7
forward 4
forward 4
up 3
down 6
down 7
up 1
down 6
forward 1
forward 9
down 7
down 8
forward 5
down 1
down 9
up 5
up 4
up 3
forward 6
down 6
forward 4
forward 8
up 6
up 2
down 9
forward 2
forward 5
forward 1
forward 3
forward 9
up 3
forward 2
forward 1
forward 3
forward 3
up 9
forward 3
forward 7
down 6
forward 2
down 8
up 9
forward 8
forward 5
forward 2
up 8
down 9
up 5
forward 3
down 4
forward 1
up 9
down 4
down 5
up 4
down 6
down 4
down 6
down 4
forward 4
down 2
down 1
down 6
forward 2
down 1
down 3
forward 4
down 3
down 5
down 5
up 1
up 4
down 4
down 4
down 5
down 4
down 5
forward 5
down 8
down 5
down 5
down 9
up 1
up 5
forward 5
down 1
down 9
down 4
down 3
forward 3
down 2
forward 9
down 3
forward 1
down 9
down 5
up 7
forward 3
forward 1
forward 2
down 5
forward 8
down 3
down 3
forward 6
down 8
down 3
down 8
up 9
forward 3
down 6
forward 4
down 6
down 4
up 5
forward 1
up 6
up 2
forward 2
down 8
forward 7
forward 8
down 6
down 7
forward 7
up 3
forward 3
up 6
forward 3
down 1
down 7
forward 9
forward 5
up 1
forward 7
forward 1
down 3
forward 1
up 4
up 2
up 1
down 8
forward 9
forward 3
forward 4
up 7
forward 5
down 1
down 8
down 3
down 4
down 6
down 5
forward 4
down 4
down 2
down 4
down 3
down 3
forward 4
up 3
forward 6
down 7
forward 4
up 2
down 7
forward 8
up 9
forward 6
forward 8
down 1
down 6
forward 6
down 6
down 9
up 8
forward 8
up 5
forward 6
forward 9
forward 4
up 2
forward 3
down 7
down 8
down 4
up 8
forward 8
forward 1
up 5
up 4
up 1
down 9
down 9
up 2
forward 9
down 7
down 2
up 2
down 1
forward 6
forward 2
down 5
down 8
forward 6
down 2
down 3
forward 6
forward 7
up 8
down 4
forward 5
down 9
down 2
down 7
down 9
down 5
forward 9
forward 2
down 6
forward 7
up 6
forward 3
up 2
forward 9
forward 2