Skip to content

Commit ec1dfe3

Browse files
committed
refactor: day 9 cleanup
1 parent 5c4631d commit ec1dfe3

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

9/main.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ func (pos Position) Move(dx, dy int) Position {
88
return Position{pos.x + dx, pos.y + dy}
99
}
1010

11-
func (pos Position) Distance(p Position) Position {
11+
func (pos Position) Add(p Position) Position {
12+
return Position{pos.x + p.x, pos.y + p.y}
13+
}
14+
15+
func (pos Position) Subtract(p Position) Position {
1216
return Position{pos.x - p.x, pos.y - p.y}
1317
}
1418

@@ -29,7 +33,7 @@ func Sign(x int) int {
2933
return 0
3034
}
3135

32-
func DirDelta(dir string) Position {
36+
func DirPosition(dir string) Position {
3337
switch dir {
3438
case "L":
3539
return Position{-1, 0}

9/part1.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ func part1() {
2626
continue
2727
}
2828
fields := strings.Fields(line)
29-
delta := DirDelta(fields[0])
29+
delta := DirPosition(fields[0])
3030
for steps, _ := strconv.Atoi(fields[1]); steps > 0; steps-- {
31-
head = head.Move(delta.x, delta.y)
32-
dist := head.Distance(tail)
31+
head = head.Add(delta)
32+
dist := head.Subtract(tail)
3333
if Abs(dist.x) > 1 || Abs(dist.y) > 1 {
3434
tail = tail.Move(Sign(dist.x), Sign(dist.y))
3535
positions[fmt.Sprintf("%v,%v", tail.x, tail.y)] = true

9/part2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func part2() {
2727
continue
2828
}
2929
fields := strings.Fields(line)
30-
delta := DirDelta(fields[0])
30+
delta := DirPosition(fields[0])
3131
for steps, _ := strconv.Atoi(fields[1]); steps > 0; steps-- {
32-
ropes[0] = ropes[0].Move(delta.x, delta.y)
32+
ropes[0] = ropes[0].Add(delta)
3333
for r := 1; r < TailSize; r++ {
34-
dist := ropes[r-1].Distance(ropes[r])
34+
dist := ropes[r-1].Subtract(ropes[r])
3535
if Abs(dist.x) > 1 || Abs(dist.y) > 1 {
3636
ropes[r] = ropes[r].Move(Sign(dist.x), Sign(dist.y))
3737
if r == TailSize-1 {

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
# advent-of-code-2022
2-
Advent of Code (2022)
1+
# Advent of Code (2022)
2+
3+
This repository has my solutions for this year's Advent of Code contest. They may not be pretty or optimal, but although it's hard to beat Python in expressiveness and brevity, I still try to solve the problems as fast as I can in Go.
4+
5+
I only do minor code cleanup after obtaining the stars for the day and do not change the algorithm, regardless of how embarrassing it could be!

0 commit comments

Comments
 (0)