-
-
Save samueltardieu/306737b951a4686443f6627d3137eadf to your computer and use it in GitHub Desktop.
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
#[aoc_generator(day23)] | |
fn input_generator(input: &str) -> Vec<usize> { | |
input | |
.as_bytes() | |
.into_iter() | |
.map(|&c| (c - b'0') as usize) | |
.collect() | |
} | |
#[aoc(day23, part1)] | |
fn part1(data: &[usize]) -> usize { | |
let next = game(data, 100, data.len()); | |
let (mut i, mut res) = (1, 0); | |
for _ in 1..data.len() { | |
i = next[i]; | |
res = res * 10 + i; | |
} | |
res | |
} | |
#[aoc(day23, part2)] | |
fn part2(data: &[usize]) -> usize { | |
let next = game(data, 10_000_000, 1_000_000); | |
next[1] * next[next[1]] | |
} | |
fn game(data: &[usize], rounds: usize, end: usize) -> Vec<usize> { | |
let mut next = (0..=end).map(|i| i + 1).collect::<Vec<_>>(); | |
next[end] = data[0]; | |
for i in 0..(data.len() - 1) { | |
next[data[i]] = data[i + 1]; | |
} | |
next[data[data.len() - 1]] = if data.len() < end { | |
data.len() + 1 | |
} else { | |
data[0] | |
}; | |
let mut sel = data[0]; | |
for _ in 0..rounds { | |
let picked: Vec<usize> = vec![next[sel], next[next[sel]], next[next[next[sel]]]]; | |
next[sel] = next[picked[2]]; | |
let mut ins = sel; | |
loop { | |
ins = ((ins + end - 2) % end) + 1; | |
if !picked.contains(&ins) { | |
break; | |
} | |
} | |
next[picked[2]] = next[ins]; | |
next[ins] = picked[0]; | |
sel = next[sel]; | |
} | |
next | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment