diff --git a/README.md b/README.md index 46caa3a..af6957c 100644 --- a/README.md +++ b/README.md @@ -7,9 +7,11 @@ Just one thing: **Don't just copy them.** Take the puzzle yourself and afterward ## Finished puzzles - Medium + - [Bender - Episode 1](https://www.codingame.com/training/medium/bender-episode-1) - [Don't Panic - Episode 1](https://www.codingame.com/training/medium/don't-panic-episode-1) - [Shadows of the Knight - Episode 1](https://www.codingame.com/training/medium/shadows-of-the-knight-episode-1) - - [Bender - Episode 1](https://www.codingame.com/training/medium/bender-episode-1) - [There is no Spoon - Episode 1](https://www.codingame.com/training/medium/there-is-no-spoon-episode-1) +- Hard + - [Roller Coaster](https://www.codingame.com/training/hard/roller-coaster) - Community - [Texas Holdem](https://www.codingame.com/training/community/texas-holdem) by AdamHill diff --git a/finished/hard_roller-coaster.go b/finished/hard_roller-coaster.go new file mode 100644 index 0000000..9901a99 --- /dev/null +++ b/finished/hard_roller-coaster.go @@ -0,0 +1,76 @@ +package main + +import ( + "bufio" + "fmt" + "log" + "os" + "time" +) + +type calculationResult struct{ Seated, Next int } + +var ( + groups [1000]int + calculationCache = map[int]calculationResult{} +) + +func main() { + defer timeTrack(time.Now(), "main") + scanner := bufio.NewScanner(os.Stdin) + scanner.Buffer(make([]byte, 1000000), 1000000) + scanner.Split(bufio.ScanWords) + + var L, C, N, i int + scanner.Scan() + L = toInt(scanner.Bytes()) + scanner.Scan() + C = toInt(scanner.Bytes()) + scanner.Scan() + N = toInt(scanner.Bytes()) + + log.Printf("C: %d", C) + + for ; i < N; i++ { + scanner.Scan() + groups[i] = toInt(scanner.Bytes()) + } + + var earned, next, seated, j int + for i = 0; i < C; i++ { + key := next + seated = 0 + + if cr, ok := calculationCache[key]; ok { + seated = cr.Seated + next = cr.Next + } else { + j = 0 + for j < N && groups[next] <= L-seated { + seated += groups[next] + j++ + next++ + if next == N { + next = 0 + } + } + calculationCache[key] = calculationResult{Seated: seated, Next: next} + } + + earned += seated + } + + fmt.Printf("%d\n", earned) +} + +func toInt(buf []byte) (n int) { + for _, v := range buf { + n = n*10 + int(v-'0') + } + return +} + +func timeTrack(start time.Time, name string) { + elapsed := time.Since(start) + log.Printf("%s took %s", name, elapsed) +}