<repo_name>clarkema/advent-of-code<file_sep>/2018/day_01/day_01.rb
#! ruby

require 'set'

def part_2(drifts)
    seen = Set.new
    freq = 0
    drifts.cycle do |delta|
        freq += delta
        break freq unless seen.add?(freq)
    end
end

drifts = File.new("day_01.input").each_line.map(&:to_i)

puts "Part 1: #{drifts.sum}"
puts "Part 2: #{part_2(drifts)}"
<file_sep>/2017/day_01/rust_01/Cargo.toml
[package]
name = "rust_01"
version = "0.1.0"
authors = ["<NAME> <<EMAIL>>"]

[dependencies]
<file_sep>/2018/day_01/day_01.lua
#! lua

function read_file(file)
    local lines = {}
    for line in io.lines(file) do
        lines[#lines + 1] = line
    end
    return lines
end

function sum(a)
    local acc = 0;
    for _, v in ipairs(a) do
        acc = acc + v
    end
    return acc
end

function part_2(drift)
    local seen = {}
    local acc = 0

    while true do
        for _, v in ipairs(drift) do
            acc = acc + v;
            if seen[acc] then
                return acc
            else
                seen[acc] = true
            end
        end
    end
end

drift = read_file("day_01.input")

print("Part 1: " .. sum(drift))
print("Part 2: " .. part_2(drift))
<file_sep>/2017/day_05/day_05.py
#!/usr/bin/env python3

with open('day_05.input') as input_file:
    lines = input_file.read().split("\n")
    instructions = [int(x) for x in lines]

index = 0
stop = len(instructions) - 1
i = 0

while index <= stop:
    jump = instructions[index];

    if jump >= 3:
        instructions[index] = instructions[index] - 1
    else:
        instructions[index] = instructions[index] + 1

    index = index + jump;
    i = i + 1

print(i)
<file_sep>/2018/day_01/rust_01/src/main.rs
use std::fs::File;
use std::io::{self, BufRead};
use std::collections::HashSet;

fn part1(drifts: &[i32]) -> i32 {
    drifts.iter().sum()
}

fn part2(drifts: &[i32]) -> i32 {
    let mut seen = HashSet::new();

    drifts.iter().cycle()
        .scan(0, |acc, x| {
            *acc = *acc + x;
            Some(*acc)
        })
        .skip_while(|x| { seen.insert(x.clone()) })
        .nth(0)
        .unwrap()
}

fn main() {
    let filename = "../day_01.input";

    let fh = File::open(filename).expect("Failed to open file");

    let drifts: Vec<i32> = io::BufReader::new(fh).lines()
        .map(|l| l.unwrap().parse::<i32>().unwrap())
        .collect();

    println!("Part 1: {}", part1(&drifts));
    println!("Part 2: {}", part2(&drifts));
}
<file_sep>/2018/day_01/day_01.py
#! python3

from itertools import cycle, accumulate

def part_2():
    seen = set()
    for x in accumulate(cycle(drift)):
        if x in seen:
            return x
        else:
            seen.add(x)

drift = [int(l) for l in open("day_01.input")]

print(sum(drift))
print(part_2())
<file_sep>/2017/day_01/day_01.py
#! /usr/bin/env python3

# This is a Python 3 solution to day one of 2017's Advent of Code; see
# http://adventofcode.com/2017/day/1 for the full puzzle.

import collections

def captcha_sum(digits, rotation):
    d = [int(d) for d in digits]
    n = collections.deque(d)
    n.rotate(-rotation)

    return sum([x[0] for x in zip(d, n) if x[0] == x[1]])

with open('day_01.input') as input_file:
    input = input_file.read().replace('\n', '')

print('Part 1:', captcha_sum(input, 1))
print('Part 2:', captcha_sum(input, len(input)//2))
<file_sep>/2018/day_01/nim_01/Makefile
all:
	nim compile -d:release day_01.nim
<file_sep>/2017/day_01/rust_01/src/main.rs
use std::fs::File;
use std::io::Read;

fn captcha_sum(input: &str, rotation: usize) -> u32 {
    let mut acc = 0;

    let digits = input.chars()
                    .map(|c| c.to_digit(10).unwrap())
                    .collect::<Vec<u32>>();

    for (i, c) in digits.iter().enumerate() {
        if digits[(i + rotation) % digits.len()] == *c {
            acc += *c;
        }
    }

    acc
}

fn main() {
    let filename = "../day_01.input";
    let mut input = String::new();

    let mut fh = File::open(filename).expect("file not found");

    fh.read_to_string(&mut input)
        .expect("something went wrong reading the file");

    let input = input.trim();

    println!("Part 1: {:?}", captcha_sum(&input, 1));
    println!("Part 2: {:?}", captcha_sum(&input, input.len() / 2));
}
