Merge pull request #2 from StevanFreeborn/stevanfreeborn/feat/solve-day-one-part-two
feat: solve day one part two
This commit is contained in:
@@ -20,17 +20,17 @@ Each day's solution is contained in a separate package.
|
|||||||
|
|
||||||
## Challenges
|
## Challenges
|
||||||
|
|
||||||
| Day | Problem | Solution | Notes |
|
| Day | Problem | Solution | Notes |
|
||||||
|-----|-------------------------------------------------|-----------------------|----------------------------------------|
|
|-----|-------------------------------------------------|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
||||||
| 01 | [Problem](https://adventofcode.com/2025/day/1) | [Solution](./cmd/01/) | Part 1 seemed straight forward enough. |
|
| 01 | [Problem](https://adventofcode.com/2025/day/1) | [Solution](./cmd/01/) | Part 1 seemed straight forward enough. Part 2 completely stumped me. The trickiest part was not counting zero's twice when you ended and then started from there. |
|
||||||
| 02 | [Problem](https://adventofcode.com/2025/day/2) | [Solution](./cmd/02/) | |
|
| 02 | [Problem](https://adventofcode.com/2025/day/2) | [Solution](./cmd/02/) | |
|
||||||
| 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | |
|
| 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | |
|
||||||
| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | |
|
| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | |
|
||||||
| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | |
|
| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | |
|
||||||
| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | |
|
| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | |
|
||||||
| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | |
|
| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | |
|
||||||
| 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | |
|
| 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | |
|
||||||
| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | |
|
| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | |
|
||||||
| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | |
|
| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | |
|
||||||
| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | |
|
| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | |
|
||||||
| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | |
|
| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | |
|
||||||
|
|||||||
+42
-8
@@ -7,42 +7,76 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Dial interface {
|
type Dial interface {
|
||||||
|
ZeroCount() int
|
||||||
Value() int
|
Value() int
|
||||||
Turn(instruction instruction.Instruction)
|
Turn(instruction instruction.Instruction)
|
||||||
}
|
}
|
||||||
|
|
||||||
type dial struct {
|
type dial struct {
|
||||||
value int
|
zeroCount int
|
||||||
|
value int
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() Dial {
|
func New() Dial {
|
||||||
return &dial{
|
return &dial{
|
||||||
value: 50,
|
zeroCount: 0,
|
||||||
|
value: 50,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func From(value int) Dial {
|
||||||
|
return &dial{
|
||||||
|
zeroCount: 0,
|
||||||
|
value: value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dial) ZeroCount() int {
|
||||||
|
return d.zeroCount
|
||||||
|
}
|
||||||
|
|
||||||
func (d *dial) Value() int {
|
func (d *dial) Value() int {
|
||||||
return d.value
|
return d.value
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *dial) Turn(instruction instruction.Instruction) {
|
func (d *dial) Turn(instruction instruction.Instruction) {
|
||||||
if instruction.Direction() == direction.Left {
|
if instruction.Direction() == direction.Left {
|
||||||
d.value -= instruction.Distance()
|
for range instruction.Distance() {
|
||||||
|
d.decreaseOneClick()
|
||||||
|
|
||||||
for d.value < 0 {
|
if d.value == 0 {
|
||||||
d.value += 100
|
d.zeroCount++
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.value < 0 {
|
||||||
|
d.value += 100
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if instruction.Direction() == direction.Right {
|
if instruction.Direction() == direction.Right {
|
||||||
d.value += instruction.Distance()
|
for range instruction.Distance() {
|
||||||
|
d.increaseOneClick()
|
||||||
|
|
||||||
for d.value > 99 {
|
if d.value >= 100 {
|
||||||
d.value -= 100
|
d.value -= 100
|
||||||
|
}
|
||||||
|
|
||||||
|
if d.value == 0 {
|
||||||
|
d.zeroCount++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (d *dial) decreaseOneClick() {
|
||||||
|
d.value--
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *dial) increaseOneClick() {
|
||||||
|
d.value++
|
||||||
|
}
|
||||||
|
|||||||
@@ -16,6 +16,25 @@ func TestNew(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestProblem(t *testing.T) {
|
||||||
|
instructions := []instruction.Instruction{
|
||||||
|
instruction.FromLine("R48"),
|
||||||
|
instruction.FromLine("L5"),
|
||||||
|
}
|
||||||
|
|
||||||
|
dial := dial.From(52)
|
||||||
|
|
||||||
|
for _, i := range instructions {
|
||||||
|
dial.Turn(i)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := dial.ZeroCount()
|
||||||
|
|
||||||
|
if result != 1 {
|
||||||
|
t.Errorf("ya done fucked up. you wanted %d but got %d", 1, result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type TurnTestCase struct {
|
type TurnTestCase struct {
|
||||||
Instruction instruction.Instruction
|
Instruction instruction.Instruction
|
||||||
ExpectedDialValue int
|
ExpectedDialValue int
|
||||||
|
|||||||
@@ -26,3 +26,19 @@ func SolvePartOne(filePath string) int {
|
|||||||
|
|
||||||
return total
|
return total
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SolvePartTwo(filePath string) int {
|
||||||
|
lines := file.ReadLines(filePath)
|
||||||
|
dial := dial.New()
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
if line == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
instruction := instruction.FromLine(line)
|
||||||
|
dial.Turn(instruction)
|
||||||
|
}
|
||||||
|
|
||||||
|
return dial.ZeroCount()
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) {
|
|||||||
t.Errorf(`SolvePartOne("INPUT.txt") = %d; want %d`, result, expected)
|
t.Errorf(`SolvePartOne("INPUT.txt") = %d; want %d`, result, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSolvePartTwoWithExampleInput(t *testing.T) {
|
||||||
|
expected := 6
|
||||||
|
|
||||||
|
result := solution.SolvePartTwo("EXAMPLE.txt")
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf(`SolvePartTwo("EXAMPLE.txt") = %d; want %d`, result, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSolvePartTwoWithInput(t *testing.T) {
|
||||||
|
expected := 5937
|
||||||
|
|
||||||
|
result := solution.SolvePartTwo("INPUT.txt")
|
||||||
|
|
||||||
|
if result != expected {
|
||||||
|
t.Errorf(`SolvePartTwo("INPUT.txt") = %d; want %d`, result, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package file
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -20,7 +19,6 @@ func ReadLines(filePath string) []string {
|
|||||||
|
|
||||||
for hasLine := scanner.Scan(); hasLine; hasLine = scanner.Scan() {
|
for hasLine := scanner.Scan(); hasLine; hasLine = scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
fmt.Println(line)
|
|
||||||
lines = append(lines, line)
|
lines = append(lines, line)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user