/
day03.go
61 lines (55 loc) · 1.59 KB
/
day03.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
package aoc2015
import (
"image"
"strconv"
)
// iterateCoordinate moves the location using an input (`v`, `^`, `<`, `>`)
func iterateCoordinate(location *image.Point, input rune) {
switch input {
case 'v':
location.Y--
case '^':
location.Y++
case '<':
location.X--
case '>':
location.X++
}
}
// Day03 solves the third day puzzle "Perfectly Spherical Houses in a Vacuum".
//
// Input
//
// A single line of length 8192 containing only the characters '<', '>', '^', and 'v',
// each representing direction. For example:
//
// v^>>v>v>>>>^^>v<^v^>><<^<>>v><^><<^>^<vv^^<><<>><vvvv^>^^<><^^
//
// Any characters that are not any of those will be ignored.
func Day03(input string) (answer1, answer2 string, err error) {
// okay how do we "store" these? maybe hash map
// part 1
var santa image.Point // santa's location (part one)
houses := make(map[image.Point]int) // houses[coord] = no. of times we've passed by a point
houses[santa]++
// part 2
var nuSanta, roboSanta image.Point // nuSanta and roboSanta's locations
dualHouses := make(map[image.Point]int) // dualHouses is houses but for part 2
dualHouses[nuSanta]++
isSanta := true // is it nuSanta's turn? will invert for every iteration
for _, readDirection := range input {
iterateCoordinate(&santa, readDirection)
houses[santa]++
if isSanta {
iterateCoordinate(&nuSanta, readDirection)
dualHouses[nuSanta]++
} else {
iterateCoordinate(&roboSanta, readDirection)
dualHouses[roboSanta]++
}
isSanta = !isSanta
}
answer1 = strconv.Itoa(len(houses))
answer2 = strconv.Itoa(len(dualHouses))
return
}