Skip to content

Commit

Permalink
Day 25 Merry Christmas!
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 25, 2024
1 parent 814f537 commit a00ce2c
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 0 deletions.
79 changes: 79 additions & 0 deletions day25p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package day25p1

import (
"io"

"aoc/utils"
)

type Key [5]int
type Lock [5]int

func (k Key) toLock() Lock {
var lock Lock
for i, v := range k {
lock[i] = v
}
return lock
}

func Solve(r io.Reader) any {
blocks := utils.ReadBlocks(r)

keys := make([]Key, 0)
locks := make([]Lock, 0)

for _, block := range blocks {
isKey := checkKey(block)
if isKey {
keys = append(keys, parseKey(block))
} else {
locks = append(locks, parseLock(block))
}
}

nonoverlapping := 0

for _, key := range keys {
for _, lock := range locks {
overlapping := false
for i := 0; i < 5; i++ {
if key[i]+lock[i] > 5 {
overlapping = true
break
}
}
if !overlapping {
nonoverlapping += 1
}
}
}

return nonoverlapping
}

func checkKey(block []string) bool {
if block[0] == "....." {
return true
} else if block[0] == "#####" {
return false
} else {
panic("Invalid block")
}
}

func parseKey(block []string) Key {
var key Key
for _, row := range block[1 : len(block)-1] {
for j, c := range row {
if c == '#' {
key[j] += 1
}
}
}
return key
}

func parseLock(block []string) Lock {
return parseKey(block).toLock()
}
71 changes: 71 additions & 0 deletions day25p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package day25p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####`

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

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)
}
}
}
25 changes: 25 additions & 0 deletions utils/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ func ReadLines(r io.Reader) []string {
return result
}

// Read until \n\n and return blocks of lines
func ReadBlocks(r io.Reader) [][]string {
result := [][]string{}

scanner := bufio.NewScanner(r)
block := []string{}
for scanner.Scan() {
line := scanner.Text()
if line == "" {
result = append(result, block)
block = []string{}
} else {
block = append(block, line)
}
}
err := scanner.Err()
Check(err, "error reading blocks")

if len(block) > 0 {
result = append(result, block)
}

return result
}

func GetInts(s string) []int {
matches := intRegex.FindAllString(s, -1)
result := make([]int, len(matches))
Expand Down

0 comments on commit a00ce2c

Please sign in to comment.