diff options
author | mathiasmagnusson <mathias@magnusson.space> | 2022-12-12 22:33:07 +0100 |
---|---|---|
committer | mathiasmagnusson <mathias@magnusson.space> | 2022-12-12 22:33:07 +0100 |
commit | d4fcb8a4a815ce8c888c3e06330e9cff71e3c312 (patch) | |
tree | 7d1b7f7a68c685804ae43293002d9ef67bff834b /aoc22/day10/src | |
parent | 5dd4e8d1b053e9c13c66a573e664c0a7808e4cb6 (diff) | |
download | programming-problem-solving-d4fcb8a4a815ce8c888c3e06330e9cff71e3c312.tar.gz |
Day 10
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>()); + } +} |