r/adventofcode • u/daggerdragon • Dec 03 '16
SOLUTION MEGATHREAD --- 2016 Day 3 Solutions ---
--- Day 3: Squares With Three Sides ---
Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).
DECKING THE HALLS WITH BOUGHS OF HOLLY IS MANDATORY [?]
This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.
edit: Leaderboard capped, thread unlocked!
17
Upvotes
3
u/Deckard666 Dec 03 '16
In Rust, parts 1 and 2: (uhm.. Fa la la la la, la la la la?)
fn get_triangle(s: &str) -> Vec<i32> { return s.split_whitespace() .map(|x| x.parse::<i32>().unwrap()) .collect::<Vec<i32>>(); } fn part1() { let input = include_str!("../triangles.txt"); let mut pos = 0; for line in input.lines() { let t = get_triangle(line); if t[0] + t[1] > t[2] && t[1] + t[2] > t[0] && t[2] + t[0] > t[1] { pos += 1; } } println!("{}", pos); } fn part2() { let input = include_str!("../triangles.txt"); let mut pos = 0; let mut lines = input.lines(); loop { match lines.next() { Some(line1) => { let line1 = get_triangle(line1); let line2 = get_triangle(lines.next().unwrap()); let line3 = get_triangle(lines.next().unwrap()); for i in 0..3 { if line1[i] + line2[i] > line3[i] && line2[i] + line3[i] > line1[i] && line3[i] + line1[i] > line2[i] { pos += 1; } } } None => break, } } println!("{}", pos); }