Skip to content

Commit 6a2d8d5

Browse files
committed
added 2022/03-04, and x86 DOS assembly for 2022/02
1 parent f33ba0e commit 6a2d8d5

14 files changed

+2022
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ a.out
44
a.o
55
*vis*.p[gp]m
66
*.com
7+
*.lst
8+
*.OUT

2022/02/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ This is one of the examples where a lot of intricate rule-following needs to be
5555

5656
* Part 1, Python: 101 bytes, <100 ms
5757
* Part 2, Python: 91 bytes, <100 ms
58+
* Parts 1+2, x86 DOS Assembly: 183 bytes (assembled)

2022/02/aoc2022_02.asm

+135
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
; yasm -fbin -oaoc22_02.com aoc2022_02.asm && dosbox aoc22_02.com
2+
; yasm -fbin -oaoc22_02.com aoc2022_02.asm && dosbox -c "mount c: ." -c "c:" -c "td aoc22_02.com"
3+
4+
BITS 16
5+
CPU 8086
6+
ORG 100h
7+
8+
; open file
9+
mov ax, 3D00h ; DOS function "open file" (for reading)
10+
mov dx, file
11+
int 21h
12+
jc io_err
13+
mov bx, ax ; save file handle for next operation
14+
15+
; read file contents into the (nearly) 64k of empty space in this .COM file
16+
mov ah, 3Fh ; DOS function "read file"
17+
mov cx, 0FF00h ; read as much as we (somewhat) safely can
18+
sub cx, dx
19+
int 21h
20+
jnc io_ok
21+
io_err:
22+
mov ax, 4C01h ; DOS function "terminate program" (with exit code 1)
23+
int 21h
24+
io_ok:
25+
mov si, dx ; store address of 'file' variable for later
26+
add dx, ax ; append null terminator to end of file data
27+
mov di, dx
28+
mov byte [di], 0
29+
30+
; close file (optional - the OS will clean up our mess too, if needed :)
31+
; mov ah, 3Eh ; DOS function "close file"
32+
; int 21h
33+
34+
; now iterate over the input file
35+
nextchar:
36+
lodsb ; load a byte
37+
or al, al ; EOF reached?
38+
jz eof
39+
cmp al, 'C' ; is is possibly an XYZ value?
40+
ja .maybe_xyz
41+
sub al, 'A' ; not XYZ -> check for ABC (and map to ABC=012)
42+
cmp al, 3
43+
jae nextchar ; not a valid character
44+
inc al ; save decoded ABC value (mapped to ABC=123)
45+
mov ah, al
46+
jmp nextchar
47+
.maybe_xyz:
48+
sub al, 'X' ; check for XYZ (and map to XYZ=012)
49+
cmp al, 3
50+
jae nextchar ; not a valid character
51+
push ax ; save original result (will be clobbered in part 1 evaluation)
52+
53+
; part 1: check if we have won
54+
xor cx, cx ; reset score
55+
inc al ; remap our result to XYZ=123
56+
cmp ah, al ; draw?
57+
je .p1_draw
58+
inc ah ; correct_guess = opponents_guess + 1
59+
cmp ah, 4 ; ... with wrap-around
60+
jb .p1_nowrap
61+
sub ah, 3
62+
.p1_nowrap:
63+
cmp ah, al ; won?
64+
jne .p1_finish ; if not, just add score
65+
add cl, 3 ; add 3 (of 6) points for win
66+
.p1_draw:
67+
add cl, 3 ; add 3 points for draw (or another 3 points for win)
68+
.p1_finish:
69+
add cl, al ; add score for local player's choice
70+
add [part1], cx ; add to accumulator
71+
72+
; part 2: create a suitable score
73+
pop ax ; restore the original input data
74+
xor cx, cx ; reset score
75+
add cl, al ; ... and set to 3 * winning_indicator
76+
add cl, al
77+
add cl, al
78+
add al, 2 ; convert winning_indicator to offset: lose=2, draw=3(=0 mod 3), win=4(=1 mod 3)
79+
add al, ah ; ... and add offset to opponent's choice to get our choice
80+
.p2_wrapcheck:
81+
cmp al, 4 ; check for wrap-around
82+
jb .p2_nowrap
83+
sub al, 3
84+
jmp .p2_wrapcheck
85+
.p2_nowrap:
86+
add cl, al ; add score for local player's choice
87+
add [part2], cx ; add to accumulator
88+
89+
jmp nextchar ; continue with next input character
90+
91+
eof:
92+
mov ax, [part1]
93+
call OutputWord
94+
mov ax, [part2]
95+
; now run right into the OutputWord function which will also exit the program
96+
97+
; -----------------------------------------------------------------------------
98+
; FUNCTION: OutputWord - output a word in decimal onto the screen
99+
; in AX = word to output
100+
; clobbers BX, CX, DX
101+
OutputWord:
102+
; add terminating EOL to output stack
103+
mov bl, 0Ah
104+
push bx
105+
mov bl, 0Dh
106+
push bx
107+
108+
.nextdigit:
109+
mov bx, 10 ; common quotient
110+
xor dx, dx ; clear upper part
111+
div bx ; divide
112+
add dl, '0' ; convert remainder into ASCII
113+
push dx ; ... and store to output stack
114+
or ax, ax ; another iteration if value is still nonzero
115+
jnz .nextdigit
116+
117+
; flush output stack
118+
mov ah, 2 ; DOS function "write character"
119+
.outchar:
120+
pop dx
121+
int 21h
122+
cmp dl, 0Ah
123+
jne .outchar
124+
125+
ret
126+
127+
; -----------------------------------------------------------------------------
128+
; DATA SECTION
129+
130+
; score accumulators for parts 1 and 2
131+
part1 dw 0
132+
part2 dw 0
133+
134+
; input filename; gets overwritten with file data later
135+
file: db "INPUT.TXT", 0

