mirror of
https://github.com/wezm/advent-of-code.git
synced 2024-12-18 10:19:55 +00:00
Add day 5 solution
This commit is contained in:
parent
d48764bac4
commit
33b55c4a08
5 changed files with 1153 additions and 0 deletions
4
2017/day/5/Cargo.lock
generated
Normal file
4
2017/day/5/Cargo.lock
generated
Normal file
|
@ -0,0 +1,4 @@
|
|||
[[package]]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
|
6
2017/day/5/Cargo.toml
Normal file
6
2017/day/5/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
authors = ["Wesley Moore <wes@wezm.net>"]
|
||||
|
||||
[dependencies]
|
1070
2017/day/5/input
Normal file
1070
2017/day/5/input
Normal file
File diff suppressed because it is too large
Load diff
29
2017/day/5/problem.txt
Normal file
29
2017/day/5/problem.txt
Normal file
|
@ -0,0 +1,29 @@
|
|||
--- 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?
|
||||
|
44
2017/day/5/src/main.rs
Normal file
44
2017/day/5/src/main.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use std::str::FromStr;
|
||||
|
||||
fn main() {
|
||||
let file = File::open("input").expect("unable to open input file");
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
let offsets = reader.lines()
|
||||
.map(|digit| i32::from_str(&digit.unwrap()).unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let steps = steps_to_exit(&offsets);
|
||||
println!("{}", steps);
|
||||
}
|
||||
|
||||
fn steps_to_exit(jump_offsets: &[i32]) -> i32 {
|
||||
let mut offsets = jump_offsets.to_vec();
|
||||
let mut pc = 0i32;
|
||||
let mut steps = 0;
|
||||
|
||||
loop {
|
||||
if let Some(offset) = offsets.get_mut(pc as usize) {
|
||||
pc += *offset;
|
||||
*offset += 1;
|
||||
steps += 1;
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
|
||||
if pc < 0 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
steps
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_example() {
|
||||
let jumps = vec![0, 3, 0, 1, -3];
|
||||
assert_eq!(steps_to_exit(&jumps), 5);
|
||||
}
|
Loading…
Reference in a new issue