generated from devries/aoc_template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
274 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package day03p1 | ||
|
||
import ( | ||
"io" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
symbols := make(map[utils.Point]rune) | ||
|
||
numbers := []Number{} | ||
|
||
for j, ln := range lines { | ||
innumber := false | ||
|
||
for i, r := range ln { | ||
switch { | ||
case r == '.': | ||
// do nothing | ||
innumber = false | ||
case r >= '0' && r <= '9': | ||
// number | ||
switch innumber { | ||
case false: | ||
innumber = true | ||
var newNumber Number | ||
newNumber.Start = utils.Point{X: i, Y: j} | ||
newNumber.End = newNumber.Start | ||
newNumber.Value = int64(r - '0') | ||
numbers = append(numbers, newNumber) | ||
case true: | ||
idx := len(numbers) - 1 | ||
numbers[idx].End = utils.Point{X: i, Y: j} | ||
numbers[idx].Value = numbers[idx].Value*10 + int64(r-'0') | ||
} | ||
default: | ||
// symbol | ||
symbols[utils.Point{X: i, Y: j}] = r | ||
innumber = false | ||
} | ||
} | ||
} | ||
|
||
var sum int64 | ||
|
||
for _, n := range numbers { | ||
surroundingPoints := pointsAround(n.Start, n.End) | ||
for _, p := range surroundingPoints { | ||
if symbols[p] != 0 { | ||
sum += n.Value | ||
break | ||
} | ||
} | ||
} | ||
|
||
return sum | ||
} | ||
|
||
type Number struct { | ||
Value int64 | ||
Start utils.Point | ||
End utils.Point | ||
} | ||
|
||
func pointsAround(start utils.Point, end utils.Point) []utils.Point { | ||
yupper := start.Y - 1 | ||
ylower := start.Y + 1 | ||
yline := start.Y | ||
|
||
xmin := start.X - 1 | ||
xmax := end.X + 1 | ||
|
||
result := []utils.Point{{X: xmin, Y: yline}, {X: xmax, Y: yline}} | ||
|
||
for i := xmin; i <= xmax; i++ { | ||
result = append(result, utils.Point{X: i, Y: yupper}) | ||
result = append(result, utils.Point{X: i, Y: ylower}) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package day03p1 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `467..114.. | ||
...*...... | ||
..35..633. | ||
......#... | ||
617*...... | ||
.....+.58. | ||
..592..... | ||
......755. | ||
...$.*.... | ||
.664.598..` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int64 | ||
}{ | ||
{testInput, 4361}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package day03p2 | ||
|
||
import ( | ||
"io" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
func Solve(r io.Reader) any { | ||
lines := utils.ReadLines(r) | ||
|
||
symbols := make(map[utils.Point]rune) | ||
|
||
numbers := []Number{} | ||
|
||
for j, ln := range lines { | ||
innumber := false | ||
|
||
for i, r := range ln { | ||
switch { | ||
case r == '.': | ||
// do nothing | ||
innumber = false | ||
case r >= '0' && r <= '9': | ||
// number | ||
switch innumber { | ||
case false: | ||
innumber = true | ||
var newNumber Number | ||
newNumber.Start = utils.Point{X: i, Y: j} | ||
newNumber.End = newNumber.Start | ||
newNumber.Value = int64(r - '0') | ||
numbers = append(numbers, newNumber) | ||
case true: | ||
idx := len(numbers) - 1 | ||
numbers[idx].End = utils.Point{X: i, Y: j} | ||
numbers[idx].Value = numbers[idx].Value*10 + int64(r-'0') | ||
} | ||
default: | ||
// symbol | ||
symbols[utils.Point{X: i, Y: j}] = r | ||
innumber = false | ||
} | ||
} | ||
} | ||
|
||
var sum int64 | ||
|
||
gearAdjacents := make(map[utils.Point][]int64) | ||
|
||
for _, n := range numbers { | ||
surroundingPoints := pointsAround(n.Start, n.End) | ||
for _, p := range surroundingPoints { | ||
if symbols[p] == '*' { | ||
gearAdjacents[p] = append(gearAdjacents[p], n.Value) | ||
break | ||
} | ||
} | ||
} | ||
|
||
for _, nums := range gearAdjacents { | ||
if len(nums) == 2 { | ||
sum += nums[0] * nums[1] | ||
} | ||
} | ||
|
||
return sum | ||
} | ||
|
||
type Number struct { | ||
Value int64 | ||
Start utils.Point | ||
End utils.Point | ||
} | ||
|
||
func pointsAround(start utils.Point, end utils.Point) []utils.Point { | ||
yupper := start.Y - 1 | ||
ylower := start.Y + 1 | ||
yline := start.Y | ||
|
||
xmin := start.X - 1 | ||
xmax := end.X + 1 | ||
|
||
result := []utils.Point{{X: xmin, Y: yline}, {X: xmax, Y: yline}} | ||
|
||
for i := xmin; i <= xmax; i++ { | ||
result = append(result, utils.Point{X: i, Y: yupper}) | ||
result = append(result, utils.Point{X: i, Y: ylower}) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package day03p2 | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"aoc/utils" | ||
) | ||
|
||
var testInput = `467..114.. | ||
...*...... | ||
..35..633. | ||
......#... | ||
617*...... | ||
.....+.58. | ||
..592..... | ||
......755. | ||
...$.*.... | ||
.664.598..` | ||
|
||
func TestSolve(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
answer int64 | ||
}{ | ||
{testInput, 467835}, | ||
} | ||
|
||
if testing.Verbose() { | ||
utils.Verbose = true | ||
} | ||
|
||
for _, test := range tests { | ||
r := strings.NewReader(test.input) | ||
|
||
result := Solve(r).(int64) | ||
|
||
if result != test.answer { | ||
t.Errorf("Expected %d, got %d", test.answer, result) | ||
} | ||
} | ||
} |
Submodule inputs
updated
from 7b4edb to 9dab28