Skip to content

Instantly share code, notes, and snippets.

@machalvan
Last active December 13, 2018 18:46
Show Gist options
  • Save machalvan/885556a17aefa97589a036496fc5c297 to your computer and use it in GitHub Desktop.
Save machalvan/885556a17aefa97589a036496fc5c297 to your computer and use it in GitHub Desktop.
Solutions to Advent of Code 2018, day 1, part 1 and 2 in VAR
// Solutions to Advent of Code 2018, day 1, part 1 and 2
// https://adventofcode.com/2018/day/1
//
// Written in VAR
// https://github.com/machalvan/VAR
//
// Approximate running time for large input: 5 min
var input_values 0 // Initialize variables
var counter 0
inp input // Get input line
// Iterate over the input and
// put the values in a list as
// positive and negative integers.
whl input // As long as input is not empty, enter block
var index 0
var char input[index] // Set char to be the first character in input
var plus "+"
var plus_char_diff plus // Subtract char from plus,
dec plus_char_diff char // if difference is 0, then char is a plus sign
var char_equals_plus 1 // Assume that char is a plus sign and change in next step if incorrect
var char_not_equals_plus 0
con plus_char_diff // If plus_char_diff is not 0, enter block
var char_equals_plus 0 // Char was not a plus sign, set char_equals_plus to false
var char_not_equals_plus 1
end
inc index // Move to next char
var char input[index]
whl char // For each remaining char on this line
var index_minus_one index
dec index_minus_one // Index - 1 is needed as a new index in the next step
var number[index_minus_one] char // Concatenate each char (integer) to a number variable
// Number will (eventually) contain input line without the plus/minus sign
inc index // Move to next char
var char input[index]
end
int number // Treat number as an integer
var input_value 0
con char_equals_plus // If
inc input_value number
end
con char_not_equals_plus // Else
dec input_value number
end
var input_values[counter] input_value // Add input value to list
inc counter
inp input // Get the next line
end
var frequency_zero_value 100000 // To avoid negative index values, frequency 0 is mapped to 100000
// This means that frequency -1 is mapped to 99999 and so on
var frequency_list 0
var frequency_list[frequency_zero_value] 1 // Map frequency 0 (at index 100000) to true because it is found
var part_1_result 0 // Initialize some more values
var frequency 0
var first_iteration_over_input_values 1
var lap_counter 1
var duplicate_frequency_not_found 1
// Starting with 0, each added value
// from the input list gives a new frequency.
// Iterate over the input list repeatedly
// until a frequency is found twice.
whl duplicate_frequency_not_found
var counter 0 // Reset counter
var input_value input_values[counter]
whl input_value // Enter loop if input is not empty
inc frequency input_value // Add the number to frequency to get the new frequency
var frequency_index frequency // Get the frequency index by adding 100000
inc frequency_index frequency_zero_value
con frequency_list[frequency_index] // Check if frequency has been found before
out "Result for part 1: " 0 // Print results
str part_1_result
out part_1_result
out "Result for part 2: " 0
str frequency
out frequency
var input_values 0 // Escape inner loop
var duplicate_frequency_not_found 0 // Escape outer loop
var lap_counter 0 // Do not print this last lap number
end
var frequency_list[frequency_index] 1 // Map frequency to true, because it has been found
inc counter
var input_value input_values[counter] // Get next frequency
end
con first_iteration_over_input_values // Print result for part 1 only after first iteration
var part_1_result frequency
var first_iteration_over_input_values 0
end
con lap_counter
out "Lap: " 0 // Print lap and start over with the first value in the input list
str lap_counter
out lap_counter
int lap_counter
inc lap_counter
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment