diff options
Diffstat (limited to 'aoc22/day5/src')
-rw-r--r-- | aoc22/day5/src/main.rs | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/aoc22/day5/src/main.rs b/aoc22/day5/src/main.rs new file mode 100644 index 0000000..714e67c --- /dev/null +++ b/aoc22/day5/src/main.rs @@ -0,0 +1,107 @@ +fn main() { + part1(); + part2(); +} + +fn part1() { + let input = include_str!("../input"); + + let mut split = input.split("\n\n"); + let start_state = split.next().unwrap(); + let mut state: Vec<Vec<char>> = vec![vec![]; 9]; + for line in start_state.lines() { + let mut index = 1; + let mut i = 0; + while index < line.len() { + let c = line.chars().nth(index).unwrap(); + if c.is_ascii_uppercase() { + state[i].push(c); + } + index += 4; + i += 1; + } + } + for s in state.iter_mut() { + s.reverse(); + } + println!("{:?}", state); + let instructions: Vec<Vec<usize>> = split + .next() + .unwrap() + .lines() + .map(|line| { + line.split(" ") + .flat_map(|w| w.parse::<usize>()) + .collect::<Vec<usize>>() + }) + .collect(); + + for instr in instructions { + let amt = instr[0]; + let src = instr[1]; + let dst = instr[2]; + + for _ in 0..amt { + let c = state[src - 1].pop().unwrap(); + state[dst - 1].push(c); + } + } + + for s in state { + print!("{}", s.last().unwrap()); + } + println!(); +} + +fn part2() { + let input = include_str!("../input"); + + let mut split = input.split("\n\n"); + let start_state = split.next().unwrap(); + let mut state: Vec<Vec<char>> = vec![vec![]; 9]; + for line in start_state.lines() { + let mut index = 1; + let mut i = 0; + while index < line.len() { + let c = line.chars().nth(index).unwrap(); + if c.is_ascii_uppercase() { + state[i].push(c); + } + index += 4; + i += 1; + } + } + for s in state.iter_mut() { + s.reverse(); + } + let instructions: Vec<Vec<usize>> = split + .next() + .unwrap() + .lines() + .map(|line| { + line.split(" ") + .flat_map(|w| w.parse::<usize>()) + .collect::<Vec<usize>>() + }) + .collect(); + + for instr in instructions { + let amt = instr[0]; + let src = instr[1]; + let dst = instr[2]; + + let mut stack = vec![]; + for _ in 0..amt { + let c = state[src - 1].pop().unwrap(); + stack.push(c); + } + for _ in 0..amt { + state[dst - 1].push(stack.pop().unwrap()); + } + } + + for s in state { + print!("{}", s.last().unwrap()); + } + println!(); +} |