summaryrefslogtreecommitdiff
path: root/day5.go
blob: 706414b420fe5096e11a731fc19ba395a76f4b1e (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"regexp"
	"strconv"
)

type crates struct {
	stacks [][]byte
}

func (c *crates) topCrates() string {
	b := make([]byte, len(c.stacks))
	for i, stack := range c.stacks {
		if len(stack) > 0 {
			b[i] = stack[len(stack)-1]
		}
	}
	return string(b)
}

func (c *crates) move(m move) {
	// copy slices
	from := c.stacks[m.from-1]
	to := c.stacks[m.to-1]

	for i := 0; i < m.amount; i++ {
		pop, popped := from[len(from)-1], from[:len(from)-1]
		from = popped
		to = append(to, pop)
	}

	// set slices
	c.stacks[m.to-1] = to
	c.stacks[m.from-1] = from
}

func (c *crates) moveMultiple(m move) {
	// copy slices
	from := c.stacks[m.from-1]
	to := c.stacks[m.to-1]

	pop, from := from[len(from)-m.amount:], from[:len(from)-m.amount]

	// push
	c.stacks[m.to-1] = append(to, pop...)
	// set popped
	c.stacks[m.from-1] = from
}

func (c *crates) parseLine(l []byte) {
	// by four
	for i := 0; i < len(l); i += 4 {
		if 'A' <= l[i+1] && l[i+1] <= 'Z' {
			// push front: https://github.com/golang/go/wiki/SliceTricks#push-frontunshift
			c.stacks[i/4] = append([]byte{l[i+1]}, c.stacks[i/4]...)
		}
	}
}

func newCargo(s *bufio.Scanner) *crates {
	// build cargo
	s.Scan()
	line := s.Bytes()
	//[x].
	//1234
	cargo := &crates{stacks: make([][]byte, len(line)/4+1)}
	cargo.parseLine(line)

	for s.Scan() {
		line = s.Bytes()
		if len(line) == 0 {
			break
		}
		cargo.parseLine(line)
	}
	return cargo
}

type move struct {
	amount int
	from   int
	to     int
}

func parseMove(l string) move {
	regex := regexp.MustCompile(`move (?P<amount>\d+) from (?P<from>\d+) to (?P<to>\d+)`)
	matches := regex.FindStringSubmatch(l)
	m := make([]int, 3)
	for i, match := range matches[1:] {
		matchInt, err := strconv.Atoi(match)
		m[i] = matchInt
		if err != nil {
			log.Fatal("Got unexpected input in move")
		}
	}
	return move{
		amount: m[0],
		from:   m[1],
		to:     m[2],
	}
}

func main() {
	f, err := os.Open("day5.txt")
	if err != nil {
		log.Fatal("Could not open input file")
	}

	// part 1
	scanner := bufio.NewScanner(f)
	cargo := newCargo(scanner)
	for scanner.Scan() {
		l := scanner.Text()
		m := parseMove(l)
		cargo.move(m)
	}
	fmt.Println("Part 1:", cargo.topCrates())

	f.Seek(0, 0)

	// part 2
	scanner = bufio.NewScanner(f)
	cargo = newCargo(scanner)
	for scanner.Scan() {
		l := scanner.Text()
		m := parseMove(l)
		cargo.moveMultiple(m)
	}
	fmt.Println("Part 2:", cargo.topCrates())
}