lol im never going to finish updating the python solutions...

This commit is contained in:
alexchao26
2025-12-01 19:45:44 -05:00
parent 778c6f3fc8
commit a4ab3b5940
2 changed files with 162 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
package main
import (
_ "embed"
"flag"
"fmt"
"strings"
"github.com/alexchao26/advent-of-code-go/util"
)
//go:embed input.txt
var input string
func init() {
// do this in init (not main) so test file has same input
input = strings.TrimRight(input, "\n")
if len(input) == 0 {
panic("empty input.txt file")
}
}
func main() {
var part int
flag.IntVar(&part, "part", 1, "part 1 or 2")
flag.Parse()
fmt.Println("Running part", part)
ans := part1(input)
util.CopyToClipboard(fmt.Sprintf("%v", ans))
fmt.Println("Output:", ans)
}
func part1(input string) int {
parts := strings.Split(input, "\n\n")
locks := [][]int{}
keys := [][]int{}
maxHeight := 0
for _, part := range parts {
lines := strings.Split(part, "\n")
maxHeight = len(lines) - 1
// lock detection: first line is all '#'
fullLock := strings.Repeat("#", len(lines[0]))
if lines[0] == fullLock {
lockHeights := []int{}
// for each column, find first non-dot from bottom
for col := range len(lines[0]) {
for row := len(lines) - 1; row >= 0; row-- {
if lines[row][col:col+1] != "." {
lockHeights = append(lockHeights, row)
break
}
}
}
locks = append(locks, lockHeights)
} else {
// key detection: for each column, find first non-dot from top
keyHeights := []int{}
for col := range len(lines[0]) {
for row := range len(lines) {
if lines[row][col:col+1] != "." {
keyHeights = append(keyHeights, len(lines)-1-row)
break
}
}
}
keys = append(keys, keyHeights)
}
}
count := 0
for _, lock := range locks {
for _, key := range keys {
fits := true
for col := range len(key) {
if lock[col]+key[col] >= maxHeight {
fits = false
break
}
}
if fits {
count += 1
}
}
}
return count
}
+71
View File
@@ -0,0 +1,71 @@
package main
import (
"testing"
)
var example = `#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####`
func Test_part1(t *testing.T) {
tests := []struct {
name string
input string
want int
}{
{
name: "example",
input: example,
want: 3,
},
{
name: "actual",
input: input,
want: 3114,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := part1(tt.input); got != tt.want {
t.Errorf("part1() = %v, want %v", got, tt.want)
}
})
}
}