Initial commit
This commit is contained in:
		
						commit
						4ac4bc6a58
					
				
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1,2 @@ | ||||
| cookie | ||||
| data | ||||
							
								
								
									
										86
									
								
								bin/run
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										86
									
								
								bin/run
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,86 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| # | ||||
| # check args | ||||
| # | ||||
| 
 | ||||
| if [[ $# -ne 3 ]] | ||||
| then | ||||
|    echo "usage: $0 year day" | ||||
|    exit 1 | ||||
| fi | ||||
| 
 | ||||
| # | ||||
| # check year and day | ||||
| # | ||||
| 
 | ||||
| year=$(($2)) | ||||
| 
 | ||||
| if [[ $year -gt 2015 ]] | ||||
| then | ||||
|     echo "invalid year: $2" | ||||
|     echo "must be 2015 or later" | ||||
|     exit 2 | ||||
| fi | ||||
| 
 | ||||
| day=$(($3)) | ||||
| 
 | ||||
| if [[ $day -lt 1 ]] || [[ $day -gt 25 ]] | ||||
| then | ||||
|     echo "invalid day: $3" | ||||
|     echo "must be between 1 and 25" | ||||
|     exit 2 | ||||
| fi | ||||
| 
 | ||||
| # | ||||
| # fetch input | ||||
| # | ||||
| 
 | ||||
| input_directory="../data/$year/$day" | ||||
| input_path="$input_directory/input.txt" | ||||
| 
 | ||||
| if ! [[ -f $input_path ]] | ||||
| then | ||||
|     mkdir -p $input_directory | ||||
|     input_url="https://adventofcode.com/$year/day/$day/input" | ||||
| 
 | ||||
|     cookie_path="../cookie" | ||||
|     if ! [[ -f $cookie_path ]] | ||||
|     then | ||||
| 	echo "can't fetch input from $input_url without a \`cookie\` file at \`$cookie_path\`" | ||||
| 	echo "create this file with \`$ touch $cookie_path\`, then fill it with the \`session\` cookie from your browser" | ||||
| 	exit 3 | ||||
|     fi | ||||
|         | ||||
|     curl -s -b "$(cat ../cookie)" $input_url > $input_path | ||||
| fi | ||||
| 
 | ||||
| language=$1 | ||||
| valid_languages="" | ||||
| valid_language="false" | ||||
| 
 | ||||
| cd .. | ||||
| for directory in $(ls .) | ||||
| do | ||||
|     if [[ -d $directory ]] && [[ -f $directory/bin/run ]] | ||||
|     then | ||||
| 	valid_languages="$valid_languages, $directory" | ||||
| 	if [[ $language == $directory ]]; then | ||||
| 	    valid_language="true" | ||||
| 	fi | ||||
|     fi | ||||
| done | ||||
| 
 | ||||
| if ! $valid_language ; then | ||||
|     echo "unsupported language $language" | ||||
|     echo "supported languages are ${valid_languages:2}" | ||||
|     exit 4 | ||||
| fi | ||||
| 
 | ||||
| # | ||||
| # delegate language | ||||
| # | ||||
| 
 | ||||
| cd $language | ||||
| echo "Running problem $day from year $year in $language" | ||||
| ./bin/run $year $day | ||||
							
								
								
									
										1
									
								
								fortran/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								fortran/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| problem | ||||
							
								
								
									
										36
									
								
								fortran/2015/1/problem.f90
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								fortran/2015/1/problem.f90
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,36 @@ | ||||
| program problem | ||||
|   implicit none | ||||
|   integer :: floor = 0 | ||||
|   integer :: steps = 0 | ||||
|   character(len=:), allocatable :: input | ||||
|   integer :: input_file_size, unit, i | ||||
| 
 | ||||
|   open(newunit=unit, file='../data/2015/1/input.txt', access="stream") | ||||
|     inquire(unit=unit, size=input_file_size) | ||||
|     allocate(character(len=input_file_size) :: input) | ||||
| 
 | ||||
|     read(unit) input | ||||
| 
 | ||||
|     do i=1, input_file_size | ||||
|        if (input(i:i) == "(") then | ||||
|           floor = floor + 1 | ||||
|        else | ||||
|           floor = floor - 1 | ||||
|        end if | ||||
|     end do | ||||
|     print *, "Part 1: ", floor | ||||
| 
 | ||||
|     floor = 0 | ||||
|     do i=1, input_file_size | ||||
|        if (input(i:i) == "(") then | ||||
|           floor = floor + 1 | ||||
|        else | ||||
|           floor = floor - 1 | ||||
|        end if | ||||
|        steps = steps + 1 | ||||
|        if (floor < 0) exit | ||||
|     end do | ||||
|     print *, "Part 2: ", steps | ||||
| 
 | ||||
|   close(1) | ||||
| end program problem | ||||
							
								
								
									
										6
									
								
								fortran/bin/run
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										6
									
								
								fortran/bin/run
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,6 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| year=$1 | ||||
| day=$2 | ||||
| 
 | ||||
| gfortran -o $year/$day/problem $year/$day/problem.f90 && time ./$year/$day/problem | ||||
							
								
								
									
										1
									
								
								python/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								python/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| __pycache__ | ||||
							
								
								
									
										33
									
								
								python/2015/1/problem.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								python/2015/1/problem.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,33 @@ | ||||
| import os | ||||
| 
 | ||||
| with open("../data/2015/1/input.txt", "r") as input_file: | ||||
|     floor = 0 | ||||
|     while True: | ||||
|         char = input_file.read(1) | ||||
|         if not char: | ||||
|             break | ||||
| 
 | ||||
|         if char == "(": | ||||
|             floor += 1 | ||||
|         else: | ||||
|             floor -= 1 | ||||
|     print("Part 1: {floor}".format(floor = floor)) | ||||
| 
 | ||||
|     input_file.seek(0) | ||||
|     floor = 0 | ||||
|     steps = 0 | ||||
|     while True: | ||||
|         char = input_file.read(1) | ||||
|         if not char: | ||||
|             break | ||||
| 
 | ||||
|         steps += 1 | ||||
|         if char == "(": | ||||
|             floor += 1 | ||||
|         else: | ||||
|             floor -= 1 | ||||
| 
 | ||||
|         if floor < 0: | ||||
|             break | ||||
| 
 | ||||
|     print("Part 2: {steps}".format(steps = steps)) | ||||
							
								
								
									
										6
									
								
								python/bin/run
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										6
									
								
								python/bin/run
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,6 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| year=$1 | ||||
| day=$2 | ||||
| 
 | ||||
| time python3 $year/$day/problem.py | ||||
							
								
								
									
										1
									
								
								rust/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								rust/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | ||||
| problem | ||||
							
								
								
									
										29
									
								
								rust/2015/1/problem.rs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								rust/2015/1/problem.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | ||||
| use std::fs; | ||||
| 
 | ||||
| fn main() -> Result<(), std::io::Error> { | ||||
|     let input = fs::read_to_string("../data/2015/1/input.txt")?; | ||||
| 
 | ||||
|     let mut floor = 0; | ||||
|     for c in input.chars() { | ||||
| 	match c { | ||||
| 	    '(' => { floor += 1 } | ||||
| 	    ')' => { floor -= 1 } | ||||
| 	    _ => { panic!("Invalid character") } | ||||
| 	} | ||||
|     } | ||||
|     println!("Part 1: {}", floor); | ||||
| 
 | ||||
|     floor = 0; | ||||
|     let mut steps = 0; | ||||
|     for c in input.chars() { | ||||
| 	match c { | ||||
| 	    '(' => { floor += 1 } | ||||
| 	    ')' => { floor -= 1 } | ||||
| 	    _ => { panic!("Invalid character") } | ||||
| 	} | ||||
| 	steps += 1; | ||||
| 	if floor < 0 { break } | ||||
|     } | ||||
|     println!("Part 2: {}", steps); | ||||
|     Result::Ok(()) | ||||
| } | ||||
							
								
								
									
										6
									
								
								rust/bin/run
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										6
									
								
								rust/bin/run
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,6 @@ | ||||
| #!/usr/bin/env bash | ||||
| 
 | ||||
| year=$1 | ||||
| day=$2 | ||||
| 
 | ||||
| rustc -o $year/$day/problem $year/$day/problem.rs && time ./$year/$day/problem | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user