From 8f59911c20fca436d4fcb82a135755b5b5441533 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sun, 17 Dec 2017 17:04:41 +0100 Subject: [PATCH] Implement day 17, in swift. --- 2017/README.md | 1 + 2017/day-17/.gitignore | 1 + 2017/day-17/solution.swift | 25 +++++++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 2017/day-17/.gitignore create mode 100644 2017/day-17/solution.swift diff --git a/2017/README.md b/2017/README.md index d81b0db..dc0209c 100644 --- a/2017/README.md +++ b/2017/README.md @@ -31,5 +31,6 @@ The current plan, in no particular order: - [x] Scala - [Day 11](./day-11/solution.scala) - [ ] Scheme - [ ] SQL +- [ ] Swift - [Day 17](./day-17/solution.swift) … and then I will need some more. But that will come in time. diff --git a/2017/day-17/.gitignore b/2017/day-17/.gitignore new file mode 100644 index 0000000..a4bd26d --- /dev/null +++ b/2017/day-17/.gitignore @@ -0,0 +1 @@ +solution diff --git a/2017/day-17/solution.swift b/2017/day-17/solution.swift new file mode 100644 index 0000000..434a58f --- /dev/null +++ b/2017/day-17/solution.swift @@ -0,0 +1,25 @@ +let input = Int(readLine()!)! + +var index = 0 + +var buffer = [0]; + +for i in 1...2017 { + index = ((index + input) % buffer.count) + 1 + buffer.insert(i, at: index) +} + +print(buffer[buffer.index(of: 2017)! + 1]) + +var last = 0 + +index = 0 + +for i in 1...50000000 { + index = ((index + input) % i) + 1 + if index == 1 { + last = i + } +} + +print(last)