Added day 10 part 1
This commit is contained in:
parent
c739a665d3
commit
77fb6d71c8
2141
inputs/input.txt
2141
inputs/input.txt
File diff suppressed because it is too large
Load Diff
|
@ -1,8 +1,146 @@
|
||||||
R 5
|
addx 15
|
||||||
U 8
|
addx -11
|
||||||
L 8
|
addx 6
|
||||||
D 3
|
addx -3
|
||||||
R 17
|
addx 5
|
||||||
D 10
|
addx -1
|
||||||
L 25
|
addx -8
|
||||||
U 20
|
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
|
||||||
|
|
105
src/main.rs
105
src/main.rs
|
@ -1,37 +1,32 @@
|
||||||
#![feature(byte_slice_trim_ascii)]
|
|
||||||
#![feature(test)]
|
#![feature(test)]
|
||||||
extern crate test;
|
extern crate test;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
const INPUTS: [&str; 2] = [
|
||||||
|
include_str!("../inputs/sample.txt"),
|
||||||
const INPUTS: [&[u8]; 2] = [
|
include_str!("../inputs/input.txt"),
|
||||||
include_bytes!("../inputs/sample.txt"),
|
|
||||||
include_bytes!("../inputs/input.txt"),
|
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
enum Move {
|
enum Ins {
|
||||||
R(u8),
|
Noop,
|
||||||
L(u8),
|
Addx(i32),
|
||||||
U(u8),
|
|
||||||
D(u8),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn parse(input: &[u8]) -> Vec<Move> {
|
fn parse(input: &'static str) -> Vec<Ins> {
|
||||||
input
|
input
|
||||||
.trim_ascii()
|
.trim()
|
||||||
.split(|&c| c == b'\n')
|
.split('\n')
|
||||||
.map(|line| {
|
.flat_map(|line| {
|
||||||
let (a, b) = line.split_at(1);
|
let (a, b) = line.split_at(4);
|
||||||
|
|
||||||
let b = b.iter().skip(1).fold(0, |a, x| (a * 10) + (x - b'0'));
|
match a {
|
||||||
|
"noop" => vec![Ins::Noop],
|
||||||
match &a {
|
"addx" => {
|
||||||
[b'R'] => Move::R(b),
|
let b = b.trim();
|
||||||
[b'L'] => Move::L(b),
|
let b: i32 = b.parse::<i32>().unwrap();
|
||||||
[b'U'] => Move::U(b),
|
|
||||||
[b'D'] => Move::D(b),
|
|
||||||
|
|
||||||
|
vec![Ins::Noop, Ins::Addx(b)]
|
||||||
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -46,55 +41,33 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn solution(input: Vec<Move>) -> usize {
|
fn solution(input: Vec<Ins>) -> i32 {
|
||||||
let mut locs = [(0, 0); 10];
|
let mut register = 1i32;
|
||||||
let mut set: HashSet<(i32, i32)> = HashSet::with_capacity_and_hasher(3000, Default::default());
|
let mut cycle = 0;
|
||||||
set.insert((0, 0));
|
let mut store = [0; 220];
|
||||||
|
|
||||||
for mmove in input {
|
for ip in input.into_iter() {
|
||||||
let (steps, (dsxh, dsyh)) = match mmove {
|
match ip {
|
||||||
Move::R(v) => (v, (1, 0)),
|
Ins::Noop => {
|
||||||
Move::L(v) => (v, (-1, 0)),
|
store[cycle] = register;
|
||||||
Move::U(v) => (v, (0, -1)),
|
|
||||||
Move::D(v) => (v, (0, 1)),
|
|
||||||
};
|
|
||||||
|
|
||||||
let locs9 = locs[9];
|
|
||||||
|
|
||||||
for _ in 0..steps {
|
|
||||||
// Update Head position
|
|
||||||
locs[0].0 += dsxh;
|
|
||||||
locs[0].1 += dsyh;
|
|
||||||
|
|
||||||
// One by one, Updated position of each knot
|
|
||||||
for i in 1..10 {
|
|
||||||
let loci = move_tail(locs[i - 1], locs[i]);
|
|
||||||
if loci == locs[i] {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
locs[i] = loci;
|
|
||||||
}
|
}
|
||||||
|
Ins::Addx(v) => {
|
||||||
if locs9 != locs[9] {
|
store[cycle] = register;
|
||||||
set.insert(locs[9]);
|
register += v;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
cycle += 1;
|
||||||
|
if cycle >= 220 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
set.len()
|
store[20 - 1] * 20
|
||||||
}
|
+ store[60 - 1] * 60
|
||||||
|
+ store[100 - 1] * 100
|
||||||
#[inline]
|
+ store[140 - 1] * 140
|
||||||
const fn move_tail((sxh, syh): (i32, i32), (sxt, syt): (i32, i32)) -> (i32, i32) {
|
+ store[180 - 1] * 180
|
||||||
let dx = sxh - sxt;
|
+ store[220 - 1] * 220
|
||||||
let dy = syh - syt;
|
|
||||||
|
|
||||||
if dx.abs() == 2 || dy.abs() == 2 {
|
|
||||||
// signum gets you 1 or -1 depending on the sign of number
|
|
||||||
(sxt + dx.signum(), syt + dy.signum())
|
|
||||||
} else {
|
|
||||||
(sxt, syt)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[bench]
|
#[bench]
|
||||||
|
|
Loading…
Reference in New Issue
Block a user