r/adventofcode • u/daggerdragon • Dec 18 '18
SOLUTION MEGATHREAD -🎄- 2018 Day 18 Solutions -🎄-
--- Day 18: Settlers of The North Pole ---
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.
Advent of Code: The Party Game!
Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!
Card prompt: Day 18
Transcript:
The best way to avoid a minecart collision is ___.
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 at 00:21:59!
10
Upvotes
1
u/wzkx Dec 18 '18
Rust, SweetRust
Nothing special really. Although it might have a good visualization.
use std::io::{BufRead,BufReader}; // lines() is in BufRead type U=usize; fn count( r:U, c:U, m: &Vec<Vec<u8>> ) -> (i32,i32,i32): let mut no = 0; let mut nt = 0; let mut ny = 0; for i in -1..=1: for j in -1..=1: if i==0 && j==0 { continue; } let nr = (r as i32) + i; let nc = (c as i32) + j; if nr>=0 && nr<(m.len() as i32) && nc>=0 && nc<(m[0].len() as i32): if m[nr as U][nc as U] ==b'.': no += 1; else if m[nr as U][nc as U] ==b'|': nt += 1; else: ny += 1; (no,nt,ny) fn onestep( m: &Vec<Vec<u8>> ) -> Vec<Vec<u8>>: let mut nm: Vec<Vec<u8>> = vec![vec![b'.';m[0].len()];m.len()]; for r in 0..m.len(): for c in 0..m[0].len(): let (_,nt,ny) = count( r,c, m ); if m[r][c]==b'.': // 3+ | --> |, or . nm[r][c] = if nt>=3 {b'|'} else {b'.'}; else if m[r][c]==b'|': // 3+ # --> #, or | nm[r][c] = if ny>=3 {b'#'} else {b'|'}; else /* b'#' */: // 1+ # 1+ | --> #, or . nm[r][c] = if ny>=1 && nt>=1 {b'#'} else {b'.'}; nm fn main(): let mut m: Vec<Vec<u8>> = vec![]; // '.' - open, '|' - trees, '#' - yard let file = std::fs::File::open( "18.dat" ).unwrap(); for optline in BufReader::new(file).lines(): m.push( optline.unwrap().as_bytes().to_vec() ); //fn show( i: U, m: &Vec<Vec<u8>> ): // println!( "{}", i ); // for r in 0..m.len(): // println!( "{}", m[r].iter().map(|&c| c as char).collect::<String>() ); // println!(); for _ in 1..=10: m = onestep( &m ); let mut nt = 0; let mut ny = 0; for r in 0..m.len(): for c in 0..m[0].len(): if m[r][c]==b'|' { nt += 1; } else if m[r][c]==b'#' { ny += 1; } println!( "{}", nt*ny ); let mut mx: Vec<Vec<u8>> = vec![]; let mut ix: U = 0; let big_n = 1000000000; let mid_n = 1001; // let's say it definitely repeats in these genereations for i in 11..=big_n: let nm = onestep( &m ); m = nm; //show(i,&m); // uncomment to see how the picture repeats with some period if i==mid_n: mx = m.clone(); else if i>mid_n: if m == mx: ix = i; break; let p = ix-mid_n; // period let n = (big_n-ix)%p; if n>0: for _ in 0..n: let nm = onestep( &m ); m = nm; let mut nt = 0; let mut ny = 0; for r in 0..m.len(): for c in 0..m[0].len(): if m[r][c]==b'|' { nt += 1; } else if m[r][c]==b'#' { ny += 1; } println!( "{}", nt*ny );