mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 16, in groovy.
This commit is contained in:
@@ -14,6 +14,7 @@ The current plan, in no particular order:
|
|||||||
- [x] Clojure - [Day 03](./day-03/solution.clj)
|
- [x] Clojure - [Day 03](./day-03/solution.clj)
|
||||||
- [ ] Coffeescript
|
- [ ] Coffeescript
|
||||||
- [x] Go - [Day 13](./day-13/solution.go)
|
- [x] Go - [Day 13](./day-13/solution.go)
|
||||||
|
- [x] Groovy - [Day 16](./day-16/solution.groovy)
|
||||||
- [x] Haskell - [Day 15](./day-15/solution.hs)
|
- [x] Haskell - [Day 15](./day-15/solution.hs)
|
||||||
- [ ] Java
|
- [ ] Java
|
||||||
- [x] Kotlin - [Day 10](./day-10/solution.kt)
|
- [x] Kotlin - [Day 10](./day-10/solution.kt)
|
||||||
|
|||||||
1
2017/day-16/input.txt
Normal file
1
2017/day-16/input.txt
Normal file
File diff suppressed because one or more lines are too long
72
2017/day-16/solution.groovy
Normal file
72
2017/day-16/solution.groovy
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
void doRotate(List<String> list, String arg) {
|
||||||
|
def amount = Integer.valueOf(arg.substring(1))
|
||||||
|
|
||||||
|
Collections.rotate(list, amount)
|
||||||
|
}
|
||||||
|
|
||||||
|
void exchangePos(List<String> list, String arg) {
|
||||||
|
def parts = arg.substring(1).split("/")
|
||||||
|
|
||||||
|
list.swap(Integer.valueOf(parts[0]), Integer.valueOf(parts[1]))
|
||||||
|
}
|
||||||
|
|
||||||
|
void exchangeProg(List<String> list, String arg) {
|
||||||
|
def p1 = list.indexOf(arg.substring(1, 2));
|
||||||
|
def p2 = list.indexOf(arg.substring(3, 4));
|
||||||
|
|
||||||
|
list.swap(p1, p2);
|
||||||
|
}
|
||||||
|
|
||||||
|
def input = System.in.newReader().readLine()
|
||||||
|
|
||||||
|
def moves = input.split(",")
|
||||||
|
|
||||||
|
def state = new LinkedList(Arrays.asList("abcdefghijklmnop".split("")))
|
||||||
|
|
||||||
|
String dance(List<String> state, String[] moves) {
|
||||||
|
for (move in moves) {
|
||||||
|
switch (move.charAt(0)) {
|
||||||
|
case 's':
|
||||||
|
doRotate(state, move)
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'x':
|
||||||
|
exchangePos(state, move)
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'p':
|
||||||
|
exchangeProg(state, move)
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
println("Unknown type!")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return state.join("")
|
||||||
|
}
|
||||||
|
|
||||||
|
String danceOften(String[] moves, Integer amount) {
|
||||||
|
def state = new LinkedList(Arrays.asList("abcdefghijklmnop".split("")))
|
||||||
|
|
||||||
|
def seen = new HashSet<String>()
|
||||||
|
def order = new ArrayList<String>()
|
||||||
|
|
||||||
|
def summary = state.join("");
|
||||||
|
|
||||||
|
for (i in 0..<amount) {
|
||||||
|
if (seen.contains(summary)) {
|
||||||
|
return order.get(amount % i)
|
||||||
|
}
|
||||||
|
|
||||||
|
seen.add(summary)
|
||||||
|
order.add(summary)
|
||||||
|
summary = dance(state, moves)
|
||||||
|
}
|
||||||
|
|
||||||
|
return summary;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
println dance(state, moves)
|
||||||
|
println danceOften(moves, 1000000000)
|
||||||
Reference in New Issue
Block a user