Compare commits

...

4 commits

Author SHA1 Message Date
0618083106 2022 day10/rust: use generators 2022-12-10 09:42:00 +01:00
135a5017f7 2022 day10/rust: cleanup 2022-12-10 06:38:47 +01:00
e0ff12f770 2022 day10/rust: add solution 2022-12-10 06:29:51 +01:00
6cae81cd65 2022 day10: add data 2022-12-10 06:27:02 +01:00
6 changed files with 279 additions and 0 deletions

7
2022/data/day10.expected Normal file
View file

@ -0,0 +1,7 @@
13520
###...##..###..#..#.###..####..##..###..
#..#.#..#.#..#.#..#.#..#.#....#..#.#..#.
#..#.#....#..#.####.###..###..#..#.###..
###..#.##.###..#..#.#..#.#....####.#..#.
#....#..#.#....#..#.#..#.#....#..#.#..#.
#.....###.#....#..#.###..####.#..#.###..

147
2022/data/day10.input Normal file
View file

@ -0,0 +1,147 @@
noop
noop
noop
addx 6
addx -1
addx 5
noop
noop
noop
addx 5
addx 11
addx -10
addx 4
noop
addx 5
noop
noop
noop
addx 1
noop
addx 4
addx 5
noop
noop
noop
addx -35
addx -2
addx 5
addx 2
addx 3
addx -2
addx 2
addx 5
addx 2
addx 3
addx -2
addx 2
addx 5
addx 2
addx 3
addx -28
addx 28
addx 5
addx 2
addx -9
addx 10
addx -38
noop
addx 3
addx 2
addx 7
noop
noop
addx -9
addx 10
addx 4
addx 2
addx 3
noop
noop
addx -2
addx 7
noop
noop
noop
addx 3
addx 5
addx 2
noop
noop
noop
addx -35
noop
noop
noop
addx 5
addx 2
noop
addx 3
noop
noop
noop
addx 5
addx 3
addx -2
addx 2
addx 5
addx 2
addx -25
noop
addx 30
noop
addx 1
noop
addx 2
noop
addx 3
addx -38
noop
addx 7
addx -2
addx 5
addx 2
addx -8
addx 13
addx -2
noop
addx 3
addx 2
addx 5
addx 2
addx -15
noop
addx 20
addx 3
noop
addx 2
addx -4
addx 5
addx -38
addx 8
noop
noop
noop
noop
noop
noop
addx 2
addx 17
addx -10
addx 3
noop
addx 2
addx 1
addx -16
addx 19
addx 2
noop
addx 2
addx 5
addx 2
noop
noop
noop
noop
noop
noop

View file

@ -0,0 +1,9 @@
[package]
name = "rust_2022_10"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = { path = "../../../common/rust" }

108
2022/day10/rust/src/main.rs Normal file
View file

@ -0,0 +1,108 @@
#![feature(generators)]
#![feature(generator_clone)]
#![feature(iter_from_generator)]
#![warn(clippy::pedantic)]
use std::{
io::{stdin, Read},
iter,
str::FromStr,
};
type Word = i64;
#[derive(Debug, Clone, PartialEq, Eq)]
struct MachineState {
x: Word,
}
impl MachineState {
const fn new() -> Self {
Self { x: 1 }
}
fn execute(&mut self, op: MicroOp) {
match op {
MicroOp::Noop => {}
MicroOp::Addx(i) => self.x += i,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MicroOp {
Noop,
Addx(Word),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Instruction {
Noop,
Addx(Word),
}
impl Instruction {
pub fn expand(self) -> impl Iterator<Item = MicroOp> {
iter::from_generator(move || match self {
Self::Noop => yield MicroOp::Noop,
Self::Addx(x) => {
yield MicroOp::Noop;
yield MicroOp::Addx(x);
}
})
}
}
impl FromStr for Instruction {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut args = s.split_whitespace();
match args.next().ok_or(())? {
"noop" => Ok(Self::Noop),
"addx" => {
let i = args.next().ok_or(())?.parse().map_err(|_| ())?;
Ok(Self::Addx(i))
}
_ => Err(()),
}
}
}
fn main() {
const START: MachineState = MachineState::new();
let mut data = String::new();
stdin().read_to_string(&mut data).unwrap();
let states: Vec<_> = data
.lines()
.map(|l| l.parse().unwrap())
.flat_map(Instruction::expand)
.scan(START, |state, op| {
let prev = state.clone();
state.execute(op);
Some(prev)
})
.enumerate()
.map(|(i, state)| (Word::try_from(i).unwrap() + 1, state))
.collect();
let sum: Word = states
.iter()
.skip(20 - 1)
.step_by(40)
.map(|(i, state)| i * state.x)
.sum();
println!("{:?}", sum);
let crt: Vec<_> = states
.iter()
.map(|(i, state)| ((i - 1) % 40).abs_diff(state.x) < 2)
.map(|x| if x { '#' } else { '.' })
.collect();
for line in crt.chunks_exact(40) {
println!("{}", line.iter().collect::<String>());
}
}

7
Cargo.lock generated
View file

@ -531,6 +531,13 @@ dependencies = [
"aoc",
]
[[package]]
name = "rust_2022_10"
version = "0.1.0"
dependencies = [
"aoc",
]
[[package]]
name = "ryu"
version = "1.0.11"

View file

@ -19,4 +19,5 @@ members = [
"2022/day7/rust",
"2022/day8/rust",
"2022/day9/rust",
"2022/day10/rust",
]