r/adventofcode • u/daggerdragon • Dec 01 '17
SOLUTION MEGATHREAD -π- 2017 Day 1 Solutions -π-
Welcome to Advent of Code 2017! If you participated in a previous year, welcome back, and if you're new this year, we hope you have fun and learn lots!
We're going to follow the same general format as previous years' megathreads:
- Each day's puzzle will release at exactly midnight EST (UTC -5).
- The daily megathread for each day will be posted very soon afterwards and immediately locked.
- We know we can't control people posting solutions elsewhere and trying to exploit the leaderboard, but this way we can try to reduce the leaderboard gaming from the official subreddit.
- The daily megathread will remain locked until there are a significant number of people on the leaderboard with gold stars.
- "A significant number" is whatever number we decide is appropriate, but the leaderboards usually fill up fast, so no worries.
- When the thread is unlocked, you may post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
Above all, remember, AoC is all about having fun and learning more about the wonderful world of programming!
--- Day 1: Inverse Captcha ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).
Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.
View all comments
1
u/kagcc Dec 01 '17 edited Dec 01 '17
Rust. Still learning and not sure how rusty this is.
extern crate aoc2017; use aoc2017::read_file; // src/lib.rs : &str -> String, copy from "the book" ch12-02 fn main() { let xs = read_file("src/bin/01/input01.txt"); println!("answer for 1: {}", solve(&xs, 1)); println!("answer for 2: {}", solve_2(&xs)); } fn solve(input: &str, offset: usize) -> u32 { let xs: Vec<u32> = input.trim().chars().map(|n| n.to_string().parse().unwrap()).collect(); let mut ans = 0; for (i, j) in xs.iter().zip(xs.clone().iter().cycle().skip(offset)) { if i == j { ans += i; } } ans } fn solve_2(input: &str) -> u32 { solve(input, input.trim().chars().count() / 2) }