diff --git a/README.md b/README.md index 734bfcf..75fd6d4 100644 --- a/README.md +++ b/README.md @@ -20,17 +20,17 @@ Each day's solution is contained in a separate package. ## Challenges -| Day | Problem | Solution | Notes | -|-----|-------------------------------------------------|-----------------------|----------------------------------------| -| 01 | [Problem](https://adventofcode.com/2025/day/1) | [Solution](./cmd/01/) | Part 1 seemed straight forward enough. | -| 02 | [Problem](https://adventofcode.com/2025/day/2) | [Solution](./cmd/02/) | | -| 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | | -| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | | -| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | | -| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | | -| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | | -| 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | | -| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | | -| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | -| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | | -| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | | +| Day | Problem | Solution | Notes | +|-----|-------------------------------------------------|-----------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| 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/) | | +| 03 | [Problem](https://adventofcode.com/2025/day/3) | [Solution](./cmd/03/) | | +| 04 | [Problem](https://adventofcode.com/2025/day/4) | [Solution](./cmd/04/) | | +| 05 | [Problem](https://adventofcode.com/2025/day/5) | [Solution](./cmd/05/) | | +| 06 | [Problem](https://adventofcode.com/2025/day/6) | [Solution](./cmd/06/) | | +| 07 | [Problem](https://adventofcode.com/2025/day/7) | [Solution](./cmd/07/) | | +| 08 | [Problem](https://adventofcode.com/2025/day/8) | [Solution](./cmd/08/) | | +| 09 | [Problem](https://adventofcode.com/2025/day/9) | [Solution](./cmd/09/) | | +| 10 | [Problem](https://adventofcode.com/2025/day/10) | [Solution](./cmd/10/) | | +| 11 | [Problem](https://adventofcode.com/2025/day/11) | [Solution](./cmd/11/) | | +| 12 | [Problem](https://adventofcode.com/2025/day/12) | [Solution](./cmd/12/) | | diff --git a/cmd/01/dial/dial.go b/cmd/01/dial/dial.go index e75f273..219a3c0 100644 --- a/cmd/01/dial/dial.go +++ b/cmd/01/dial/dial.go @@ -7,42 +7,76 @@ import ( ) type Dial interface { + ZeroCount() int Value() int Turn(instruction instruction.Instruction) } type dial struct { - value int + zeroCount int + value int } func New() 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 { return d.value } func (d *dial) Turn(instruction instruction.Instruction) { if instruction.Direction() == direction.Left { - d.value -= instruction.Distance() + for range instruction.Distance() { + d.decreaseOneClick() - for d.value < 0 { - d.value += 100 + if d.value == 0 { + d.zeroCount++ + } + + if d.value < 0 { + d.value += 100 + } } return } if instruction.Direction() == direction.Right { - d.value += instruction.Distance() + for range instruction.Distance() { + d.increaseOneClick() - for d.value > 99 { - d.value -= 100 + if d.value >= 100 { + d.value -= 100 + } + + if d.value == 0 { + d.zeroCount++ + } } return } } + +func (d *dial) decreaseOneClick() { + d.value-- +} + +func (d *dial) increaseOneClick() { + d.value++ +} diff --git a/cmd/01/dial/dial_test.go b/cmd/01/dial/dial_test.go index 37c13cf..e50ca8a 100644 --- a/cmd/01/dial/dial_test.go +++ b/cmd/01/dial/dial_test.go @@ -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 { Instruction instruction.Instruction ExpectedDialValue int diff --git a/cmd/01/main.go b/cmd/01/main.go index 2247a83..d064824 100644 --- a/cmd/01/main.go +++ b/cmd/01/main.go @@ -26,3 +26,19 @@ func SolvePartOne(filePath string) int { 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() +} diff --git a/cmd/01/main_test.go b/cmd/01/main_test.go index c69c8f5..1e175bb 100644 --- a/cmd/01/main_test.go +++ b/cmd/01/main_test.go @@ -25,3 +25,23 @@ func TestSolvePartOneWithInput(t *testing.T) { 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) + } +} diff --git a/internal/file/file.go b/internal/file/file.go index ce90c26..b76a3fd 100644 --- a/internal/file/file.go +++ b/internal/file/file.go @@ -3,7 +3,6 @@ package file import ( "bufio" - "fmt" "os" ) @@ -20,7 +19,6 @@ func ReadLines(filePath string) []string { for hasLine := scanner.Scan(); hasLine; hasLine = scanner.Scan() { line := scanner.Text() - fmt.Println(line) lines = append(lines, line) }