fn main() { let input = lib::read_input(10); part1(&input); part2(&input); } fn part1(input: &str) { let addx = "addx "; let mut x = 1; let mut cycle = 1; let mut ans = 0; for line in input.lines() { if line.starts_with(addx) { cycle += 1; if [20, 60, 100, 140, 180, 220].contains(&cycle) { ans += cycle * x; } x += line[addx.len()..].parse::().unwrap(); cycle += 1; if [20, 60, 100, 140, 180, 220].contains(&cycle) { ans += cycle * x; } } else { cycle += 1; if [20, 60, 100, 140, 180, 220].contains(&cycle) { ans += cycle * x; } } } println!("{}", ans); } fn part2(input: &str) { let addx = "addx "; let mut x = 1; let mut cycle = 1i32; let mut ans = ['.'; 40 * 6]; for line in input.lines() { if line.starts_with(addx) { if [x - 1, x, x + 1].contains(&(cycle % 40)) { ans[cycle as usize - 1] = '#'; } cycle += 1; x += line[addx.len()..].parse::().unwrap(); if [x - 1, x, x + 1].contains(&(cycle % 40)) { ans[cycle as usize - 1] = '#'; } cycle += 1; } else { if [x - 1, x, x + 1].contains(&(cycle % 40)) { ans[cycle as usize - 1] = '#'; } cycle += 1; } } for i in 0..6 { println!("{}", &ans[i * 40..(i + 1) * 40].iter().collect::()); } }