From 5d7ee2e5028cb283735b6788fa046843ed1f0821 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 10 Dec 2021 21:41:37 +0100 Subject: Added solutions for 10th day : syntax scoring --- 2021/10/first.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 2021/10/first.go (limited to '2021/10/first.go') diff --git a/2021/10/first.go b/2021/10/first.go new file mode 100644 index 0000000..0c96fe8 --- /dev/null +++ b/2021/10/first.go @@ -0,0 +1,66 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +type stack []byte + +func (s *stack) push(b byte) { + *s = append(*s, b) +} + +func (s *stack) pop() *byte { + l := len(*s) + if l == 0 { + return nil + } else { + elt := (*s)[l-1] + *s = (*s)[:l-1] + return &elt + } +} + +func main() { + score := 0 + s := make(stack, 0) + + scanner := bufio.NewScanner(os.Stdin) +out: + for scanner.Scan() { + line := scanner.Text() + for i := 0; i < len(line); i++ { + c := line[i] + if c == '(' || c == '[' || c == '{' || c == '<' { + s.push(c) + continue + } + b := s.pop() + switch c { + case ')': + if *b != '(' { + score += 3 + continue out + } + case ']': + if *b != '[' { + score += 57 + continue out + } + case '}': + if *b != '{' { + score += 1197 + continue out + } + case '>': + if *b != '<' { + score += 25137 + continue out + } + } + } + } + fmt.Println(score) +} -- cgit v1.2.3