From 4e0ca00936fca466dbd47a0e81aaaefa73bc0afc Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 3 Dec 2017 02:31:14 -0500 Subject: Day 3 This is fucking awful and I'm angry. --- 2017/input/day03.txt | 1 + 2017/src/bin/day03.rs | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 2017/input/day03.txt create mode 100644 2017/src/bin/day03.rs (limited to '2017') diff --git a/2017/input/day03.txt b/2017/input/day03.txt new file mode 100644 index 0000000..9cf67fc --- /dev/null +++ b/2017/input/day03.txt @@ -0,0 +1 @@ +368078 diff --git a/2017/src/bin/day03.rs b/2017/src/bin/day03.rs new file mode 100644 index 0000000..4f38ac3 --- /dev/null +++ b/2017/src/bin/day03.rs @@ -0,0 +1,57 @@ +use std::io::{self, Read}; + +// 17 16 15 14 13 +// 18 5 4 3 12 +// 19 6 1 2 11 +// 20 7 8 9 10 +// 21 22 23---> ... +// +// 1 R +// 1 U +// 2 L +// 2 D +// 3 R +// 3 U +// 4 L +// 4 D +// 5 R + +fn solve1(input: i32) -> i32 { + let ds = [(1, 0), (0, 1), (-1, 0), (0, -1)]; + + let mut n = 1; + let mut x = 0i32; + let mut y = 0i32; + let mut r = 1; + let mut i = 0; + + for &(dx, dy) in ds.iter().cycle() { + for _ in 0..r { + if n == input { + return x.abs() + y.abs(); + } + + n += 1; + x += dx; + y += dy; + } + r += i % 2; + i += 1; + } + unreachable!() +} + +fn main() { + let mut input = String::new(); + io::stdin().read_to_string(&mut input).unwrap(); + + println!("Part 1: {}", solve1(input.trim().parse().unwrap())); +} + +#[test] +fn part1() { + assert_eq!(0, solve1(1)); + assert_eq!(3, solve1(12)); + assert_eq!(2, solve1(23)); + assert_eq!(31, solve1(1024)); +} -- cgit 1.4.1 type='hidden' name='id' value='e3cff14e0336b70bb7aad4855b8b2f6c4d963d05'/>
path: root/ui.c (unfollow)
Commit message (Collapse)Author
2018-09-11Don't render every PM as a pingJune McEnroe
2018-09-11Add urlOpenMatchJune McEnroe
2018-09-10Depend on man.sh for chroot.tar targetJune McEnroe
2018-09-10Set LESSSECURE=1 in man.shJune McEnroe
Ridiculous.
2018-09-10Add /man commandJune McEnroe
2018-09-10Install man page in chrootJune McEnroe
2018-09-10Install man pageJune McEnroe
2018-09-10Split keys into subsections and document colorsJune McEnroe
2018-09-10Add "blank" lines to chatte.1June McEnroe
2018-09-10Document key bindings in chatte.1June McEnroe
2018-09-08Document slash commands in chatte.1June McEnroe