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
|
||||
U 8
|
||||
L 8
|
||||
D 3
|
||||
R 17
|
||||
D 10
|
||||
L 25
|
||||
U 20
|
||||
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
|
||||
|
|
107
src/main.rs
107
src/main.rs
|
@ -1,37 +1,32 @@
|
|||
#![feature(byte_slice_trim_ascii)]
|
||||
#![feature(test)]
|
||||
extern crate test;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
const INPUTS: [&[u8]; 2] = [
|
||||
include_bytes!("../inputs/sample.txt"),
|
||||
include_bytes!("../inputs/input.txt"),
|
||||
const INPUTS: [&str; 2] = [
|
||||
include_str!("../inputs/sample.txt"),
|
||||
include_str!("../inputs/input.txt"),
|
||||
];
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Move {
|
||||
R(u8),
|
||||
L(u8),
|
||||
U(u8),
|
||||
D(u8),
|
||||
enum Ins {
|
||||
Noop,
|
||||
Addx(i32),
|
||||
}
|
||||
|
||||
fn parse(input: &[u8]) -> Vec<Move> {
|
||||
fn parse(input: &'static str) -> Vec<Ins> {
|
||||
input
|
||||
.trim_ascii()
|
||||
.split(|&c| c == b'\n')
|
||||
.map(|line| {
|
||||
let (a, b) = line.split_at(1);
|
||||
.trim()
|
||||
.split('\n')
|
||||
.flat_map(|line| {
|
||||
let (a, b) = line.split_at(4);
|
||||
|
||||
let b = b.iter().skip(1).fold(0, |a, x| (a * 10) + (x - b'0'));
|
||||
|
||||
match &a {
|
||||
[b'R'] => Move::R(b),
|
||||
[b'L'] => Move::L(b),
|
||||
[b'U'] => Move::U(b),
|
||||
[b'D'] => Move::D(b),
|
||||
match a {
|
||||
"noop" => vec![Ins::Noop],
|
||||
"addx" => {
|
||||
let b = b.trim();
|
||||
let b: i32 = b.parse::<i32>().unwrap();
|
||||
|
||||
vec![Ins::Noop, Ins::Addx(b)]
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
})
|
||||
|
@ -46,55 +41,33 @@ fn main() {
|
|||
}
|
||||
}
|
||||
|
||||
fn solution(input: Vec<Move>) -> usize {
|
||||
let mut locs = [(0, 0); 10];
|
||||
let mut set: HashSet<(i32, i32)> = HashSet::with_capacity_and_hasher(3000, Default::default());
|
||||
set.insert((0, 0));
|
||||
fn solution(input: Vec<Ins>) -> i32 {
|
||||
let mut register = 1i32;
|
||||
let mut cycle = 0;
|
||||
let mut store = [0; 220];
|
||||
|
||||
for mmove in input {
|
||||
let (steps, (dsxh, dsyh)) = match mmove {
|
||||
Move::R(v) => (v, (1, 0)),
|
||||
Move::L(v) => (v, (-1, 0)),
|
||||
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] {
|
||||
for ip in input.into_iter() {
|
||||
match ip {
|
||||
Ins::Noop => {
|
||||
store[cycle] = register;
|
||||
}
|
||||
Ins::Addx(v) => {
|
||||
store[cycle] = register;
|
||||
register += v;
|
||||
}
|
||||
}
|
||||
cycle += 1;
|
||||
if cycle >= 220 {
|
||||
break;
|
||||
}
|
||||
locs[i] = loci;
|
||||
}
|
||||
|
||||
if locs9 != locs[9] {
|
||||
set.insert(locs[9]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
set.len()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
const fn move_tail((sxh, syh): (i32, i32), (sxt, syt): (i32, i32)) -> (i32, i32) {
|
||||
let dx = sxh - sxt;
|
||||
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)
|
||||
}
|
||||
store[20 - 1] * 20
|
||||
+ store[60 - 1] * 60
|
||||
+ store[100 - 1] * 100
|
||||
+ store[140 - 1] * 140
|
||||
+ store[180 - 1] * 180
|
||||
+ store[220 - 1] * 220
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
|
Loading…
Reference in New Issue
Block a user