mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 11:23:17 -09:00
do day 10
This commit is contained in:
parent
dc38a5a155
commit
c3808cc473
138
2022/input/day_10.txt
Normal file
138
2022/input/day_10.txt
Normal file
@ -0,0 +1,138 @@
|
||||
addx 1
|
||||
addx 5
|
||||
addx -1
|
||||
addx 20
|
||||
addx -14
|
||||
addx -1
|
||||
addx 5
|
||||
addx 13
|
||||
addx -12
|
||||
addx 3
|
||||
addx 3
|
||||
addx 3
|
||||
addx 1
|
||||
addx 4
|
||||
noop
|
||||
noop
|
||||
addx 1
|
||||
noop
|
||||
noop
|
||||
addx 4
|
||||
noop
|
||||
addx -35
|
||||
addx 11
|
||||
addx -1
|
||||
addx -7
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
addx -2
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx 2
|
||||
addx 7
|
||||
noop
|
||||
noop
|
||||
addx 3
|
||||
addx -2
|
||||
addx -36
|
||||
noop
|
||||
addx 25
|
||||
addx -22
|
||||
addx 7
|
||||
noop
|
||||
addx -2
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
addx 5
|
||||
addx 4
|
||||
noop
|
||||
addx -2
|
||||
addx 5
|
||||
addx -4
|
||||
addx 5
|
||||
addx 4
|
||||
noop
|
||||
addx -29
|
||||
addx 32
|
||||
addx -23
|
||||
addx -12
|
||||
noop
|
||||
addx 7
|
||||
noop
|
||||
addx -2
|
||||
addx 4
|
||||
addx 3
|
||||
addx 20
|
||||
addx 3
|
||||
addx -20
|
||||
addx 5
|
||||
addx 16
|
||||
addx -15
|
||||
addx 6
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
addx 5
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
addx -37
|
||||
addx 2
|
||||
addx -2
|
||||
addx 7
|
||||
noop
|
||||
addx -2
|
||||
addx 5
|
||||
addx 2
|
||||
addx 3
|
||||
addx -2
|
||||
addx 2
|
||||
addx 5
|
||||
addx 2
|
||||
addx -6
|
||||
addx -15
|
||||
addx 24
|
||||
addx 2
|
||||
noop
|
||||
addx 3
|
||||
addx -8
|
||||
addx 15
|
||||
addx -14
|
||||
addx 15
|
||||
addx -38
|
||||
noop
|
||||
noop
|
||||
addx 21
|
||||
addx -14
|
||||
addx 1
|
||||
addx 5
|
||||
noop
|
||||
addx -2
|
||||
addx 7
|
||||
addx -1
|
||||
addx 5
|
||||
noop
|
||||
addx 2
|
||||
addx 3
|
||||
addx 3
|
||||
addx -2
|
||||
addx 4
|
||||
addx 2
|
||||
addx -17
|
||||
addx 20
|
||||
noop
|
||||
noop
|
||||
noop
|
||||
noop
|
146
2022/input/day_10_sample.txt
Normal file
146
2022/input/day_10_sample.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
|
112
2022/src/day_10.rs
Normal file
112
2022/src/day_10.rs
Normal file
@ -0,0 +1,112 @@
|
||||
use Instruction::*;
|
||||
#[derive(Clone, Copy)]
|
||||
enum Instruction {
|
||||
Addx(i32),
|
||||
Noop,
|
||||
}
|
||||
type Data = Vec<Instruction>;
|
||||
type Output = i32;
|
||||
fn parse(input: &str) -> Data {
|
||||
input
|
||||
.lines()
|
||||
.map(|line| {
|
||||
if line.starts_with("addx") {
|
||||
let (_addx, num) = line.split_once(' ').unwrap();
|
||||
Addx(num.parse().unwrap())
|
||||
} else {
|
||||
Noop
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn part_one(data: Data) -> Output {
|
||||
let mut r_x = 1;
|
||||
let mut instr_iter = data.iter();
|
||||
let mut cycles_left = 0;
|
||||
let mut last_instr = Noop;
|
||||
let mut sum = 0;
|
||||
for cycle in 1.. {
|
||||
if cycles_left == 0 {
|
||||
match last_instr {
|
||||
Noop => {}
|
||||
Addx(num) => {
|
||||
r_x += num;
|
||||
}
|
||||
}
|
||||
last_instr = match instr_iter.next() {
|
||||
Some(instr) => *instr,
|
||||
_ => break,
|
||||
};
|
||||
cycles_left = match last_instr {
|
||||
Addx(_) => 2,
|
||||
Noop => 1,
|
||||
}
|
||||
}
|
||||
if (cycle - 20) % 40 == 0 {
|
||||
sum += r_x * cycle;
|
||||
}
|
||||
cycles_left -= 1;
|
||||
}
|
||||
sum
|
||||
}
|
||||
fn part_two(data: Data) -> String {
|
||||
let mut r_x = 1;
|
||||
let mut instr_iter = data.iter();
|
||||
let mut cycles_left = 0;
|
||||
let mut last_instr = Noop;
|
||||
let mut output = vec![];
|
||||
for cycle in 0.. {
|
||||
if cycles_left == 0 {
|
||||
match last_instr {
|
||||
Noop => {}
|
||||
Addx(num) => {
|
||||
r_x += num;
|
||||
}
|
||||
}
|
||||
last_instr = match instr_iter.next() {
|
||||
Some(instr) => *instr,
|
||||
_ => break,
|
||||
};
|
||||
cycles_left = match last_instr {
|
||||
Addx(_) => 2,
|
||||
Noop => 1,
|
||||
}
|
||||
}
|
||||
let i: i32 = cycle % 40;
|
||||
if i == 0 {
|
||||
output.push("".to_string());
|
||||
}
|
||||
if i.abs_diff(r_x) <= 1 {
|
||||
*output.last_mut().unwrap() += "#";
|
||||
} else {
|
||||
*output.last_mut().unwrap() += ".";
|
||||
}
|
||||
cycles_left -= 1;
|
||||
}
|
||||
output.join("\n")
|
||||
}
|
||||
|
||||
advent_of_code_macro::generate_tests!(
|
||||
day 10,
|
||||
parse,
|
||||
part_one,
|
||||
part_two,
|
||||
sample tests [
|
||||
13140,
|
||||
"##..##..##..##..##..##..##..##..##..##..
|
||||
###...###...###...###...###...###...###.
|
||||
####....####....####....####....####....
|
||||
#####.....#####.....#####.....#####.....
|
||||
######......######......######......####
|
||||
#######.......#######.......#######....."
|
||||
],
|
||||
star tests [
|
||||
17380,
|
||||
"####..##...##..#..#.####.###..####..##..
|
||||
#....#..#.#..#.#..#....#.#..#.#....#..#.
|
||||
###..#....#....#..#...#..#..#.###..#....
|
||||
#....#.##.#....#..#..#...###..#....#....
|
||||
#....#..#.#..#.#..#.#....#.#..#....#..#.
|
||||
#.....###..##...##..####.#..#.####..##.."
|
||||
]
|
||||
);
|
Loading…
x
Reference in New Issue
Block a user