diff options
Diffstat (limited to 'aoc22/day10/src')
-rw-r--r-- | aoc22/day10/src/main.rs | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/aoc22/day10/src/main.rs b/aoc22/day10/src/main.rs new file mode 100644 index 0000000..42d6490 --- /dev/null +++ b/aoc22/day10/src/main.rs @@ -0,0 +1,62 @@ +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::<i32>().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::<i32>().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::<String>()); + } +} |