diff options
| -rw-r--r-- | day14/.vscode/launch.json | 43 | ||||
| -rw-r--r-- | day14/Cargo.lock | 4 | ||||
| -rw-r--r-- | day14/Cargo.toml | 6 | ||||
| -rw-r--r-- | day14/src/main.rs | 85 |
4 files changed, 138 insertions, 0 deletions
diff --git a/day14/.vscode/launch.json b/day14/.vscode/launch.json new file mode 100644 index 0000000..8385d69 --- /dev/null +++ b/day14/.vscode/launch.json @@ -0,0 +1,43 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'day14'", + "cargo": { + "args": [ + "build", + "--bin=day14", + "--package=day14" + ], + "filter": { + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'day14'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=day14", + "--package=day14" + ], + "filter": { + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +}
\ No newline at end of file diff --git a/day14/Cargo.lock b/day14/Cargo.lock new file mode 100644 index 0000000..90e7534 --- /dev/null +++ b/day14/Cargo.lock @@ -0,0 +1,4 @@ +[[package]] +name = "day14" +version = "0.1.0" + diff --git a/day14/Cargo.toml b/day14/Cargo.toml new file mode 100644 index 0000000..8f9175f --- /dev/null +++ b/day14/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day14" +version = "0.1.0" +authors = ["Jan Tuomi <jan.tuomi@eficode.com>"] + +[dependencies] diff --git a/day14/src/main.rs b/day14/src/main.rs new file mode 100644 index 0000000..72ec43f --- /dev/null +++ b/day14/src/main.rs @@ -0,0 +1,85 @@ +static INPUT: usize = 01245; +static INPUT_STR: &str = "293801"; + +fn visualize(scoreboard: &Vec<u32>, i1: usize, i2: usize) { + for (i, val) in scoreboard.iter().enumerate() { + match i { + i if i == i1 => print!("({})", val), + i if i == i2 => print!("[{}]", val), + _ => print!(" {} ", val) + } + } + print!("\n"); +} + +fn exercise1() { + let mut scoreboard: Vec<u32> = vec![3, 7]; + let mut i1: usize = 0; + let mut i2: usize = 1; + + for iter in 0..INPUT + 10 { + // visualize(&scoreboard, i1, i2); + let new_sum = scoreboard[i1] + scoreboard[i2]; + let string = new_sum.to_string(); + for ch in string.chars() { + let s = ch.to_string(); + let score = s.parse::<u32>().unwrap(); + scoreboard.push(score); + } + + let move1 = (scoreboard[i1] + 1) as usize; + let move2 = (scoreboard[i2] + 1) as usize; + + let n = scoreboard.len(); + i1 = (i1 + move1) % n; + i2 = (i2 + move2) % n; + } + + print!("Result: "); + for vi in INPUT..INPUT + 10 { + let v = scoreboard[vi]; + print!("{}", v); + } + print!("\n"); +} + +fn exercise2() { + let mut scoreboard: Vec<u32> = vec![3, 7]; + let mut i1: usize = 0; + let mut i2: usize = 1; + + let input_len = INPUT_STR.len(); + let ITER_MAX = 100000000; + for _ in 0..ITER_MAX { + // visualize(&scoreboard, i1, i2); + let new_sum = scoreboard[i1] + scoreboard[i2]; + let string = new_sum.to_string(); + for ch in string.chars() { + let s = ch.to_string(); + let score = s.parse::<u32>().unwrap(); + scoreboard.push(score); + + let n = scoreboard.len(); + if n >= input_len { + let slice: String = scoreboard[n - input_len..].iter().map(|&v| v.to_string()).collect(); + if slice == INPUT_STR { + println!("Result 2: {}", n - input_len); + return; + } + } + } + + let move1 = (scoreboard[i1] + 1) as usize; + let move2 = (scoreboard[i2] + 1) as usize; + + let n = scoreboard.len(); + i1 = (i1 + move1) % n; + i2 = (i2 + move2) % n; + } + println!("Iteration max"); +} + +fn main() { + // exercise1(); + exercise2(); +} |
