Last active
February 12, 2016 21:21
-
-
Save DrFrankenstein/38b77b92b7080fae7e6c to your computer and use it in GitHub Desktop.
Advent of Code 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.model flat, stdcall | |
option casemap:none | |
STD_INPUT_HANDLE equ -10 | |
STD_OUTPUT_HANDLE equ -11 | |
GetStdHandle proto stdcall, nStdHandle:dword | |
ReadFile proto stdcall, hFile:dword, lpBuffer:ptr byte, nNumberOfBytesToRead:dword, lpNumberOfBytesRead:ptr dword, lpOverlapped:ptr dword | |
WriteFile proto stdcall, hFile:dword, lpBuffer:ptr byte, nNumberOfBytesToWrite:dword, lpumberOfBytesWritten:ptr dword, lpOverlapped: ptr dword | |
FORMAT_MESSAGE_ALLOCATE_BUFFER equ 100h | |
FORMAT_MESSAGE_FROM_STRING equ 400h | |
FORMAT_MESSAGE_ARGUMENT_ARRAY equ 2000h | |
FormatMessageA proto stdcall, dwFlags:dword, lpSource:ptr byte, dwMessageId:dword, dwLanguageId:dword, lpBuffer:ptr byte, nSize:dword, varargs:dword | |
LocalFree proto stdcall, hMem:dword | |
ExitProcess proto stdcall, uExitCode:dword | |
.const | |
errInvChar db 'invalid character%n', 0 | |
errReadErr db 'cannot read input file%n', 0 | |
msgFinalFloor db 'final floor: %1!d!%n', 0 | |
msgBasement db 'entered basement at step %1!u!%n', 0 | |
.data? | |
stdout dd ? | |
stdin dd ? | |
.code | |
printf proc, format:ptr byte;,... | |
local buffer:ptr byte | |
invoke FormatMessageA, \ | |
FORMAT_MESSAGE_ALLOCATE_BUFFER or FORMAT_MESSAGE_FROM_STRING or FORMAT_MESSAGE_ARGUMENT_ARRAY, \ | |
format, 0, 0, addr buffer, 0, addr format + 4 | |
invoke WriteFile, stdout, buffer, eax, 0, 0 | |
invoke LocalFree, buffer | |
ret | |
printf endp | |
nextChar proc | |
local char:byte | |
local bytesRead:dword | |
invoke ReadFile, stdin, addr char, 1, addr bytesRead, 0 | |
.if eax == 0 | |
invoke printf, offset errReadErr | |
ret | |
.endif | |
.if bytesRead == 0 | |
xor eax, eax | |
ret | |
.endif | |
mov al, char | |
ret | |
nextChar endp | |
main proc uses ebx esi di | |
;floor = ebx | |
invoke GetStdHandle, STD_INPUT_HANDLE | |
mov stdin, eax | |
invoke GetStdHandle, STD_OUTPUT_HANDLE | |
mov stdout, eax | |
xor ebx, ebx | |
xor esi, esi | |
xor di, di | |
invoke nextChar | |
.while al != 0 | |
.if al == '(' | |
inc ebx | |
.elseif al == ')' | |
dec ebx | |
.else | |
invoke printf, offset errInvChar | |
invoke ExitProcess, 1 | |
.endif | |
.if !di && ebx == -1 | |
push esi | |
invoke printf, offset msgBasement | |
add esp, 4 | |
mov di, 1 | |
.endif | |
inc esi | |
invoke nextChar | |
.endw | |
push ebx | |
invoke printf, offset msgFinalFloor | |
add esp, 4 | |
invoke ExitProcess, 0 | |
ret | |
main endp | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
int main(void) | |
{ | |
int ch, floor = 0, been_basement = 0; | |
while ((ch = getchar()) != EOF) | |
{ | |
switch (ch) | |
{ | |
case '(': ++floor; break; | |
case ')': --floor; break; | |
default: | |
printf("Warning: unexpected character in input: %c\n", ch); | |
break; | |
} | |
if (!been_basement && floor == -1) | |
{ | |
printf("Note: first entered basement at step %ld\n", ftell(stdin)); | |
been_basement = 1; | |
} | |
} | |
if (ferror(stdin)) | |
{ | |
perror("Cannot read input"); | |
return EXIT_FAILURE; | |
} | |
printf("Floor: %d\n", floor); | |
return EXIT_SUCCESS; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Aoc1 | |
{ | |
static class Program | |
{ | |
static IEnumerable<char> Characters(TextReader input) | |
{ | |
int c; | |
while ((c = input.Read()) > 0) | |
yield return (char) c; | |
} | |
static int RunThruElevator(this IEnumerable<int> commands) | |
{ | |
int floor = 0, step = 0; | |
bool beenInBasement = false; | |
foreach (int command in commands) | |
{ | |
floor += command; | |
++step; | |
if (!beenInBasement && floor == -1) | |
{ | |
Console.WriteLine("Note: first entered basement at step {0}", step); | |
beenInBasement = true; | |
} | |
} | |
return floor; | |
} | |
static void Main(string[] args) | |
{ | |
var floor = Characters(Console.In) | |
.Select((ch) => | |
{ | |
switch (ch) | |
{ | |
case '(': return 1; | |
case ')': return -1; | |
default: return 0; | |
} | |
}) | |
.RunThruElevator() | |
; | |
Console.WriteLine("Floor: {0}", floor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment