Initial commit for day 1
This commit is contained in:
commit
1bfd6c462d
14
Gemfile.lock
Normal file
14
Gemfile.lock
Normal file
@ -0,0 +1,14 @@
|
||||
GEM
|
||||
remote: https://rubygems.org/
|
||||
specs:
|
||||
|
||||
PLATFORMS
|
||||
ruby
|
||||
|
||||
DEPENDENCIES
|
||||
|
||||
RUBY VERSION
|
||||
ruby 3.0.2p107
|
||||
|
||||
BUNDLED WITH
|
||||
2.1.4
|
||||
2
gemset.nix
Normal file
2
gemset.nix
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
||||
6
nixpkgs.nix
Normal file
6
nixpkgs.nix
Normal file
@ -0,0 +1,6 @@
|
||||
let
|
||||
channel-ref = "nixos-21.05";
|
||||
in
|
||||
import (fetchTarball {
|
||||
url = "https://github.com/NixOS/nixpkgs/archive/${channel-ref}.tar.gz";
|
||||
}) {}
|
||||
2
ruby-version.nix
Normal file
2
ruby-version.nix
Normal file
@ -0,0 +1,2 @@
|
||||
with (import ./nixpkgs.nix);
|
||||
pkgs.ruby_3_0
|
||||
18
shell.nix
Normal file
18
shell.nix
Normal file
@ -0,0 +1,18 @@
|
||||
with (import ./nixpkgs.nix);
|
||||
let
|
||||
ruby_version = import ./ruby-version.nix;
|
||||
gems = bundlerEnv {
|
||||
name = "advent-of-code";
|
||||
ruby = ruby_version;
|
||||
gemdir = ./.;
|
||||
};
|
||||
in stdenv.mkDerivation {
|
||||
name = "qa-mds";
|
||||
buildInputs = [ gems gems.wrappedRuby ];
|
||||
shellHook = ''
|
||||
if ! [ $NO_CHANGE_PROMPT ]; then
|
||||
export PS1='\[\033[1;31m\]λ\[\033[0m\] '
|
||||
export PS2='\[\033[1;31m\]|\[\033[0m\] '
|
||||
fi
|
||||
'';
|
||||
}
|
||||
32
src/day1/day1.rb
Normal file
32
src/day1/day1.rb
Normal file
@ -0,0 +1,32 @@
|
||||
inputs = ARGF.read.lines
|
||||
|
||||
begin
|
||||
inputs.map! {|input| Integer(input)}
|
||||
rescue ArgumentError
|
||||
puts "An input item wasn't a number, invalid input"
|
||||
end
|
||||
|
||||
larger_than_before = 0
|
||||
before = nil
|
||||
inputs.each do |input|
|
||||
before = input if before.nil?
|
||||
larger_than_before += 1 if input > before
|
||||
before = input
|
||||
end
|
||||
|
||||
puts "Amount of measurements larger than before: #{larger_than_before}"
|
||||
|
||||
larger_than_previous_window = 0
|
||||
previous_window = nil
|
||||
window = []
|
||||
inputs.each_with_index do |input, index|
|
||||
unless index+2 >= inputs.size # Handle the last window not overreaching
|
||||
window = [inputs[index], inputs[index+1], inputs[index+2]]
|
||||
if previous_window && window.reduce(:+) > previous_window.reduce(:+)
|
||||
larger_than_previous_window += 1
|
||||
end
|
||||
previous_window = window
|
||||
end
|
||||
end
|
||||
|
||||
puts "Amount of measurements larger than preivous window: #{larger_than_previous_window}"
|
||||
2000
src/day1/input
Executable file
2000
src/day1/input
Executable file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user