Skip to content

Instantly share code, notes, and snippets.

@jtdowney
Last active December 3, 2017 18:05
Show Gist options
  • Save jtdowney/2869615d6dea60175c498572fc9d8f13 to your computer and use it in GitHub Desktop.
Save jtdowney/2869615d6dea60175c498572fc9d8f13 to your computer and use it in GitHub Desktop.
day 3
use std::env;
#[derive(Debug)]
enum Direction {
Left,
Right,
Up,
Down,
}
fn part1(size: usize) -> i32 {
let mut direction = Direction::Right;
let mut memory = vec![vec![1]];
let mut cur_x = 0;
let mut cur_y = 0;
let mut max_x = 1;
let mut max_y = 1;
let mut origin_x = 0;
let mut origin_y = 0;
for i in 2..size + 1 {
match direction {
Direction::Right => {
cur_x += 1;
if cur_x == max_x {
for row in memory.iter_mut() {
row.push(0);
}
max_x += 1;
direction = Direction::Up;
}
memory[cur_y][cur_x] = i;
}
Direction::Up => {
if cur_y == 0 {
memory.insert(0, vec![0; max_x]);
origin_y += 1;
max_y += 1;
direction = Direction::Left;
} else {
cur_y -= 1;
}
memory[cur_y][cur_x] = i;
}
Direction::Left => {
if cur_x == 0 {
for row in memory.iter_mut() {
row.insert(0, 0);
}
origin_x += 1;
max_x += 1;
direction = Direction::Down;
} else {
cur_x -= 1;
}
memory[cur_y][cur_x] = i;
}
Direction::Down => {
cur_y += 1;
if cur_y == max_y {
memory.push(vec![0; max_x]);
max_y += 1;
direction = Direction::Right;
}
memory[cur_y][cur_x] = i;
}
}
}
(cur_x as i32 - origin_x as i32).abs() + (cur_y as i32 - origin_y as i32).abs()
}
fn main() {
let size = env::args().nth(1).expect("size of memory").parse().expect(
"size to be a number",
);
let distance = part1(size);
println!("distance: {}", distance);
}
use std::env;
#[derive(Debug)]
enum Direction {
Left,
Right,
Up,
Down,
}
fn part2(size: usize) -> usize {
let mut direction = Direction::Right;
let mut memory = vec![vec![1]];
let mut cur_x = 0;
let mut cur_y = 0;
let mut max_x = 1;
let mut max_y = 1;
loop {
match direction {
Direction::Right => {
cur_x += 1;
if cur_x == max_x {
for row in memory.iter_mut() {
row.push(0);
}
max_x += 1;
direction = Direction::Up;
}
}
Direction::Up => {
if cur_y == 0 {
memory.insert(0, vec![0; max_x]);
max_y += 1;
direction = Direction::Left;
} else {
cur_y -= 1;
}
}
Direction::Left => {
if cur_x == 0 {
for row in memory.iter_mut() {
row.insert(0, 0);
}
max_x += 1;
direction = Direction::Down;
} else {
cur_x -= 1;
}
}
Direction::Down => {
cur_y += 1;
if cur_y == max_y {
memory.push(vec![0; max_x]);
max_y += 1;
direction = Direction::Right;
}
}
}
memory[cur_y][cur_x] = [
(-1, -1),
(0, -1),
(1, -1),
(-1, 0),
(1, 0),
(-1, 1),
(0, 1),
(1, 1),
].iter()
.filter_map(|&(dx, dy)| {
memory.get((cur_y as isize + dy) as usize).and_then(|r| {
r.get((cur_x as isize + dx) as usize)
})
})
.sum();
if memory[cur_y][cur_x] > size {
break;
}
}
memory[cur_y][cur_x]
}
fn main() {
let size = env::args().nth(1).expect("size of memory").parse().expect(
"size to be a number",
);
let value = part2(size);
println!("value: {}", value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment