Skip to content

Commit

Permalink
day10
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 10, 2024
1 parent e0c8c87 commit 0fd2c49
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 0 deletions.
52 changes: 52 additions & 0 deletions day10p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package day10p1

import (
"io"

"aoc/utils"
)

type position struct {
x, y int
}

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

board := make([][]int, len(lines))
for i, line := range lines {
board[i] = make([]int, len(line))
for j, c := range line {
board[i][j] = int(c) - int('0')
}
}

scores := 0

for i, row := range board {
for j, _ := range row {
if row[j] != 0 {
continue
}
visited := make(map[position]bool)
queue := []position{{i, j}}
for len(queue) > 0 {
pos := queue[0]
queue = queue[1:]
if board[pos.x][pos.y] == 9 {
if !visited[pos] {
scores++
}
visited[pos] = true
}
for _, dir := range []position{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
if pos.x+dir.x >= 0 && pos.x+dir.x < len(board) && pos.y+dir.y >= 0 && pos.y+dir.y < len(board[0]) && (board[pos.x+dir.x][pos.y+dir.y]-board[pos.x][pos.y]) == 1 {
queue = append(queue, position{pos.x + dir.x, pos.y + dir.y})
}
}
}
}
}

return scores
}
40 changes: 40 additions & 0 deletions day10p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package day10p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 36},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}
48 changes: 48 additions & 0 deletions day10p2/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package day10p2

import (
"io"

"aoc/utils"
)

type position struct {
x, y int
}

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)

board := make([][]int, len(lines))
for i, line := range lines {
board[i] = make([]int, len(line))
for j, c := range line {
board[i][j] = int(c) - int('0')
}
}

scores := 0

for i, row := range board {
for j, _ := range row {
if row[j] != 0 {
continue
}
queue := []position{{i, j}}
for len(queue) > 0 {
pos := queue[0]
queue = queue[1:]
if board[pos.x][pos.y] == 9 {
scores++
}
for _, dir := range []position{{0, 1}, {0, -1}, {1, 0}, {-1, 0}} {
if pos.x+dir.x >= 0 && pos.x+dir.x < len(board) && pos.y+dir.y >= 0 && pos.y+dir.y < len(board[0]) && (board[pos.x+dir.x][pos.y+dir.y]-board[pos.x][pos.y]) == 1 {
queue = append(queue, position{pos.x + dir.x, pos.y + dir.y})
}
}
}
}
}

return scores
}
40 changes: 40 additions & 0 deletions day10p2/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package day10p2

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732`

func TestSolve(t *testing.T) {
tests := []struct {
input string
answer int
}{
{testInput, 81},
}

if testing.Verbose() {
utils.Verbose = true
}

for _, test := range tests {
r := strings.NewReader(test.input)

result := Solve(r).(int)

if result != test.answer {
t.Errorf("Expected %d, got %d", test.answer, result)
}
}
}

0 comments on commit 0fd2c49

Please sign in to comment.