-
-
Save solson/87e62122095726c4696a55a5ec1611ff to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug)] | |
struct Layer { | |
depth: usize, | |
range: usize, | |
} | |
const _SAMPLE_INPUT: &str = "\ | |
0: 3 | |
1: 2 | |
4: 4 | |
6: 4\ | |
"; | |
const INPUT: &str = "\ | |
0: 5 | |
1: 2 | |
2: 3 | |
4: 4 | |
6: 6 | |
8: 4 | |
10: 6 | |
12: 10 | |
14: 6 | |
16: 8 | |
18: 6 | |
20: 9 | |
22: 8 | |
24: 8 | |
26: 8 | |
28: 12 | |
30: 12 | |
32: 8 | |
34: 8 | |
36: 12 | |
38: 14 | |
40: 12 | |
42: 10 | |
44: 14 | |
46: 12 | |
48: 12 | |
50: 24 | |
52: 14 | |
54: 12 | |
56: 12 | |
58: 14 | |
60: 12 | |
62: 14 | |
64: 12 | |
66: 14 | |
68: 14 | |
72: 14 | |
74: 14 | |
80: 14 | |
82: 14 | |
86: 14 | |
90: 18 | |
92: 17\ | |
"; | |
// (n-1)*2 | |
// for 3 | |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
// 0 1 2 1 0 1 2 1 0 1 2 1 0 1 2 1 | |
// for 4 | |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
// 0 1 2 3 2 1 0 1 2 3 2 1 0 1 2 3 | |
// for 5 | |
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
// 0 1 2 3 4 3 2 1 0 1 2 3 4 3 2 1 | |
fn position_at_step(step: usize, range: usize) -> usize { | |
let mut position = step % range; | |
// If the scanner is going in the reverse direction, correct for this by | |
// reversing the mod. | |
if (step / range) % 2 != 0 { | |
position = range - position - 1; | |
} | |
position | |
} | |
fn main() { | |
let layers: Vec<Layer> = INPUT | |
.split('\n') | |
.map(|line| { | |
let mut iter = line.split(": ").map(|n| n.parse::<usize>().unwrap()); | |
let depth = iter.next().unwrap(); | |
let range = iter.next().unwrap(); | |
Layer { depth, range } | |
}) | |
.collect(); | |
let mut severity = 0; | |
for Layer { depth, range } in layers { | |
if depth % ((range - 1) * 2) == 0 { | |
println!("{} hits", depth); | |
severity += depth * range; | |
} | |
} | |
println!("{}", severity); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment