Skip to content

Commit

Permalink
new version without ugly parser :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar Franzén committed Dec 25, 2017
1 parent 633ea5a commit 181584e
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions digidis-go/day25/day25.go
Expand Up @@ -2,65 +2,40 @@ package main

import (
"fmt"
"io/ioutil"
"strconv"
"strings"
)

type rule struct {
v bool
v int
p int
n string
}

func main() {
rules := map[string][2]rule{
"F": [2]rule{rule{0, 1, "C"}, rule{0, 1, "E"}},
"A": [2]rule{rule{1, 1, "B"}, rule{0, -1, "D"}},
"B": [2]rule{rule{1, 1, "C"}, rule{0, 1, "F"}},
"C": [2]rule{rule{1, -1, "C"}, rule{1, -1, "A"}},
"D": [2]rule{rule{0, -1, "E"}, rule{1, 1, "A"}},
"E": [2]rule{rule{1, -1, "A"}, rule{0, 1, "B"}},
}

s := "A"
steps := 12302209

s, c, rules := parse()
var pos, checksum int
v := make(map[int]bool)
v := make(map[int]int)

for ; c > 0; c-- {
for c := 0; c < steps; c++ {
i := v[pos]
v[pos] = rules[s][i].v
pos += rules[s][i].p
s = rules[s][i].n
}

for i := range v {
if v[i] {
checksum++
}
for _, i := range v {
checksum += i
}

fmt.Printf("Checksum: %v\n", checksum)
}

func parse() (string, int, map[string]map[bool]rule) {
data, _ := ioutil.ReadFile("input.txt")
parts := strings.Split(string(data), "\n\n")

rules := make(map[string]map[bool]rule)

rows := strings.Split(parts[0], "\n")
steps, _ := strconv.Atoi(strings.Fields(rows[1])[5])
state := strings.TrimSuffix(strings.Fields(rows[0])[3], ".")

for _, p := range parts[1:] {
rows := strings.Split(p, "\n")
s := strings.TrimSuffix(strings.Fields(rows[0])[2], ":")
rules[s] = map[bool]rule{false: getRule(rows[1:]), true: getRule(rows[5:])}
}

return state, steps, rules
}

func getRule(f []string) rule {
v := strings.HasSuffix(f[1], "1.")
p := -1
if strings.HasSuffix(f[2], "right.") {
p = 1
}
nf := strings.Fields(f[3])
n := strings.TrimSuffix(nf[len(nf)-1], ".")
return rule{v, p, n}
}

0 comments on commit 181584e

Please sign in to comment.