Skip to content

Commit

Permalink
day24p1
Browse files Browse the repository at this point in the history
  • Loading branch information
gamersi committed Dec 24, 2024
1 parent 1acf410 commit 1912083
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 0 deletions.
89 changes: 89 additions & 0 deletions day24p1/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package day24p1

import (
"fmt"
"io"
"sort"

"aoc/utils"
)

type Gate struct {
Op string
Left string
Right string
Dest string
}

func Solve(r io.Reader) any {
lines := utils.ReadLines(r)
wires := make(map[string]byte)
gates := make(map[Gate]bool)

wireDecs := true

for _, line := range lines {
if line == "" {
wireDecs = false
continue
} else if wireDecs {
var wire string
var val byte
if _, err := fmt.Sscanf(line, "%3s: %d", &wire, &val); err != nil {
panic(err)
}
wires[wire] = val
} else {
var g Gate
if _, err := fmt.Sscanf(line, "%s %s %s -> %s", &g.Left, &g.Op, &g.Right, &g.Dest); err != nil {
panic(err)
}
gates[g] = false
}
}

for len(gates) > 0 {
for g := range gates {
if g.Left != "" {
if _, ok := wires[g.Left]; !ok {
continue
}
}
if g.Right != "" {
if _, ok := wires[g.Right]; !ok {
continue
}
}
switch g.Op {
case "AND":
wires[g.Dest] = wires[g.Left] & wires[g.Right]
case "OR":
wires[g.Dest] = wires[g.Left] | wires[g.Right]
case "XOR":
wires[g.Dest] = wires[g.Left] ^ wires[g.Right]
}
delete(gates, g)
}
}

return produceResult(wires)
}

func produceResult(wires map[string]byte) int {
var keys []string
for k := range wires {
if k[0] == 'z' {
keys = append(keys, k)
}
}

sort.Strings(keys)

var result int
for i, k := range keys {
if wires[k] == 1 {
result |= 1 << i
}
}
return result
}
79 changes: 79 additions & 0 deletions day24p1/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package day24p1

import (
"strings"
"testing"

"aoc/utils"
)

var testInput = `x00: 1
x01: 0
x02: 1
x03: 1
x04: 0
y00: 1
y01: 1
y02: 1
y03: 1
y04: 1
ntg XOR fgs -> mjb
y02 OR x01 -> tnw
kwq OR kpj -> z05
x00 OR x03 -> fst
tgd XOR rvg -> z01
vdt OR tnw -> bfw
bfw AND frj -> z10
ffh OR nrd -> bqk
y00 AND y03 -> djm
y03 OR y00 -> psh
bqk OR frj -> z08
tnw OR fst -> frj
gnj AND tgd -> z11
bfw XOR mjb -> z00
x03 OR x00 -> vdt
gnj AND wpb -> z02
x04 AND y00 -> kjc
djm OR pbm -> qhw
nrd AND vdt -> hwm
kjc AND fst -> rvg
y04 OR y02 -> fgs
y01 AND x02 -> pbm
ntg OR kjc -> kwq
psh XOR fgs -> tgd
qhw XOR tgd -> z09
pbm OR djm -> kpj
x03 XOR y03 -> ffh
x00 XOR y04 -> ntg
bfw OR bqk -> z06
nrd XOR fgs -> wpb
frj XOR qhw -> z04
bqk OR frj -> z07
y03 OR x01 -> nrd
hwm AND bqk -> z03
tgd XOR rvg -> z12
tnw OR pbm -> gnj`

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

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 1912083

Please sign in to comment.