2022/03/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
\--- Day 3: Rucksack Reorganization ---
2+
---------------------------------------
3+
4+
One Elf has the important job of loading all of the [rucksacks](https://en.wikipedia.org/wiki/Rucksack) with supplies for the jungle journey. Unfortunately, that Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged.
5+
6+
Each rucksack has two large _compartments_. All items of a given type are meant to go into exactly one of the two compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.
7+
8+
## Part 1
9+
10+
The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, `a` and `A` refer to different types of items).
11+
12+
The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same number of items in each of its two compartments, so the first half of the characters represent items in the first compartment, while the second half of the characters represent items in the second compartment.
13+
14+
For example, suppose you have the following list of contents from six rucksacks:
15+
16+
vJrwpWtwJgWrhcsFMMfFFhFp
17+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
18+
PmmdzqPrVvPwwTWBwg
19+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
20+
ttgJtRGJQctTZtZT
21+
CrZsJsPPZsGzwwsLwLmpwMDw
22+
23+
24+
* The first rucksack contains the items `vJrwpWtwJgWrhcsFMMfFFhFp`, which means its first compartment contains the items `vJrwpWtwJgWr`, while the second compartment contains the items `hcsFMMfFFhFp`. The only item type that appears in both compartments is lowercase _`p`_.
25+
* The second rucksack's compartments contain `jqHRNqRjqzjGDLGL` and `rsFMfFZSrLrFZsSL`. The only item type that appears in both compartments is uppercase _`L`_.
26+
* The third rucksack's compartments contain `PmmdzqPrV` and `vPwwTWBwg`; the only common item type is uppercase _`P`_.
27+
* The fourth rucksack's compartments only share item type _`v`_.
28+
* The fifth rucksack's compartments only share item type _`t`_.
29+
* The sixth rucksack's compartments only share item type _`s`_.
30+
31+
To help prioritize item rearrangement, every item type can be converted to a _priority_:
32+
33+
* Lowercase item types `a` through `z` have priorities 1 through 26.
34+
* Uppercase item types `A` through `Z` have priorities 27 through 52.
35+
36+
In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (`p`), 38 (`L`), 42 (`P`), 22 (`v`), 20 (`t`), and 19 (`s`); the sum of these is _`157`_.
37+
38+
Find the item type that appears in both compartments of each rucksack. _What is the sum of the priorities of those item types?_
39+
40+
Your puzzle answer was `8153`.
41+
42+
## Part 2
43+
44+
As you finish identifying the misplaced items, the Elves come to you with another issue.
45+
46+
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For efficiency, within each group of three Elves, the badge is the _only item type carried by all three Elves_. That is, if a group's badge is item type `B`, then all three Elves will have item type `B` somewhere in their rucksack, and at most two of the Elves will be carrying any other item type.
47+
48+
The problem is that someone forgot to put this year's updated authenticity sticker on the badges. All of the badges need to be pulled out of the rucksacks so the new authenticity stickers can be attached.
49+
50+
Additionally, nobody wrote down which item type corresponds to each group's badges. The only way to tell which item type is the right one is by finding the one item type that is _common between all three Elves_ in each group.
51+
52+
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item type. So, in the above example, the first group's rucksacks are the first three lines:
53+
54+
vJrwpWtwJgWrhcsFMMfFFhFp
55+
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
56+
PmmdzqPrVvPwwTWBwg
57+
58+
59+
And the second group's rucksacks are the next three lines:
60+
61+
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
62+
ttgJtRGJQctTZtZT
63+
CrZsJsPPZsGzwwsLwLmpwMDw
64+
65+
66+
In the first group, the only item type that appears in all three rucksacks is lowercase `r`; this must be their badges. In the second group, their badge item type must be `Z`.
67+
68+
Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (`r`) for the first group and 52 (`Z`) for the second group. The sum of these is _`70`_.
69+
70+
Find the item type that corresponds to the badges of each three-Elf group. _What is the sum of the priorities of those item types?_
71+
72+
Your puzzle answer was `2342`.
73+
74+
## Solution Notes
75+
76+
Just intersections of sets, nothing special to see here.
77+
78+
An x86 DOS implementation of this task is still perfectly in the realm of the possible; having to implement bitfield-based sets from scratch isn't _that_ much fun, though -- and it takes a surprisingly high amount of code space too.
79+
80+
* Part 1, Python: 122 bytes, <100 ms
81+
* Part 2, Python: 128 bytes, <100 ms
82+
* Parts 1+2, x86 DOS Assembly: 306 bytes (assembled)

0 commit comments

Comments
 (0)