Add solution for 2022 day 10
This commit is contained in:
parent
ed18a04dae
commit
f36de1f4e9
4
2022/Cargo.lock
generated
4
2022/Cargo.lock
generated
@ -44,6 +44,10 @@ version = "0.1.0"
|
|||||||
name = "aoc-2022-09"
|
name = "aoc-2022-09"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "aoc-2022-10"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "either"
|
name = "either"
|
||||||
version = "1.8.0"
|
version = "1.8.0"
|
||||||
|
@ -9,6 +9,7 @@ members = [
|
|||||||
"./day-07",
|
"./day-07",
|
||||||
"./day-08",
|
"./day-08",
|
||||||
"./day-09",
|
"./day-09",
|
||||||
|
"./day-10",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
| 07 | ✓ | ✓ | [Rust] |
|
| 07 | ✓ | ✓ | [Rust] |
|
||||||
| 08 | ✓ | ✓ | [Rust] |
|
| 08 | ✓ | ✓ | [Rust] |
|
||||||
| 09 | ✓ | ✓ | [Rust] |
|
| 09 | ✓ | ✓ | [Rust] |
|
||||||
| 10 | | | |
|
| 10 | ✓ | ✓ | [Rust] |
|
||||||
| 11 | | | |
|
| 11 | | | |
|
||||||
| 12 | | | |
|
| 12 | | | |
|
||||||
| 13 | | | |
|
| 13 | | | |
|
||||||
|
12
2022/day-10/Cargo.toml
Normal file
12
2022/day-10/Cargo.toml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc-2022-10"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
|
||||||
|
[lib]
|
||||||
|
test = false
|
||||||
|
doctest = false
|
||||||
|
|
||||||
|
|
||||||
|
[dependencies]
|
5
2022/day-10/Justfile
Normal file
5
2022/day-10/Justfile
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
@part PART INPUT_FILE="inputs/puzzle.txt":
|
||||||
|
cargo --quiet run --bin part_{{PART}} -- {{INPUT_FILE}}
|
||||||
|
|
||||||
|
clean:
|
||||||
|
cargo clean
|
139
2022/day-10/inputs/puzzle.txt
Normal file
139
2022/day-10/inputs/puzzle.txt
Normal file
@ -0,0 +1,139 @@
|
|||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx -38
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 2
|
||||||
|
addx -5
|
||||||
|
addx 12
|
||||||
|
addx 2
|
||||||
|
addx 27
|
||||||
|
addx -40
|
||||||
|
addx 19
|
||||||
|
addx 2
|
||||||
|
addx 19
|
||||||
|
addx -18
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx -23
|
||||||
|
addx 22
|
||||||
|
addx 4
|
||||||
|
addx -34
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
addx 20
|
||||||
|
addx -17
|
||||||
|
noop
|
||||||
|
addx 25
|
||||||
|
addx -17
|
||||||
|
addx -2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 19
|
||||||
|
addx -12
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -37
|
||||||
|
addx 3
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx 24
|
||||||
|
addx -6
|
||||||
|
addx -15
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 6
|
||||||
|
addx -2
|
||||||
|
addx 6
|
||||||
|
addx -12
|
||||||
|
addx -2
|
||||||
|
addx 19
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
addx -24
|
||||||
|
addx -11
|
||||||
|
addx 4
|
||||||
|
addx 3
|
||||||
|
addx -2
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx -2
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx -2
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -35
|
||||||
|
addx 35
|
||||||
|
addx -21
|
||||||
|
addx -14
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 33
|
||||||
|
addx -7
|
||||||
|
addx -23
|
||||||
|
addx 5
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
noop
|
||||||
|
addx 3
|
||||||
|
addx -23
|
||||||
|
addx 30
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 4
|
||||||
|
addx -17
|
||||||
|
addx 11
|
||||||
|
noop
|
||||||
|
noop
|
146
2022/day-10/inputs/test.txt
Normal file
146
2022/day-10/inputs/test.txt
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
addx 15
|
||||||
|
addx -11
|
||||||
|
addx 6
|
||||||
|
addx -3
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -8
|
||||||
|
addx 13
|
||||||
|
addx 4
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx 5
|
||||||
|
addx -1
|
||||||
|
addx -35
|
||||||
|
addx 1
|
||||||
|
addx 24
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 16
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 21
|
||||||
|
addx -15
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -3
|
||||||
|
addx 9
|
||||||
|
addx 1
|
||||||
|
addx -3
|
||||||
|
addx 8
|
||||||
|
addx 1
|
||||||
|
addx 5
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -36
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 6
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 7
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx 13
|
||||||
|
addx 7
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
addx -33
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 8
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 17
|
||||||
|
addx -9
|
||||||
|
addx 1
|
||||||
|
addx 1
|
||||||
|
addx -3
|
||||||
|
addx 11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -13
|
||||||
|
addx -19
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
addx 26
|
||||||
|
addx -30
|
||||||
|
addx 12
|
||||||
|
addx -1
|
||||||
|
addx 3
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -9
|
||||||
|
addx 18
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 9
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx -1
|
||||||
|
addx 2
|
||||||
|
addx -37
|
||||||
|
addx 1
|
||||||
|
addx 3
|
||||||
|
noop
|
||||||
|
addx 15
|
||||||
|
addx -21
|
||||||
|
addx 22
|
||||||
|
addx -6
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx 2
|
||||||
|
addx 1
|
||||||
|
noop
|
||||||
|
addx -10
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
addx 20
|
||||||
|
addx 1
|
||||||
|
addx 2
|
||||||
|
addx 2
|
||||||
|
addx -6
|
||||||
|
addx -11
|
||||||
|
noop
|
||||||
|
noop
|
||||||
|
noop
|
58
2022/day-10/src/bin/part_one.rs
Normal file
58
2022/day-10/src/bin/part_one.rs
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use aoc_2022_10::{parse_input, Instruction};
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let infile_path = env::args().nth(1).expect("input file");
|
||||||
|
|
||||||
|
let result = solve(&infile_path)?;
|
||||||
|
|
||||||
|
println!("{result}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve(path: &str) -> io::Result<i64> {
|
||||||
|
let instructions = parse_input(path)?;
|
||||||
|
|
||||||
|
let mut cycles_total = 0;
|
||||||
|
let mut reg_x = 1;
|
||||||
|
let mut sig_strength_total = 0;
|
||||||
|
|
||||||
|
for instruction in instructions {
|
||||||
|
let (cycles, add_x) = match instruction {
|
||||||
|
Instruction::Noop => (1, 0),
|
||||||
|
Instruction::Addx(val) => (2, val),
|
||||||
|
};
|
||||||
|
|
||||||
|
for _ in 0..cycles {
|
||||||
|
cycles_total += 1;
|
||||||
|
|
||||||
|
if cycles_total == 20 || (cycles_total - 20) % 40 == 0 {
|
||||||
|
let sig_strength = cycles_total * reg_x;
|
||||||
|
sig_strength_total += sig_strength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reg_x += add_x;
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(sig_strength_total)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample() {
|
||||||
|
let result = solve("inputs/test.txt").unwrap();
|
||||||
|
assert_eq!(13140, result)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn puzzle() {
|
||||||
|
let result = solve("inputs/puzzle.txt").unwrap();
|
||||||
|
assert_eq!(13760, result);
|
||||||
|
}
|
||||||
|
}
|
92
2022/day-10/src/bin/part_two.rs
Normal file
92
2022/day-10/src/bin/part_two.rs
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
use std::{env, io};
|
||||||
|
|
||||||
|
use aoc_2022_10::{parse_input, Instruction};
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let infile_path = env::args().nth(1).expect("input file");
|
||||||
|
|
||||||
|
let result = solve(&infile_path)?;
|
||||||
|
|
||||||
|
println!("{result}");
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
const PIXEL_LIT: char = '#';
|
||||||
|
const PIXEL_UNLIT: char = '.';
|
||||||
|
const DRAW_CYCLE: i64 = 40;
|
||||||
|
|
||||||
|
fn solve(path: &str) -> io::Result<String> {
|
||||||
|
let instructions = parse_input(path)?;
|
||||||
|
|
||||||
|
let mut crt = Vec::<[char; 40]>::new();
|
||||||
|
let mut crt_row = [PIXEL_UNLIT; 40];
|
||||||
|
|
||||||
|
let mut cycles_total = 0;
|
||||||
|
let mut reg_x = 1_i64;
|
||||||
|
|
||||||
|
for instruction in instructions {
|
||||||
|
let (cycles, add_x) = match instruction {
|
||||||
|
Instruction::Noop => (1, 0),
|
||||||
|
Instruction::Addx(val) => (2, val),
|
||||||
|
};
|
||||||
|
|
||||||
|
for _ in 0..cycles {
|
||||||
|
let cur_row_pos = cycles_total % DRAW_CYCLE;
|
||||||
|
|
||||||
|
if (reg_x - 1..=reg_x + 1).contains(&cur_row_pos) {
|
||||||
|
crt_row[cur_row_pos as usize] = PIXEL_LIT;
|
||||||
|
}
|
||||||
|
|
||||||
|
cycles_total += 1;
|
||||||
|
|
||||||
|
if cycles_total % DRAW_CYCLE == 0 {
|
||||||
|
crt.push(crt_row);
|
||||||
|
crt_row = [PIXEL_UNLIT; 40];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reg_x += add_x;
|
||||||
|
}
|
||||||
|
|
||||||
|
let lines = crt
|
||||||
|
.iter()
|
||||||
|
.map(|elem| elem.iter().collect::<String>())
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
Ok(lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn sample() {
|
||||||
|
const OUTPUT: &str = "\
|
||||||
|
##..##..##..##..##..##..##..##..##..##..
|
||||||
|
###...###...###...###...###...###...###.
|
||||||
|
####....####....####....####....####....
|
||||||
|
#####.....#####.....#####.....#####.....
|
||||||
|
######......######......######......####
|
||||||
|
#######.......#######.......#######.....";
|
||||||
|
|
||||||
|
let result = solve("inputs/test.txt").unwrap();
|
||||||
|
assert_eq!(OUTPUT, &result)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn puzzle() {
|
||||||
|
const OUTPUT: &str = "\
|
||||||
|
###..####.#..#.####..##..###..####.####.
|
||||||
|
#..#.#....#.#.....#.#..#.#..#.#....#....
|
||||||
|
#..#.###..##.....#..#....#..#.###..###..
|
||||||
|
###..#....#.#...#...#....###..#....#....
|
||||||
|
#.#..#....#.#..#....#..#.#....#....#....
|
||||||
|
#..#.#....#..#.####..##..#....####.#....";
|
||||||
|
|
||||||
|
let result = solve("inputs/puzzle.txt").unwrap();
|
||||||
|
assert_eq!(OUTPUT, result);
|
||||||
|
}
|
||||||
|
}
|
33
2022/day-10/src/lib.rs
Normal file
33
2022/day-10/src/lib.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use std::{
|
||||||
|
fs::File,
|
||||||
|
io::{self, BufRead, BufReader},
|
||||||
|
path::Path,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub enum Instruction {
|
||||||
|
Noop,
|
||||||
|
Addx(i64),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_input<P>(path: P) -> io::Result<Vec<Instruction>>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let file = File::open(path)?;
|
||||||
|
let mut instructions = vec![];
|
||||||
|
|
||||||
|
for line in BufReader::new(file).lines() {
|
||||||
|
let line = line?;
|
||||||
|
let parts = line.split_whitespace().collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let instr = match parts[..] {
|
||||||
|
["addx", val] => Instruction::Addx(val.parse().unwrap()),
|
||||||
|
["noop"] => Instruction::Noop,
|
||||||
|
[..] => panic!("Invalid instruction"),
|
||||||
|
};
|
||||||
|
|
||||||
|
instructions.push(instr);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(instructions)
|
||||||
|
}
|
@ -9,4 +9,4 @@
|
|||||||
- [2019](2019/README.md) (0% completed)
|
- [2019](2019/README.md) (0% completed)
|
||||||
- [2020](2020/README.md) (20% completed)
|
- [2020](2020/README.md) (20% completed)
|
||||||
- [2021](2021/README.md) (68% completed)
|
- [2021](2021/README.md) (68% completed)
|
||||||
- [2022](2022/README.md) (36% completed)
|
- [2022](2022/README.md) (40% completed)
|
||||||
|
Loading…
Reference in New Issue
Block a user