Compare commits
4 commits
daad18c6af
...
0618083106
Author | SHA1 | Date | |
---|---|---|---|
0618083106 | |||
135a5017f7 | |||
e0ff12f770 | |||
6cae81cd65 |
6 changed files with 279 additions and 0 deletions
7
2022/data/day10.expected
Normal file
7
2022/data/day10.expected
Normal file
|
@ -0,0 +1,7 @@
|
|||
13520
|
||||
###...##..###..#..#.###..####..##..###..
|
||||
#..#.#..#.#..#.#..#.#..#.#....#..#.#..#.
|
||||
#..#.#....#..#.####.###..###..#..#.###..
|
||||
###..#.##.###..#..#.#..#.#....####.#..#.
|
||||
#....#..#.#....#..#.#..#.#....#..#.#..#.
|
||||
#.....###.#....#..#.###..####.#..#.###..
|
147
2022/data/day10.input
Normal file
147
2022/data/day10.input
Normal 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
|
9
2022/day10/rust/Cargo.toml
Normal file
9
2022/day10/rust/Cargo.toml
Normal 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
108
2022/day10/rust/src/main.rs
Normal 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
7
Cargo.lock
generated
|
@ -531,6 +531,13 @@ dependencies = [
|
|||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_2022_10"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"aoc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.11"
|
||||
|
|
|
@ -19,4 +19,5 @@ members = [
|
|||
"2022/day7/rust",
|
||||
"2022/day8/rust",
|
||||
"2022/day9/rust",
|
||||
"2022/day10/rust",
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue