Compare commits

...

4 Commits

Author SHA1 Message Date
e763653a96 test: add unit tests 2025-11-24 22:13:21 +01:00
13165c5fa3 feat: add main code 2025-11-24 22:13:16 +01:00
c5885f9bbf feat: add input 2025-11-24 22:13:12 +01:00
6931a63080 go: init 2021/day03 package 2025-11-24 22:13:08 +01:00
4 changed files with 1159 additions and 0 deletions

3
2021/day03/go.mod Normal file
View File

@@ -0,0 +1,3 @@
module 2021/day03
go 1.25.4

1000
2021/day03/input.txt Normal file

File diff suppressed because it is too large Load Diff

122
2021/day03/main.go Normal file
View File

@@ -0,0 +1,122 @@
package main
import (
"fmt"
"log"
"os"
"strings"
)
func parseInput(file string) []string {
content, err := os.ReadFile(file)
if err != nil {
log.Fatalf("Failed to read input file: %v", err)
}
return strings.Split(strings.TrimSpace(string(content)), "\n")
}
func PartOne(data []string) int {
bitlen := len(data[0])
ones := make([]int, bitlen)
for _, line := range data {
for i := range bitlen {
if line[i] == '1' {
ones[i]++
}
}
}
var gamma int
total := len(data)
for idx := range bitlen {
gamma <<= 1
if ones[idx] > total-ones[idx] {
gamma |= 1
}
}
mask := (1 << bitlen) - 1
epsilon := mask ^ gamma
return gamma * epsilon
}
func PartTwo(data []string) int {
bitlen := len(data[0])
oxygenIndices := make([]int, len(data))
for idx := range oxygenIndices {
oxygenIndices[idx] = idx
}
co2Indices := make([]int, len(data))
for idx := range co2Indices {
co2Indices[idx] = idx
}
for indice := 0; indice < bitlen && len(oxygenIndices) > 1; indice++ {
ones := 0
for _, idx := range oxygenIndices {
if data[idx][indice] == '1' {
ones++
}
}
target := byte('1')
if ones*2 < len(oxygenIndices) {
target = '0'
}
writeIdx := 0
for readIdx := 0; readIdx < len(oxygenIndices); readIdx++ {
if data[oxygenIndices[readIdx]][indice] == target {
oxygenIndices[writeIdx] = oxygenIndices[readIdx]
writeIdx++
}
}
oxygenIndices = oxygenIndices[:writeIdx]
}
for idx := 0; idx < bitlen && len(co2Indices) > 1; idx++ {
ones := 0
for _, indice := range co2Indices {
if data[indice][idx] == '1' {
ones++
}
}
target := byte('0')
if ones*2 < len(co2Indices) {
target = '1'
}
writeIdx := 0
for readIdx := 0; readIdx < len(co2Indices); readIdx++ {
if data[co2Indices[readIdx]][idx] == target {
co2Indices[writeIdx] = co2Indices[readIdx]
writeIdx++
}
}
co2Indices = co2Indices[:writeIdx]
}
oxygenRating := 0
for _, char := range data[oxygenIndices[0]] {
oxygenRating <<= 1
if char == '1' {
oxygenRating |= 1
}
}
co2Rating := 0
for _, char := range data[co2Indices[0]] {
co2Rating <<= 1
if char == '1' {
co2Rating |= 1
}
}
return oxygenRating * co2Rating
}
func main() {
data := parseInput("input.txt")
fmt.Println("Part 1:", PartOne(data))
fmt.Println("Part 2:", PartTwo(data))
}

34
2021/day03/main_test.go Normal file
View File

@@ -0,0 +1,34 @@
package main
import "testing"
var testInput = []string{
"00100",
"11110",
"10110",
"10111",
"10101",
"01111",
"00111",
"11100",
"10000",
"11001",
"00010",
"01010",
}
func TestPartOne(t *testing.T) {
expected := 198
got := PartOne(testInput)
if got != expected {
t.Errorf("PartOne() = %d, want %d", got, expected)
}
}
func TestPartTwo(t *testing.T) {
expected := 230
got := PartTwo(testInput)
if got != expected {
t.Errorf("PartTwo() = %d, want %d", got, expected)
}
}