summary refs log tree commit diff homepage
path: root/2017/src
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2017-12-03 19:35:22 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commitfe750311b96eec22103143042eef1f4e4a722970 (patch)
treec50f87c2daf938b54654e4bb0a449697f9e17622 /2017/src
parentDay 3, clean up (diff)
downloadaoc-fe750311b96eec22103143042eef1f4e4a722970.tar.gz
aoc-fe750311b96eec22103143042eef1f4e4a722970.zip
Day 3, part 2
I am super surprised that worked on the first try.
Diffstat (limited to '2017/src')
-rw-r--r--2017/src/bin/day03.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/2017/src/bin/day03.rs b/2017/src/bin/day03.rs
index 8a556a1..99aecd2 100644
--- a/2017/src/bin/day03.rs
+++ b/2017/src/bin/day03.rs
@@ -1,3 +1,4 @@
+use std::collections::HashMap;
 use std::io::{self, Read};
 
 fn solve1(input: i32) -> i32 {
@@ -19,11 +20,44 @@ fn solve1(input: i32) -> i32 {
     unreachable!()
 }
 
+fn solve2(input: i32) -> i32 {
+    let spiral = [(1, 0), (0, 1), (-1, 0), (0, -1)];
+    let (mut x, mut y) = (0i32, 0i32);
+    let mut values = HashMap::new();
+    values.insert((0, 0), 1);
+
+    for (i, &(dx, dy)) in spiral.iter().cycle().enumerate() {
+        let length = 1 + i / 2;
+        for _ in 0..length {
+            let n =
+                values.get(&(x,     y    )).unwrap_or(&0) +
+                values.get(&(x - 1, y - 1)).unwrap_or(&0) +
+                values.get(&(x,     y - 1)).unwrap_or(&0) +
+                values.get(&(x + 1, y - 1)).unwrap_or(&0) +
+                values.get(&(x + 1, y    )).unwrap_or(&0) +
+                values.get(&(x + 1, y + 1)).unwrap_or(&0) +
+                values.get(&(x,     y + 1)).unwrap_or(&0) +
+                values.get(&(x - 1, y + 1)).unwrap_or(&0) +
+                values.get(&(x - 1, y    )).unwrap_or(&0);
+            values.insert((x, y), n);
+
+            if n > input {
+                return n;
+            }
+
+            x += dx;
+            y += dy;
+        }
+    }
+    unreachable!()
+}
+
 fn main() {
     let mut input = String::new();
     io::stdin().read_to_string(&mut input).unwrap();
 
     println!("Part 1: {}", solve1(input.trim().parse().unwrap()));
+    println!("Part 2: {}", solve2(input.trim().parse().unwrap()));
 }
 
 #[test]
er-highlight'> 2019-09-27Fail on HTTP failure status in titleJune McEnroe 2019-09-23Add Trail of LightningJune McEnroe 2019-09-22Revert "Enable cookies in title"June McEnroe This reverts commit 279111dda15dd9170e11b9688eb973f2af2e6300. 2019-09-20Enable cookies in titleJune McEnroe Perhaps this will make it less suspicious to Google. Who knows. 2019-09-16Use sensitivity aliases in TF2June McEnroe 2019-09-16Add The Just CityJune McEnroe 2019-09-12Only GET the final redirect locationJune McEnroe 2019-09-12Consume entire bodyJune McEnroe Aborting the request and leaving data around may be causing intermittent errors. Just discard the rest of the data. 2019-09-10Add title -v flagJune McEnroe 2019-09-10Use curl error bufferJune McEnroe 2019-09-10Set Accept-Encoding in titleJune McEnroe Because apparently it's fine for servers to respond with Content-Encoding you didn't ask for, and curl won't decode it if you didn't ask for it. 2019-09-08Set title User-AgentJune McEnroe Some things don't like you if you don't send one. 2019-09-07Add -x flag to titleJune McEnroe 2019-09-07Ignore SIGPIPE in relayJune McEnroe Allows restarting consumers safely. 2019-09-07Add A Memory Called EmpireJune McEnroe 2019-09-05Handle lack of Content-TypeJune McEnroe 2019-09-05Use CURLINFO_CONTENT_TYPEJune McEnroe Oops, didn't see this. 2019-09-05Decode entities in titlesJune McEnroe 2019-09-05Print title as soon as it's availableJune McEnroe 2019-09-05Use CURL_PREFIX to set flagsJune McEnroe 2019-09-05Add titleJune McEnroe 2019-09-04Add Avorter n'est pas tuerJune McEnroe 2019-08-29Unset executable on shell scriptsJune McEnroe 2019-08-29Add long-missing setopt to bin.7June McEnroe 2019-08-29Add editJune McEnroe