diff --git a/day-2/main.go b/day-2/main.go new file mode 100644 index 0000000..0a7c0f0 --- /dev/null +++ b/day-2/main.go @@ -0,0 +1,111 @@ +/* Day 2: Dive! */ +package main + +import ( + "AoC2021/utils/timer" + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func check(e error) { + if e != nil { + fmt.Println("Oh snap!") + panic(e) + } +} + +func main() { + fmt.Println("--- Day 2: Dive! ---") + timer.Start() + part1() + timer.Tick() + fmt.Println() + + part2() + timer.Tick() + fmt.Println() +} + +func part1() { + fmt.Println("* Part 1 *") + fmt.Println(" Goal: Calculate the horizontal position and depth you would have after following the planned course") + fmt.Println(" Answer: What do you get if you multiply your final horizontal position by your final depth?") + + input, err := os.Open("./input.txt") + defer input.Close() + check(err) + + scanner := bufio.NewScanner(input) + lines := 0 + posX, posY := 0, 0 + + for scanner.Scan() { + str := scanner.Text() + values := strings.Fields(str) + if len(values) < 2 { + panic("read invalid line: " + str) + } + direction := values[0] + units, err := strconv.Atoi(values[1]) + check(err) + switch direction { + case "forward": + posX += units + case "down": + posY += units + case "up": + posY -= units + default: + panic("Invalid direction: " + direction) + } + + lines++ + } + + fmt.Printf("Read %d lines from input\n", lines) + fmt.Printf("The final position is (%d, %d) for an answer of %d\n", posX, posY, posX*posY) +} + +func part2() { + fmt.Println("* Part 2 *") + fmt.Println(" Goal: Using this new interpretation of the commands, calculate the horizontal position and depth you would have after following the planned course") + fmt.Println(" Answer: What do you get if you multiply your final horizontal position by your final depth?") + + input, err := os.Open("./input.txt") + defer input.Close() + check(err) + + scanner := bufio.NewScanner(input) + lines := 0 + posX, posY, aim := 0, 0, 0 + + for scanner.Scan() { + str := scanner.Text() + values := strings.Fields(str) + if len(values) < 2 { + panic("read invalid line: " + str) + } + direction := values[0] + units, err := strconv.Atoi(values[1]) + check(err) + switch direction { + case "forward": + posX += units + posY += units * aim + case "down": + aim += units + case "up": + aim -= units + default: + panic("Invalid direction: " + direction) + } + + lines++ + } + + fmt.Printf("Read %d lines from input\n", lines) + fmt.Printf("The final position is (%d, %d) for an answer of %d\n", posX, posY, posX*posY) +} diff --git a/day-2/main_test.go b/day-2/main_test.go new file mode 100644 index 0000000..0afaaf4 --- /dev/null +++ b/day-2/main_test.go @@ -0,0 +1,11 @@ +package main + +import ( + assert "AoC2021/utils/testing" + "testing" +) + +func TestDay2(t *testing.T) { + assert.IsTrue(t, true, "cool") + assert.StrEquals(t, "foo", "bar", "baz") +} diff --git a/main_test.go.template b/main_test.go.template new file mode 100644 index 0000000..8ba5789 --- /dev/null +++ b/main_test.go.template @@ -0,0 +1,11 @@ +package main + +import ( + assert "AoC2021/utils/testing" + "testing" +) + +func TestDay#DAY#(t *testing.T) { + assert.IsTrue(t, true, "cool") + assert.StrEquals(t, "foo", "bar", c.msg) +} diff --git a/makefile b/makefile index 3468bf7..06f2d7d 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,11 @@ -default: build day=1 +default: # This is my 2021 Advent of Code project init: - mkdir day-$(day); sed 's/#DAY#/$(day)/g' main.go.template | sed 's/#TITLE#/$(title)/g' > day-$(day)/main.go; touch day-$(day)/input.txt + mkdir day-$(day) + sed 's/#DAY#/$(day)/g' main.go.template | sed 's/#TITLE#/$(title)/g' > day-$(day)/main.go + sed 's/#DAY#/$(day)/g' main_test.go.template > day-$(day)/main_test.go + touch day-$(day)/input.txt; build: cd day-$(day); go run . diff --git a/utils/testing/assert.go b/utils/testing/assert.go index 9d89a1b..6375cf7 100644 --- a/utils/testing/assert.go +++ b/utils/testing/assert.go @@ -2,6 +2,13 @@ package assert import "testing" +func IsTrue(t *testing.T, expr bool, message string) { + if expr != true { + t.Logf("Expected true but got false: %s", message) + t.Fail() + } +} + func StrEquals(t *testing.T, actual, expected, message string) { if actual != expected { t.Logf("Expected value '%s' but got '%s': %s", expected, actual, message)