-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
41 lines (35 loc) · 775 Bytes
/
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
package main
import (
"fmt"
"github.com/devkvlt/aoc"
)
func main() {
guide := aoc.Lines("input")
// Part 1
var total1 int
for _, hint := range guide {
theirs := int(hint[0] - 'A' + 1)
mine := int(hint[2] - 'X' + 1)
total1 += score(theirs, mine)
}
fmt.Println(total1)
// Part 2
var total2 int
for _, hint := range guide {
theirs := int(hint[0] - 'A' + 1)
outcome := int(hint[2]) - int('Y')
mine := (theirs + outcome) % 3
if mine == 0 {
mine = 3
}
total2 += score(theirs, mine)
}
fmt.Println(total2)
}
func score(theirs, mine int) int {
x := (theirs - mine + 3) % 3 // x==2 => win, x==1 => loss, x==0 => draw
f := func(x int) int {
return (9*x*x - 15*x + 6) / 2 // f(2)=6, f(1)=0, f(0)=3, god bless Lagrange
}
return mine + f(x)
}