-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
77 lines (64 loc) · 1.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Package day05 - Solution for Advent of Code 2018/05
// Problem Link: http://adventofcode.com/2018/day/05
package day05
import (
_ "embed"
"fmt"
"github.com/code-shoily/aocgo/algo"
"math"
)
//go:embed input.txt
var input []byte
const casingOffset = 32 // 'a' - 'A'
// Run prints out the result of the solution.
func Run() {
fmt.Println(solve(input))
}
func solve(input []byte) (int, int) {
return solvePart1(input), solvePart2(input)
}
func solvePart1(polymer []byte) int {
return react(polymer)
}
func solvePart2(polymer []byte) int {
desiredPolymerLength := math.MaxInt
for i := 'A'; i <= 'Z'; i++ {
testPolymer := without(polymer, byte(i))
if polymerLength := react(testPolymer); polymerLength < desiredPolymerLength {
desiredPolymerLength = polymerLength
}
}
return desiredPolymerLength
}
func react(polymer []byte) int {
var reacted algo.Stack[byte]
for len(polymer) > 0 {
current := polymer[0]
if unit, err := reacted.Peek(); !err {
if willReact(unit, current) {
reacted.Pop()
} else {
reacted.Push(current)
}
} else {
reacted.Push(current)
}
polymer = polymer[1:]
}
return len(reacted)
}
func without(polymer []byte, unit byte) []byte {
newPolymer := make([]byte, 0, len(polymer))
for i := 0; i < len(polymer); i++ {
if current := polymer[i]; current != unit && current != unit+casingOffset {
newPolymer = append(newPolymer, current)
}
}
return newPolymer
}
func willReact(unit1 byte, unit2 byte) bool {
if unit1 > unit2 {
return unit1-unit2 == casingOffset
}
return unit2-unit1 == casingOffset
}