DAY 15 - GoLang을 통한 코드 2020 출현
8226 단어 goadventofcode
![](https://s1.md5.ltd/image/bccb60af7202bb3a6bd3ef31140e4087.gif)
그래서 어제 Advent Of Code를 마무리해야겠다고 생각했는데 오늘의 문제는 그냥 클릭했습니다. 나는 그것이 지난 며칠 후에 더 쉬울 예정이라고 가정하고 있습니다.
2부에서 정확히 0줄의 변경이 필요했다는 것이 이상했습니다. 마지막 턴을 변경하기만 하면 됩니다. 시간이 조금 더 걸렸습니다. 내 시간은 약 7.6초
package days
import (
"fmt"
inputs "../inputs"
)
// https://adventofcode.com/2020/day/15
// Fifteen : advent of code, day Fifteen part1 and 2
func Fifteen() {
inputSlice := inputs.Day15
// k: number spoken | v: {num of times spoken, last time spoken}
spoken := make(map[int][]int)
for i, num := range inputSlice {
spoken[num] = []int{1, i, i}
}
fmt.Print("(Part 1) - 2020th number spoken: ")
fmt.Println(getLastNumberSpoken(spoken, 2020))
fmt.Print("(Part 1) - 2020th number spoken: ")
fmt.Println(getLastNumberSpoken(spoken, 30000000))
}
func getLastNumberSpoken(spoken map[int][]int, last int) int {
nextNum := 0
var lastNum int
var lastTurn int
for _, v := range spoken {
if v[1] > lastTurn {
lastTurn = v[1]
lastNum = v[0]
}
}
for i := len(spoken); i < last; i++ {
if _, ok := spoken[lastNum]; ok {
if spoken[lastNum][0] == 1 {
nextNum = 0
} else {
nextNum = spoken[lastNum][1] - spoken[lastNum][2]
}
}
if _, ok := spoken[nextNum]; ok {
spoken[nextNum][0]++
spoken[nextNum][2] = spoken[nextNum][1]
spoken[nextNum][1] = i
lastNum = nextNum
} else {
spoken[nextNum] = []int{1, i, i}
lastNum = nextNum
}
}
return nextNum
}
Link to Github source file
Reference
이 문제에 관하여(DAY 15 - GoLang을 통한 코드 2020 출현), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/dizveloper/day-15-advent-of-code-2020-w-golang-jid텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)