2017 Day 05 Complete!
This commit is contained in:
parent
1a26dee628
commit
1780618d7c
64
2017/day05/day05.go
Normal file
64
2017/day05/day05.go
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
inp := StdinToInts()
|
||||||
|
inp2 := make([]int, len(inp))
|
||||||
|
copy(inp2, inp)
|
||||||
|
|
||||||
|
part1(inp)
|
||||||
|
part2(inp2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1(inp []int) {
|
||||||
|
fmt.Println("= Part 1 =")
|
||||||
|
idx, steps := 0, 0
|
||||||
|
for idx >= 0 && idx < len(inp) {
|
||||||
|
jmp := inp[idx]
|
||||||
|
inp[idx] = inp[idx] + 1
|
||||||
|
idx = idx + jmp
|
||||||
|
steps++
|
||||||
|
}
|
||||||
|
fmt.Println("Total Steps:", steps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2(inp []int) {
|
||||||
|
fmt.Println("= Part 2 =")
|
||||||
|
idx, steps := 0, 0
|
||||||
|
for idx >= 0 && idx < len(inp) {
|
||||||
|
jmp := inp[idx]
|
||||||
|
if jmp >= 3 {
|
||||||
|
inp[idx] = inp[idx] - 1
|
||||||
|
} else {
|
||||||
|
inp[idx] = inp[idx] + 1
|
||||||
|
}
|
||||||
|
idx = idx + jmp
|
||||||
|
steps++
|
||||||
|
}
|
||||||
|
fmt.Println("Total Steps:", steps)
|
||||||
|
}
|
||||||
|
|
||||||
|
func StdinToInts() []int {
|
||||||
|
var input []int
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
input = append(input, Atoi(scanner.Text()))
|
||||||
|
}
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
func Atoi(i string) int {
|
||||||
|
var ret int
|
||||||
|
var err error
|
||||||
|
if ret, err = strconv.Atoi(i); err != nil {
|
||||||
|
log.Fatal("Invalid Atoi")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
1043
2017/day05/input
Normal file
1043
2017/day05/input
Normal file
File diff suppressed because it is too large
Load Diff
77
2017/day05/problem
Normal file
77
2017/day05/problem
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- Day 5: A Maze of Twisty Trampolines, All Alike ---
|
||||||
|
|
||||||
|
An urgent interrupt arrives from the CPU: it's trapped in a maze of jump instructions, and it would
|
||||||
|
like assistance from any programs with spare cycles to help find the exit.
|
||||||
|
|
||||||
|
The message includes a list of the offsets for each jump. Jumps are relative: -1 moves to the
|
||||||
|
previous instruction, and 2 skips the next one. Start at the first instruction in the list. The goal
|
||||||
|
is to follow the jumps until one leads outside the list.
|
||||||
|
|
||||||
|
In addition, these instructions are a little strange; after each jump, the offset of that
|
||||||
|
instruction increases by 1. So, if you come across an offset of 3, you would move three instructions
|
||||||
|
forward, but change it to a 4 for the next time it is encountered.
|
||||||
|
|
||||||
|
For example, consider the following list of jump offsets:
|
||||||
|
|
||||||
|
0
|
||||||
|
3
|
||||||
|
0
|
||||||
|
1
|
||||||
|
-3
|
||||||
|
|
||||||
|
Positive jumps ("forward") move downward; negative jumps move upward. For legibility in this
|
||||||
|
example, these offset values will be written all on one line, with the current instruction marked in
|
||||||
|
parentheses. The following steps would be taken before an exit is found:
|
||||||
|
|
||||||
|
* (0) 3 0 1 -3 - before we have taken any steps.
|
||||||
|
* (1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all). Fortunately, the instruction
|
||||||
|
is then incremented to 1.
|
||||||
|
* 2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first
|
||||||
|
instruction is incremented again, now to 2.
|
||||||
|
* 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.
|
||||||
|
* 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.
|
||||||
|
* 2 5 0 1 -2 - jump 4 steps forward, escaping the maze.
|
||||||
|
|
||||||
|
In this example, the exit is reached in 5 steps.
|
||||||
|
|
||||||
|
How many steps does it take to reach the exit?
|
||||||
|
|
||||||
|
Your puzzle answer was _______.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Now, the jumps are even stranger: after each jump, if the offset was three or more, instead decrease
|
||||||
|
it by 1. Otherwise, increase it by 1 as before.
|
||||||
|
|
||||||
|
Using this rule with the above example, the process now takes 10 steps, and the offset values after
|
||||||
|
finding the exit are left as 2 3 2 3 -1.
|
||||||
|
|
||||||
|
How many steps does it now take to reach the exit?
|
||||||
|
|
||||||
|
Your puzzle answer was _________.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
At this point, you should return to your advent calendar and try another puzzle.
|
||||||
|
|
||||||
|
If you still want to see it, you can get your puzzle input.
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. http://adventofcode.com/
|
||||||
|
. http://adventofcode.com/2017/about
|
||||||
|
. http://adventofcode.com/2017/support
|
||||||
|
. http://adventofcode.com/2017/events
|
||||||
|
. http://adventofcode.com/2017/settings
|
||||||
|
. http://adventofcode.com/2017/auth/logout
|
||||||
|
. http://adventofcode.com/2017
|
||||||
|
. http://adventofcode.com/2017
|
||||||
|
. http://adventofcode.com/2017/leaderboard
|
||||||
|
. http://adventofcode.com/2017/stats
|
||||||
|
. http://adventofcode.com/2017/sponsors
|
||||||
|
. http://adventofcode.com/2017/sponsors
|
||||||
|
. http://adventofcode.com/2017
|
||||||
|
. http://adventofcode.com/2017/day/5/input
|
5
2017/day05/testinput
Normal file
5
2017/day05/testinput
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
0
|
||||||
|
3
|
||||||
|
0
|
||||||
|
1
|
||||||
|
-3
|
Loading…
Reference in New Issue
Block a user