Finished day 10 of aoc in rust with heavy guidance
This commit is contained in:
112
advent_of_code/2022/10/src/main.rs
Normal file
112
advent_of_code/2022/10/src/main.rs
Normal file
@@ -0,0 +1,112 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use itertools::Itertools;
|
||||
use nom::{
|
||||
character::complete::{self, newline},
|
||||
branch::alt,
|
||||
multi::separated_list1,
|
||||
bytes::complete::tag,
|
||||
sequence::preceded,
|
||||
*,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Command {
|
||||
NoOp,
|
||||
AddX(i32),
|
||||
}
|
||||
use Command::*;
|
||||
|
||||
impl Command{
|
||||
pub fn cycles(&self) -> u32 {
|
||||
match &self {
|
||||
NoOp => 1,
|
||||
AddX(_) => 2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_input(input: &str) -> IResult<&str, Vec<Command>> {
|
||||
let (input, vecs) = separated_list1(
|
||||
newline,
|
||||
alt((
|
||||
tag("noop").map(|_| NoOp),
|
||||
preceded(tag("addx "), complete::i32)
|
||||
.map(|num| AddX(num))
|
||||
)),
|
||||
)(input)?;
|
||||
|
||||
Ok((input, vecs))
|
||||
}
|
||||
|
||||
fn part1(){
|
||||
let important_cycles = [20, 60, 100, 140, 180, 220];
|
||||
let mut scores: BTreeMap<u32, i32> = BTreeMap::new();
|
||||
let mut x = 1;
|
||||
let mut cycles = 0;
|
||||
|
||||
let input_data = include_str!("../input.txt");
|
||||
let (_, command_lines) = parse_input(input_data).unwrap();
|
||||
|
||||
// let result = fold(command_lines.iter(), (0, 1), |(cycles, x), command| match command {
|
||||
// NoOp => (cycles + 1, x),
|
||||
// AddX(num) => (cycles + 2, x + num),
|
||||
// });
|
||||
// println!("Result: {:?}", result);
|
||||
|
||||
for command in command_lines.iter() {
|
||||
if important_cycles.contains(&(cycles + 1)) {
|
||||
scores.insert(cycles + 1, (cycles as i32 + 1) * x);
|
||||
}
|
||||
|
||||
if important_cycles.contains(&(cycles + 2)) {
|
||||
scores.insert(cycles + 2, (cycles as i32 + 2) * x);
|
||||
}
|
||||
|
||||
cycles += command.cycles();
|
||||
match command {
|
||||
NoOp => {},
|
||||
AddX(num) => { x += num; },
|
||||
};
|
||||
}
|
||||
|
||||
let score = scores.iter().map(|(_key, value)| value).sum::<i32>();
|
||||
println!("Score: {:?}", score);
|
||||
}
|
||||
|
||||
fn part2() {
|
||||
let input_data = include_str!("../input.txt");
|
||||
let (_, command_lines) = parse_input(input_data).unwrap();
|
||||
|
||||
let mut x = 1;
|
||||
let mut cycles = 0;
|
||||
let mut pixels = "".to_string();
|
||||
|
||||
for command in command_lines.iter() {
|
||||
for cycle_add in 0..command.cycles() {
|
||||
let pixel_id = (cycles as i32 + cycle_add as i32) % 40;
|
||||
|
||||
if ((x-1)..=(x+1)).contains(&pixel_id) {
|
||||
pixels.push('#');
|
||||
} else {
|
||||
pixels.push('.');
|
||||
}
|
||||
}
|
||||
|
||||
cycles += command.cycles();
|
||||
match command {
|
||||
NoOp => {},
|
||||
AddX(num) => {
|
||||
x += num;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
let result = pixels.chars().chunks(40).into_iter().map(|chunk| chunk.collect::<String>()).join("\n");
|
||||
print!("{}", result);
|
||||
}
|
||||
|
||||
fn main(){
|
||||
part1();
|
||||
part2();
|
||||
}
|
||||
Reference in New Issue
Block a user