mirror of
https://github.com/RussellChamp/advent-of-code-2021.git
synced 2026-10-27 21:52:32 +00:00
add day 2 results
This commit is contained in:
1 parent
92dd557177
commit
73933e8e39
5 files changed
+145
-2
No files matched your search
+111
@@ -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)
|
||||||
|
}
|
||||||
@@ -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")
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
default: build day=1
|
default:
|
||||||
# This is my 2021 Advent of Code project
|
# This is my 2021 Advent of Code project
|
||||||
|
|
||||||
init:
|
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:
|
build:
|
||||||
cd day-$(day); go run .
|
cd day-$(day); go run .
|
||||||
|
|||||||
@@ -2,6 +2,13 @@ package assert
|
|||||||
|
|
||||||
import "testing"
|
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) {
|
func StrEquals(t *testing.T, actual, expected, message string) {
|
||||||
if actual != expected {
|
if actual != expected {
|
||||||
t.Logf("Expected value '%s' but got '%s': %s", expected, actual, message)
|
t.Logf("Expected value '%s' but got '%s': %s", expected, actual, message)
|
||||||
|
|||||||
Reference in new issue
Block a user