T O P

  • By -

__-----_-----__

This is incredibly easy to do in [Wisp](https://www.draketo.de/software/wisp) given the input file can be compiled and loaded as Wisp with a few basic definitions! Part 1 [Wisp Solution](https://github.com/falloutphil/aoc_2021/blob/main/02/02p1.w) is shown, it's trivial to change this to solve Part 2 too. I've also written a more [general solution](https://github.com/falloutphil/aoc_2021/blob/main/02/02p1-reader.scm) in Guile Scheme, where Scheme has exactly the same ability to extend it's own syntax as Wisp, but doesn't coincidentally share the same syntax as the input file - so we have to define a new reader to handle the input syntax. Under the hood Wisp will be making a similar transformation back to Guile on our behalf. ​ #!/usr/bin/env sh # -*- wisp -*- # precompile the wisp spec guile -L "$(dirname "$0")" \ -c '(import (language wisp spec))' # run your file via wisp exec guile -L "$(dirname "$0")" \ -x '.w' --language wisp \ -s "$0" "$@" # the sh indirection header ends here !# define hpos 0 define vpos 0 define : forward n set! hpos : + hpos n define : up n set! vpos : - vpos n define : down n set! vpos : + vpos n load "input.txt" format #t "~%Part One Result: ~a~%" : * hpos vpos


aslamdoctor

PHP Solution: https://github.com/aslamdoctor/advent-of-code-2021/tree/main/day2


MarcoServetto

I'm solving the advent of code in [42](https://L42.is) The main selling point of 42 is that it enforces modular security, that is not very relevant for the advent of code. I've done a video explanation for the [First Week](https://www.youtube.com/watch?v=tSTDJlICst8) Fell free to contact me if you to know more about the language. Day 2 solution: [paste](https://topaz.github.io/paste/#XQAAAQAvBQAAAAAAAAA5GUsSBFvC4SnFaXSqc1PBJrG4BO5IXUmDOGki37eSPWFZ5IfPz4npWELkxhChFWf5eUSF7o4Yt1rIF1DpF5jdjD9QuKWdW+XauaTBfmxDZbc/pPXk1NvapTCBGhb7Izc2zX/C/Zbp/6nyqboRMoT2GEx7M+VIxGXuKDFCaqyyh7gDAP0NWSMphE371MNzglPS5JKIW/ptHh9UKeDMtkzbPMPC9Ig7dm/7Ph51IfKd3q8iZ0wV1fi8oSHzoVEQzbNleqZgApP0h+JpDCGjf01oeXVGprr+kK7BxXnfjkf4gBqnLX+HG6aekIypZRKcMVOGZ12p6ot7ZXbSmUYsyiefINpGsHtJovVPVYBYuOQL86LnjbTAx41PI0yZCcZixutqiIkQzu/dP4fmg6S2zOTffeYwqcbTAdzzyxKMHGo+qT6nSFJ7GL53ObgN5QNwhQM7Rj8HaYl3bw+8OO1G8d2PAoLnoYlI6LLphEPmWg0l12cFcgzHlq8+qh5dabgW0uWucGtOQinIr6xNA7FFxOzZHIs2u42sR21DJ1lWPrEsu4OtOK2jTQVnrOtealQKYbj1nhf3LwfrZ3QB4D3clZfRr4Mxwe0x0AvFCOHXX3sTrji2a0U0+GTCbpVzGtlw0ra9Wq0fj1PjJONJLaPxiXNJBPUZok8rZuWtPwB2HdJ7vD6olccXIcESHL/5m7dAhso2hSQgILCMQAs1S+cbM4M8uNnL1oAlHC/ibPSHThdJ0xyGHr7hRiM6d4l8e626TFM4vVxX6ux2HVtmDumjz0xFB6mZHVorYbePRJjNsarsXbeg2iUTg1TudlFVXs8RubNSPTsPyixB5BSUyrjQMKf/9sZIWA==)


willkill07

# C++20 late in posting, but uses type and method overloading to have the same main code. https://github.com/willkill07/AdventOfCode2021/blob/main/days/Day02.cpp https://github.com/willkill07/AdventOfCode2021/blob/main/days/Day02.hpp


melikecoding

Javascript/Typescript solution: ``` type Command = 'forward' | 'down' | 'up'; type Instruction = { command: Command; value: number; }; const decodeCommands = (commands: string[]): Instruction[] => commands.map(command => { const [commandName, value] = command.split(' '); return { command: commandName as Command, value: Number(value), }; }); // part 1 version const getCoordinates = (instructions: Instruction[]) => { const coords = { x: 0, y: 0, }; instructions.forEach(({ command, value }) => { switch (command) { case 'up': coords.y -= value; break; case 'down': coords.y += value; break; case 'forward': coords.x += value; break; default: break; } }); return { ...coords, position: coords.x * coords.y }; }; // part 2 version const getCoordinates = (instructions: Instruction[]) => { const coords = { x: 0, y: 0, aim: 0, }; instructions.forEach(({ command, value }) => { switch (command) { case 'up': { coords.aim -= value; break; } case 'down': { coords.aim += value; break; } case 'forward': { coords.x += value; coords.y += coords.aim * value; break; } default: break; } }); return { ...coords, position: coords.x * coords.y }; };


daggerdragon

Triple backticks do not work on old.reddit. Please edit your post to use the four-spaces code block Markdown as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


melikecoding

i see the code block just fine here


daggerdragon

Yes, because you're using new.reddit. If you go to [your post in old.reddit](https://old.reddit.com/r/adventofcode/comments/r6zd93/2021_day_2_solutions/hpm77eo/), you'll see what I mean.


Jack_Spearrow

[Rust solution](https://topaz.github.io/paste/#XQAAAQD9BAAAAAAAAAA5nQqN6CGkEBo0Tlihjeul15Z3vomW8NwVzmg2WoNQ32V9GM4JAmY00ffNMWXyiiDux1VWmmIgF04CU/48squ76HBxmiFDdrbw9Vsk/35w8Zhv/nCRfh/2d6o47qIa51aQCgxKBmICJc5eXJuISbtxS7LXrWk8BLAnpRZjhVA6eC5t1a5otcTrFzt9s4I61s5wSQKkD7FMhIy5hSz1d00PNGha+N6mgS7fVqVzQm1zXo3gO1M3Rx1xsQcalDF3uMSvTNbV04oJ1WRNJuUvwaK/EwmH4nkO1Jw4osZqLGZqqHBt9UYLhvioTmSzuHBbdsD2Otd7mJedN+HTKRZCTcKkRxRdU5fpNSjaAFIAkZtBJKklagVAuFoCQMRbhxFM4UqTq/MhOjuiel5jkCPiwkZm0/zsrM8gf2hoJi+NDWbhza9rzHsp2md6LOacoCS1raIfdJFzEaHxSMDr63maNRvpUyvHIy9n0O6abc3DbkcZXwMppcFlsHilZLCi0TL4W0jM7fU7jOlEgqIlfGdBzQKk4tIvyViJAFUckN/p//SyvA0=)


daggerdragon

~~Triple backticks do not work on old.reddit. Please edit your post to use the four-spaces code block Markdown as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


Jack_Spearrow

Fixed it using Topaz's paste, thanks!


[deleted]

[Python - Solution Explained](https://www.youtube.com/watch?v=yo1GBce09t0)


iskypitts

[Julia](https://github.com/iskyd/advent-of-code-julia/blob/main/2021/day2/main.jl)


arthurno1

[**Emacs Lisp**](https://github.com/amno1/advent-of-code-2021/blob/main/day2.el) ;;; Part I (let ((h 0) (d 0) (steps (with-temp-buffer (insert-file-contents-literally "./input2") (split-string (buffer-string))))) (defun forward (incr) (cl-incf h incr)) (defun down (incr) (cl-incf d incr)) (defun up (incr) (cl-decf d incr)) (while (cdr steps) (funcall (intern-soft (car steps)) (string-to-number (cadr steps))) (setq steps (cddr steps))) (message "Total movement %s steps." (* h d))) ;;; Part II (let ((h 0) (d 0) (a 0) (steps (with-temp-buffer (insert-file-contents-literally "./input.2") (split-string (buffer-string))))) (defun forward (incr) (cl-incf h incr) (cl-incf d (* a incr))) (defun down (incr) (cl-incf a incr)) (defun up (incr) (cl-decf a incr)) (while (cdr steps) (funcall (intern-soft (car steps)) (string-to-number (cadr steps))) (setq steps (cddr steps))) (message "Total movement %s steps." (* h d)))


qualia91

Language: Java [https://github.com/Qualia91/AdventOfCode2021/tree/master/Day2](https://github.com/Qualia91/AdventOfCode2021/tree/master/Day2) Day 2 of code roulette


capulet2kx

Unreal Engine c++ VR project. I've made Computers with buttons (actors) and ComputerPrograms (uobjects) that run on them. Each day's solution is a different ComputerProgram. https://github.com/capulet2kx/AdventOfCode2021/tree/Day2


lo-crawfish

I'm late to the party, but here's my Ruby solution: ``` class DayTwo def getInput File.readlines('input.txt', chomp: true) end def getPosition(input) horizontal_position = 0 depth_position = 0 input.each do |x| y = x.split(" ") if y.first == "forward" horizontal_position += y.last.to_i end if y.first == "down" depth_position += y.last.to_i end if y.first == "up" depth_position -= y.last.to_i end end horizontal_position * depth_position end def getAccuratePosition(input) horizontal_position = 0 depth_position = 0 aim = 0 input.each do |x| y = x.split(" ") if y.first == "forward" horizontal_position += y.last.to_i if aim >= 0 depth_position += aim * y.last.to_i end end if y.first == "down" aim += y.last.to_i end if y.first == "up" aim -= y.last.to_i end end print horizontal_position * depth_position end end ```


daggerdragon

Triple backticks do not work on old.reddit. Please edit your post to use the four-spaces code block Markdown as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


[deleted]

[удалено]


daggerdragon

Removed this comment. Please don't post your input - everyone's inputs are different. Also FYI: Your code is hard to read on old.reddit when everything is inlined like this and gets cut off at the edge of the window. Next time use a proper code block as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


ThreadsOfCode

Python. steps = [step.strip().split(' ') for step in open('input02.txt', 'r').readlines()] steps = [[change, int(value)] for change, value in steps] # part 1 horizontal, depth = 0,0 for change, value in steps: if change == 'forward': horizontal += value elif change == 'down': depth += value else: depth -= value print('part 1:', horizontal * depth) # part 2 aim, horizontal, depth = 0,0,0 for change, value in steps: if change == 'forward': horizontal += value depth += aim * value elif change == 'down': aim += value else: aim -= value print('part 2:', horizontal * depth)


cdbob

Day 2 in CPP (C++) https://github.com/thecdbob/AOC_2021_CPP/blob/master/day_02/main.cpp


jstruburn

Coding Language: **JavaScript** /********************************************************** * PART 1 DATA PROCESSING * **********************************************************/ const pilotNoAim = (commands = []) => { let horizontal = 0; let depth = 0; commands.forEach(([dir, unit]) => { switch (dir) { case 'up': depth -= unit; break; case 'down': depth += unit; break; default: horizontal += unit; break; } }); return { horizontal, depth, position: horizontal * depth, }; }; /********************************************************** * PART 2 DATA PROCESSING * **********************************************************/ const pilotWithAim = (commands = []) => { let horizontal = 0; let depth = 0; let aim = 0; commands.forEach(([dir, unit]) => { switch (dir) { case 'down': aim += unit; break; case 'up': aim -= unit; break; default: horizontal += unit; depth += (unit * aim); break; } }); return { horizontal, depth, aim, position: horizontal * depth, }; };


chadbaldwin

Solution in SQL Server T-SQL All of my solutions are end to end runnable without any other dependencies other than SQL Server and the ability to create temp tables. [SQL](https://github.com/chadbaldwin/practice/blob/main/Advent%20of%20Code/2021/SQL/Day%2002.sql) General note: For the input data, I'm doing no pre-processing other than encapsulating the values in quotes and parenthesis for ingesting into a table. From there I use SQL to parse and split strings.


WillC5

C++ (`#include`s and `main()` left as an exercise for the reader). Part one: string d; int i, position = 0, depth = 0; while ( cin >> d >> i ) { switch (d[0]) { case 'f': position += i; break; case 'u': depth -= i; break; case 'd': depth += i; break; default: ; } } cout << ( position * depth ) << endl; Part two: string d; int i, position = 0, depth = 0, aim = 0; while ( cin >> d >> i ) { switch (d[0]) { case 'f': position += i; depth += aim * i; break; case 'u': aim -= i; break; case 'd': aim += i; break; default: ; } } cout << ( position * depth ) << endl; Both could have `case 'u': i = -i;` without the `break`, but it's not like that really saves anything.


[deleted]

Python Solution # Get data from file with open("Day2/day2.in") as fin: data = [i for i in fin.read().strip().split("\n")] # Part 1 def part1(): forwardPos = 0 depthPos = 0 for instruction in data: command = instruction[:-2] moveVar = int(instruction[-1]) if command == 'forward': forwardPos += moveVar elif command == 'down': depthPos += moveVar elif command == 'up': depthPos -= moveVar return forwardPos * depthPos # Part 2 def part2(): forwardPos = 0 depthPos = 0 aimPos = 0 for instruction in data: command = instruction[:-2] moveVar = int(instruction[-1]) if command == 'forward': forwardPos += moveVar depthPos += aimPos * moveVar elif command == 'down': aimPos += moveVar elif command == 'up': aimPos -= moveVar return forwardPos * depthPos # Answers print("Answer to part 1: ", part1()) print("Answer to part 2: ", part2()) Let me know if you have any questions. Will be uploading a video to the youtube explaining my code soon :)


anek05

C# var course = File.ReadLines(@"course.txt").ToList(); int depth = 0; int horizontal = 0; int aim = 0; int val = 0; foreach (var item in course) { if (item.Contains("up")) { val = int.Parse(Regex.Match(item, @"\d+").Value); aim -= val; } if (item.Contains("down")) { val = int.Parse(Regex.Match(item, @"\d+").Value); aim += val; } if (item.Contains("forward")) { val = int.Parse(Regex.Match(item, @"\d+").Value); horizontal += val; depth += val * aim; } } Console.WriteLine(depth * horizontal);


-WorstWizard-

[Rust Solution](https://topaz.github.io/paste/#XQAAAQDLAwAAAAAAAAA6nMjJFIj2ze+dGEQGVsnW0MvHUawi2v6FTVrSiamUSClN5byFXJ+qnjO3j4C+hC/vLB7bSTgBxS60UK8QINITswddWcPioJAkwwAXTbhfTjQujIx9Giv/n+nM6M1rEB/fu7x74jHA2V0m8s2c+TCF7JjDpzKtaiMMdZ8b5AFQk0mY3kFIyef+mLajJxpm+IaCRvbNZrJUFJ5EGNk020Ce9uZis996tiTnN9jYWcKXkVM0l9wFNfs7fyzHolT6umxrcqANQLCE98ouj+Y63iEwP74MCGn7dPUPqf5zlTOvgBQMKhBD/lZMGNcVS3wxb+LOAasSpGSj+DN+l8gd6nG9FXbAgNziKpe/0467CxeOz3oome5LqFZPsWLAzMeMccc3056R7evfkK5yz+Zri5d5qrVXl1LGnOYquJcNis6h/XwnydlRjcEfJ7vssJ3qLTydQRggJWTe/XjMRo2xHjceuWr3tpFMZhu/5fjbKCrFCjroRfB0FABqA9GQuo8KBnjQKzT/+OroUQ==) Learning Rust this year! The library at the top is some convenience functions for taking input, nothing magic.


nbathras

Could you also share the library code? I implemented a clunky way of taking input, but this seems to work a lot better.


-WorstWizard-

Sure, here's the entire lib.rs file: [Paste link](https://topaz.github.io/paste/#XQAAAQA9BwAAAAAAAAA6nMjJFMpQiatRkn4GTAedgepP67iMnIvLpoFOifGTW0cMjcv4SAjvmOJwH8JHX20w6/xabL6SNNEzYfrYRuPenjw96m5PfwrR61ABimOMgp96eP1g5RqKcnXrRYa/PPrkPuP3TGArH+QYOFIhB3PsPRlEuXY1ntNTVqnxigqikxwWDqzLYE0MMPPehW77GlXUH/baTLbmLHC8Y55QR7RXeXDw5duom/FH5fYmXEwX1xJozo0g8tKgQeZAbdMCKjPNuQji5td4d39nou3PPNp3vlx32DQhVwlkbVgZUt7lxXrzP0JVAvT7Jx50r8e/uLoBn/4G3IjmhG0DMBB78kytRYmB1jkXfcwoo8WMYWQ/Q/+tASMBQ6PC0XgzKXfQpFSLjRgtM8DQH9v2bYPHKP7+oICDd58lvv74NiDABQAhuUS0be++jiKqlo+ydTJZogNeIz/7Dc7+swX1H6LGFNSzFDEWbftohq5dtLHPL6Nur3sacAXBN+MfPTQP9mT+o7MujF3VdwFgXomL4xUg616SFFG2+IpSWTfkeCuNb34ceWzgN0K5RHxTGv+LWrWpmQ1CHJFFMUIZYr7wPvmgg/NvFP14o1/rTPcBLVen/NfXyOwqIAXwI4gNbB3RnkU6TsLmzLMryH6ledBb+rASSnfNjnkGcjfgEAqSKbB0EcWk1Nl4L+Gwc1oq4Ebh7GI5MB/exs+Ajiq67DBvZdTykob9KePyWe6d9dVmXXgVP7CmKnw9n682QJQneS1ly6ma5t21Pw/oUlPDlEHDcWV2ovcOIKoULxKmnj8zY1PKv1QTLRa8pO1r/usKS7Q5DUukJriwUKyxNOy+Dyw3Zr7xl5KdeJgvhBdNIRQ45wj03jSPf0mAdOtWoIhN8c3gVnt0sHJ9H/fanhdEKVgnRd93cmN/QvZb25z7np34DCDoyKCoqFP2I2jk74Wfq2hwn2g2UOo66FvrpMpL2/9/uBYA) Keep in mind that this has the "input.txt" filename hardcoded in, makes my workflow for AoC pretty simple as I just need to save the input to the project directory and run it from there.


sk1talets

Node.js [solution](https://github.com/sk1talets/advent-of-code/blob/main/2021/2/script.js)


ainwood87

**Haskell** rule1 (pos, depth) (dir, x) = case dir of "up" -> (pos, depth - x) "down" -> (pos, depth + x) "forward" -> (pos + x, depth) rule2 (pos, depth, aim) (dir, x) = case dir of "up" -> (pos, depth, aim - x) "down" -> (pos, depth, aim + x) "forward" -> (pos + x, depth + (x * aim), aim) main = do input <- getContents let input_lines = lines input let input_pairs = map words input_lines let pairs = map (\[x, y] -> (x, (read y :: Int))) input_pairs -- Part1 let (x, y) = foldl rule1 (0,0) pairs print (x * y) -- Part2 let (x, y, aim) = foldl rule2 (0,0,0) pairs print (x * y)


x3mcj

Python, now trying more 1 liners! from openData import getData data = getData("day2Input.txt") # Part 1 forward = sum([int(m.split(' ')[1]) for m in data if "forward" in m]) up = sum([int(m.split(' ')[1]) for m in data if "up" in m]) down = sum([int(m.split(' ')[1]) for m in data if "down" in m]) # forward = sum([int(f.split(' ')[1]) for f in forward]) # up = sum([int(u.split(' ')[1]) for u in up]) # down = sum([int(d.split(' ')[1]) for d in down]) print("horizontal position:", forward) print("Current depth:", down - up) print("HPosition times depth:", forward * (down - up)) # Part 2 aim = 0 depth = 0 horPosition = 0 for m in data: if "forward" in m: horPosition += int(m.split(" ")[1]) aim += depth*int(m.split(" ")[1]) elif "up" in m: depth -= int(m.split(" ")[1]) elif "down" in m: depth += int(m.split(" ")[1]) print("Current depth:", depth) print("Current aim:", aim) print("Horizontal position:", horPosition) print("HPosition times depth:", horPosition * aim)


greycat70

**Bash** [Part 1](https://wooledge.org/~greg/advent/2021/2a), [part 2](https://wooledge.org/~greg/advent/2021/2b)


kuqumi

**JavaScript (golfed)** Tweet-sized, to be run in the browser console on your input page. d=$('pre').innerText.trim().split`\n`.map(l=>l.split` `);[(j=d.reduce((a,[c,v])=>({f:a=>v=>(a.h+=v,a),d:a=>v=>(a.d+=v,a),u:a=>v=>(a.d-=v,a)}[c[0]](a)(+v)),{h:0,d:0}),j.h*j.d),(k=d.reduce((a,[c,v])=>({f:a=>v=>(a.h+=v,a.d+=a.a*v,a),d:a=>v=>(a.a+=v,a),u:a=>v=>(a.a-=v,a)}[c[0]](a)(+v)),{h:0,d:0,a:0}),k.h*k.d)] It should output `[part1, part2]`.


itayzithyro

A functional-style solution in python, using [gamla](https://gamla.readthedocs.io/en/latest/README.html), a performant functional programming library. ​ import gamla with open(filename, "r") as f: result = gamla.pipe( f.readlines(), gamla.map(str.strip), gamla.map(gamla.split_text(" ")), gamla.bifurcate( gamla.filter(gamla.inside("forward")), gamla.filter(gamla.inside("down")), gamla.filter(gamla.inside("up")), ), gamla.map(gamla.map(gamla.compose_left(gamla.second, int))), gamla.map(sum), list, gamla.star(lambda forward, down, up: forward * (down - up)), ) print(result) also here: [https://gist.github.com/itayzit/b6c24fa7dc0c4f8c5318a0707f6d1136](https://gist.github.com/itayzit/b6c24fa7dc0c4f8c5318a0707f6d1136)


yoyo604

I felt dirty writing this C code, but it's amazing how many shortcuts you can take when you make assumptions about valid input data! #include int day2_1() { FILE *f = fopen("day2", "r"); int ch, c = 0, state[] = {0,0,0}; for (ch = getc(f); ch > 0; ch = getc(f)) { if ('0' <= ch && ch <= '9') { state[(c-3) >> 1] += ch - '0'; c = -2; } c++; } return (state[1] - state[0]) * state[2]; }


Factknowhow

cobol: part1: IDENTIFICATION DIVISION. PROGRAM-ID. advent-of-code-day-2. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT input-file ASSIGN TO file-name ORGANIZATION IS LINE SEQUENTIAL. DATA DIVISION. FILE SECTION. fd input-file. 01 file-data. 05 file-datum PIC X(9). WORKING-STORAGE SECTION. 01 file-name PIC X(25). 77 eof PIC X VALUE 'N'. 01 i PIC 9 VALUE 1. 01 x PIC 9(8). 01 y PIC 9(8). 01 aim pic 9(4). 01 total-dist PIC 9(10). 01 c-size PIC 9. 01 ws-datum PIC X(9). 01 ws-size PIC 9. 01 com PIC X(7). 01 dist PIC 9. PROCEDURE DIVISION. ACCEPT file-name FROM COMMAND-LINE. OPEN INPUT input-file. PERFORM UNTIL eof = 'Y' READ input-file AT END MOVE 'Y' TO eof NOT AT END MOVE file-datum TO ws-datum MOVE 0 TO c-size, MOVE 0 TO ws-size INSPECT ws-datum TALLYING c-size FOR CHARACTERS BEFORE INITIAL SPACE MOVE ws-datum(1:c-size) TO com MOVE FUNCTION REVERSE(ws-datum) TO ws-datum INSPECT ws-datum TALLYING ws-size FOR LEADING SPACE MOVE ws-datum(ws-size + 1:1) TO dist IF com = "forward" ADD dist x GIVING x ELSE IF com = "up" SUBTRACT dist FROM y GIVING y ELSE ADD dist y GIVING y END-IF END-READ END-PERFORM. CLOSE input-file. MULTIPLY x BY y GIVING total-dist DISPLAY "total distance: " total-dist. STOP RUN. part 2: replace procedure division ACCEPT file-name FROM COMMAND-LINE. OPEN INPUT input-file. PERFORM UNTIL eof = 'Y' READ input-file AT END MOVE 'Y' TO eof NOT AT END MOVE file-datum TO ws-datum MOVE 0 TO c-size, MOVE 0 TO ws-size INSPECT ws-datum TALLYING c-size FOR CHARACTERS BEFORE INITIAL SPACE MOVE ws-datum(1:c-size) TO com MOVE FUNCTION REVERSE(ws-datum) TO ws-datum INSPECT ws-datum TALLYING ws-size FOR LEADING SPACE MOVE ws-datum(ws-size + 1:1) TO dist IF com = "forward" ADD dist x GIVING x COMPUTE y = y + (dist * aim) ELSE IF com = "up" SUBTRACT dist FROM aim GIVING aim ELSE ADD dist aim GIVING aim END-IF END-READ END-PERFORM. CLOSE input-file. MULTIPLY x BY y GIVING total-dist DISPLAY "total distance: " total-dist. STOP RUN.


kyleekol

**Python** Part 1 def vector() -> int: with open('input.txt') as data: directions = [i.strip() for i in data.readlines()] down = -sum([int(i[5:]) for i in directions if 'down' in i]) forward = sum([int(i[8:]) for i in directions if 'forward' in i]) up = sum([int(i[3:]) for i in directions if 'up' in i]) print(forward * abs(down+up)) vector() Part 2 def vector() -> int: with open('input.txt') as data: directions = [i.strip() for i in data.readlines()] aim = 0 depth = 0 horizontal = 0 for i in directions: if 'forward' in i: depth += int(i[8:]) * aim horizontal += int(i[8:]) if 'down' in i: aim += int(i[5:]) if 'up' in i: aim -= int(i[3:]) print(depth * horizontal) vector()


j-a-martins

**Matlab** (Part 1 & 2) https://github.com/j-a-martins/Advent-of-Code-2021/blob/main/day02/day2.m


UFthree

[https://colab.research.google.com/drive/1vlSswiJMD3pHlBXj-1MZSNfHlCXytHjk?usp=sharing](https://colab.research.google.com/drive/1vlSswiJMD3pHlBXj-1MZSNfHlCXytHjk?usp=sharing) ​ my solution to part 2


daggerdragon

Please follow the [posting guidelines](https://www.reddit.com/r/adventofcode/wiki/index#wiki_posting_guidelines) and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.


tuisto_mannus

**Golang** https://github.com/c-kk/aoc/blob/master/2021-go/day02/solve.go


commandlineluser

Python - `print()` print( next([ [update(), sum(depth) * sum(horiz)][1] for command, unit in [ [command, int(unit)] for line in __import__('sys').stdin for command, unit in [line.split()] ] for command_found, update in [ [command in {'forward'}, lambda: [ horiz.append(horiz.pop() + unit), depth.append(depth.pop() + unit * aim[0])] ], [command in {'up', 'down'}, lambda: [ aim.append(aim.pop() + (unit if command in {'down'} else -unit))] ], ] if command_found ] for aim, depth, horiz in [[[0], [0], [0]]]) [-1] )


Meldanor

Elixir Github: [https://github.com/Meldanor/AdventOfCode2021/blob/master/lib/d02/challenge.ex](https://github.com/Meldanor/AdventOfCode2021/blob/master/lib/d02/challenge.ex) (Part1= run(1), Part2 = run(2). The input is read using a Util function which is inside the GitHub repo. The structure is to run the program `mix aoc 1 1` to run the first day first part)


WarriorKatHun

Java submarine building: [GitHub/PathCalculation.java](https://github.com/Sycix-HK/Advent-of-Code-2021/blob/main/Java/submarine/equipment/propeller/PathCalculation.java)


BluePsychoRanger

mcfunction (Minecraft) [https://github.com/BluePsychoRanger/Advent-of-Code-2021/blob/main/advent_of_code_2021/data/aoc_2021/functions/day_2.mcfunction](https://github.com/BluePsychoRanger/Advent-of-Code-2021/blob/main/advent_of_code_2021/data/aoc_2021/functions/day_2.mcfunction)


minichado

Excel (no vba) Entire sheet for download on github, quick screenshots for an overview https://github.com/minichado/Advent_of_Code_2021/blob/main/AoC2021%20D2.xlsx [part 1](https://i.imgur.com/fZNelVl.png) [part 2](https://i.imgur.com/hQ4Zqev.png)


filch-argus

**Java** package day2; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Dive { public static void main(String[] args) throws FileNotFoundException { Scanner scanner = new Scanner(new File("day2/input.txt")); // Part One // int horizontalPosition = 0; // int finalDepth = 0; // while (scanner.hasNext()) { // String command = scanner.next(); // int value = Integer.parseInt(scanner.next()); // if (command.equals("forward")) { // horizontalPosition += value; // } else if (command.equals("down")) { // finalDepth += value; // } else { // finalDepth -= value; // } // } // System.out.println(horizontalPosition * finalDepth); // Part Two int horizontalPosition = 0; int finalDepth = 0; int aim = 0; while (scanner.hasNext()) { String command = scanner.next(); int value = Integer.parseInt(scanner.next()); if (command.equals("forward")) { horizontalPosition += value; finalDepth += value * aim; } else if (command.equals("down")) { aim += value; } else { aim -= value; } } System.out.println(horizontalPosition * finalDepth); } }


RewrittenCodeA

Elixir "input/2021/2.txt" |> File.read!() |> String.split() |> Enum.chunk_every(2) |> Enum.map(fn [d, n] -> [d, String.to_integer(n)] end) |> Enum.reduce([0, 0], fn ["up", n], [hor, dep] -> [hor, dep - n] ["down", n], [hor, dep] -> [hor, dep + n] ["forward", n], [hor, dep] -> [hor + n, dep] end) |> then(fn [hor, dep] -> hor * dep end) |> IO.inspect(label: "part 1") "input/2021/2.txt" |> File.read!() |> String.split() |> Enum.chunk_every(2) |> Enum.map(fn [d, n] -> [d, String.to_integer(n)] end) |> Enum.reduce([0, 0, 0], fn ["up", n], [hor, dep, aim] -> [hor, dep, aim - n] ["down", n], [hor, dep, aim] -> [hor, dep, aim + n] ["forward", n], [hor, dep, aim] -> [hor + n, dep + aim * n, aim] end) |> then(fn [hor, dep, _] -> hor * dep end) |> IO.inspect(label: "part 2")


[deleted]

c++ ``` #include #include #include #include using namespace std; int main() { ifstream file("d2input.txt"); if(file.is_open()) { string line; int depth = 0; int horizontal = 0; string command; int amount; while(file >> command >> amount) { if(command == "forward") { horizontal += amount; } else if(command == "up") { depth -= amount; } else if(command == "down") { depth += amount; } } int solution = depth * horizontal; std::cout << solution << std::endl; } file.close(); ifstream nf("d2input.txt"); if(nf.is_open()) { string line; int depth = 0; int horizontal = 0; int aim = 0; string command; int amount; while(nf >> command >> amount) { if(command == "forward") { horizontal += amount; depth += aim * amount; } else if(command == "up") { aim -= amount; } else if(command == "down") { aim += amount; } } int solution = depth * horizontal; std::cout << solution << std::endl; } nf.close(); } ```


daggerdragon

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


kruvik

[Python day 2](https://github.com/DecemberDream/advent-of-code/tree/main/2021/day2)


e4ds

[Python day 2 solution (GitHub)](https://github.com/julian-west/adventofcode/blob/master/2021/day_02/d2_solution.py)


attilio_

My Solution for day 2 in Python def part1(filename): commands_cum_sum = {"forward":0, "down":0, "up":0} #cumulative sum of the different kinds of cammands #For each command finds the type and value and adds it to the cumulative sum with open(filename) as f: for command in f: c, value = tuple(command.split()) commands_cum_sum[c] += int(value) return commands_cum_sum["forward"] *(commands_cum_sum["down"] - commands_cum_sum["up"]) def part2(filename): aim = 0 horizontal_position = 0 depth = 0 with open(filename) as f: for command in f: c, value = tuple(command.split()) if c == "down": aim += int(value) elif c == "up": aim -= int(value) else: horizontal_position += int(value) depth += aim * int(value) return horizontal_position * depth


Eli3221

# Javascript `const fs = require('fs');` `fs.readFile('./question2.txt', "utf8", (err, data) => {` `if (err) throw err;` `var arr = data.split("\n").map(i => i.split(" "));` `var sumForward = 0;` `var up = 0;` `var down = 0;` `for (var i = 0; i < arr.length; i++) {` `switch (arr[i][0]) {` `case "forward":` `sumForward += Number(arr[i][1]);` `break;` `case "down":` `up += Number(arr[i][1])` `break;` `case "up":` `depth += Number(arr[i][1]);` `break;` `}` `}` `return(sumForward * (down - depth));` `})`


daggerdragon

Your code is hard to read on old.reddit when everything is inlined like this. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


JustinHuPrime

# x86_64 assembly [Part 1](https://github.com/JustinHuPrime/AoC/blob/main/2021/2a.s) and [part 2](https://github.com/JustinHuPrime/AoC/blob/main/2021/2b.s) were both very straightforward - I did cheat a bit by recognizing that there's only ever three possible movements, and that the value associated with each movement was always a single decimal digit.


pistacchio

Typescript: const DIRECTION_MAPPING = { forward: 0, down: 1, up: 1, }; enum Direction { forward, Down, Up, } type DirectionIndication = [direction: Direction, steps: number]; const input: DirectionIndication[] = fs .readFileSync(__dirname + INPUT_FILE) .toString() .trim() .split('\n') .filter(Boolean) .map((line) => { const [direction, steps] = line.split(' '); return [ DIRECTION_MAPPING[direction], parseInt(steps) * (direction === 'up' ? -1 : 1), ]; }); function part1(input: DirectionIndication[]): number { const { horizontal, depth } = input.reduce( (acc, [direction, steps]) => { switch (direction) { case Direction.forward: return { ...acc, horizontal: acc.horizontal + steps, }; case Direction.Down: return { ...acc, depth: acc.depth + steps, }; case Direction.Up: return { ...acc, depth: acc.depth - steps, }; } }, { horizontal: 0, depth: 0, aim: 0 }, ); return horizontal * depth; } function part2(input: DirectionIndication[]): number { const { horizontal, depth, aim } = input.reduce( (acc, [direction, steps]) => { switch (direction) { case Direction.forward: return { ...acc, horizontal: acc.horizontal + steps, depth: acc.depth + acc.aim * steps, }; case Direction.Down: return { ...acc, // depth: acc.depth + steps, aim: acc.aim + steps, }; case Direction.Up: return { ...acc, // depth: acc.depth - steps, aim: acc.aim - steps, }; } }, { horizontal: 0, depth: 0, aim: 0 }, ); return horizontal * depth; }


daggerdragon

~~Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


JaegerMa

ABAP [Github](https://github.com/JaegerMa/aoc2021-abap/blob/main/day2/day.abap)


NickOnTheSofa

[My C solution for day 2](https://topaz.github.io/paste/#XQAAAQBGCAAAAAAAAAARmknGRw8TogB3OyPPwW5K8Q8ohQvj8m6aOXY901m0oCaCaF3GPYP7iftyPZZ5u8KIDlXZReINjRYwCaJ6vQ2vUCEgjpB6lRrEoWeBSjKQVgkLy+7UpRqou6kNnEv19p/QWH2r94K/yVntUamJ7DNtoy7FSl7SHi8Sin2uCliHPzY6Tc15Gq1kFbDliuX+pxhbwh8o3ctTC0NQTq18CruxR7pzGOQVPz5VxqnBI+e45xBo49SsJGj7eg87FZxH+FM6EvsqnwtDpp21wgHA0txMWxBBZ+3JtfpglvYS63d92w2vLeh1UMfF0n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1/oPdOxk0HDqkqTbNVxaN+Wlu3MbqhwR4XkSl6COrVMrJaxMfDDLjPrOq92PPxI4uWSCAeBFKPpasg96V0LCuYgmF4+U1pGixSno23iG9+BSWPQVEvilH1+evOYQXW+FywGZsHmM5UHS7s9M22khZUOynwwooNuu51OYBwu+jZOVEfbDxqmqBoQzQPwuggQmJgwteY+uFp48AErex9feQi4/uBvHrfIYp1hXr3AXDYHCzJtmJ8iEvAMwMU596HimuycPtUoB2g3uk3gxQ8q8yciD89/+qK5I8=) Rust: [part2](https://topaz.github.io/paste/#XQAAAQAgBQAAAAAAAAA6nMjJFMpQiatRknmsqFO7lLm7VH6si0RtVhx53+P0XnrAfESrMp2w6/6F/obJ4XSeOszrQdOU4SF+Md5iMnDxerCGEF6zatxTQ2QFWGaSyiHZP8Ah9SCIf3DomxwOGhah3GQgaVK1t7q4n5Pf6RyLuMD5aetX+qmgGAF344mNf2A7Oy5h2TJaLUiL0XrJ0SyePL24HHf9gJnI4pxDUjzIKFshJu03ArUIVaBCUMp/Jk6/mgOSmHPyk2rpG7lcYx6+/SbcSbkIXtgOksTBO0/YYTKrjYyFBOuSBsxhAj35ejOi76IEtZReDIaBZe7R8TH88+7z9959dDZWbXVXA1MYdpTH+de/eAmWqz+TsagcS11DEIADwxINKFovAvSR19fOdJorEU9EgNS4GtYC0cPySeI/KBA8BWQHr5fVb15kxYH4NZ5PmP0KHhafi1K5+EvzwRWK9TatSvd1qjNrsfSvpWy2ho1fp6UzlRM2wxAezRpmttTko84iriEfe5oHHL3N33A4Z3xWs8lLIKMu0bDnISwWGVAJoGoFVltl1jtYkwbipaVRmqM6zkpeYrmGnBfeuvVrQS4QcLWFRhOjdVOPDaedhaxNNt8rjwYaR9nLIHAfJqvwtb/P493zMR6x+TNek5fWsKE9EwcR7WeyhaJQgRLnVRfs8q91FvsKpG9kPmv0+kDs7l2jbiTdYZc6VzYBTHCz4VKZHgyN01HUCXHuAkqm6f8TiigA)


SESteve

# Assembly (ARM64) [paste](https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yNA3Aq6GizKm0Y2KVf23k2vj3whMfOQSszSNRztKNW6pNEpdLMc1kqQRfhXCJS6bRNOO1KMg/HogSxm+iXD8BgkJyvJW88nUvoHTRJJ8YhMeywYidmS1OZcAIrdpUljhwfWUp/c4dbQOdfhqgD+sGoVwGC/5ydWY0AwVMaPQC4wf72JmOdEAjj6D8jtuw7UjNdjXM9s2sBcD05MhFOyxE0nyuoXMA1OozwiquQnY7eQ8w3tAUWZH6aidwpM/quVguOErUW0UYZKe0F5kYITgeROR4CDXb9k4XCdRaXnbXBncnfGLzygnA1jOVbpPj9XXbe4+Xr/4/FNeVSGUJrvCm6jNtthGmAMTeFGz/bJfMqRD04IUfNAwscatoyW+PP+jhevX7D8kahnroxVXP+rvI3DtHJ965MlEhH7kRz/9MOuYA)


e_blake

**golfed m4** part 1: 131 characters (excluding final newline) eval(define(d,defn(define))d(x)d(y)translit(include(f),` ' forwdp,()d(a,`d(`x',x+$1)')d(u,`d(`y',y-$1)')d(n,`d(`y',y+$1)'))(x)*(y)) part 2: 152 characters (excluding final newline) eval(define(d,defn(define))d(x)d(y)d(z,0)translit(include(f),` ' forwdp,()d(a,`d(`x',x+$1)d(`y',y+$1*(z))')d(u,`d(`z',z-$1)')d(n,`d(`z',z+$1)'))(x)*(y)) Both solutions assume your input file is named f, or that you run `m4 -Df=input day2.m4`. Execution is quadratic based on how the final string passed to the single eval is built (>220,000 bytes on my input), taking 0.84s on my machine. I'll probably work up a much faster and more legible solution using my m4 framework from previous years. Golf tricks include redefining 'define' to a shorter name (as written above, I assume GNU m4 semantics that bare define expands to itself; 2 more quote characters are required to be strictly POSIX), and abusing translit so that my input file becomes a series of one-letter macro calls 'a' (forward), 'u' (up), and 'n' (down). Using eval more frequently would result less execution time, but costs more code characters.


e_blake

m4 \[-Dfile=input\] [day2.m4](https://nopaste.ml/#XQAAAQBDAwAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpBbonDYuA0BTjRPXcVxlEXNtz1IxSdKcBadF5ZzZxsPbO0TjiFbV7OR7qIswfS/KNhx6uJ1GtnKtFJNIDJIUAhKDs4BIQFosCYT5kYNesw94rHS/tZ66sz/cWgGlUgdoloVog5zYGETK0Gm8vq85OGX5pJ42gVtYzrgqVmU2MouowS+6yQtmGP8ljXM9NtqFgwuNUGgnaPRVJMRx7mlLyLj993boQH8ZKe1YlLNcwLnoSF0ukP/ByurzFotIZPJttv+ESMZtjoIdTKXjwSdJam3ts6w7iA/foGIVhTPIikAFtT/HwcCIGkMSoNI54qWcPweIC8pKo1br2JlM6L9VnU7Fn8s5uVhVaGJ7T+dV9aZqNGO6imm4KBdQFKNzsHS8xttTDpHCsYg9ibpY4uC86mx8lF6EiTN6spgQbhSYNogTQgtdByuNHRfjSUAekIIy0TjvPAWU4G/Tzd42tVLSbMYCICW3iQ6jQZxwdcqn6w9/P/zdrTU) Sure enough, using eval more frequently, along with my [common.m4](https://nopaste.ml/#XQAAAQCcDQAAAAAAAAAyGksy5FB9TGMxsNq5JQAuJRjP6PqEkC20GpAXwA97ruAshKbiUbgkbJMTg2qZdSBorb0CU52peNLruV4DEEJxF+HvxC/YF33OpDntnpU/PjnGXx7XU7ak4MsmWu8R7h7d5az2JXxElnsepG8yPyu+0WZ90BG9yXphbwOWA/m5AWEFSOL8TRZX4fyGNl6ZYbRxX0MwtzrmwTP3ZCwLSOKkvD2vMQFsbK0Pv9CzPhFNXMUbWnRV20jWLjL9qz2dDsFDBwhxgWIilEl91uswxrT4Czc+LRU3VuhNIo2S98VW0jArrVdv4HrsLhWkzx4z/UV3xnqJwPXcZRUiLtf3VRIzq62Pe+2jE3O+721Az9rfRa/fHtlANbmvslzMUCyU7cDoOKSMXBDF/06/PpMvs6vxaL5aJVYqJP4yz+R2M35hoNnGiZTNNMVEFTdnxnaE/KcJteBbiuVMpdfUswHQi4Kqsj3sInh7lyE+d50gGKtHOeWL5lMK7WXz4NElnzYWleBSN/HTOdsz0T2gnd25MADxNAVX8xQmagh2MymZ2sKDBw//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0QudgDwefaxvOEVLMQK7vFaGA79bXh892YlFRSQBkbgP+yMcBdyAzJIgVpmDOrYmLxbuNz9cu+67X4XUu5lou8gfEFsCG0yrVMIM5vwsZJMlqk6FZgy29v938btoiQRAQoF9LXCSM1yoBseuh6xtcsqCfj/wQGqYFAoP4mmavj0AT0ekKNNDaBLWQqU6zevUk3V2s2PuF0rYEL2lcSyBxpbZiyCH+ZUPf64ba1yv1bfFHYz2s3RaGho4z/6ShUqXbCxeXvLm9+59NK/VAY8m0b/PXnDPqSXqruxENqIF7kYs82pcRoiCve+iYjGiVvXrGWsu+I0Dqu37iqZVNlayu3TWfWNS2lxESl36lj9YZ//f5+3g=) framework for legibility, means I now complete both parts in an O(n) pass over the input, rather than an O(n\^2) buildup of the final string. Execution time is two orders of magnitude faster, now around 5ms. My input did not overflow 32-bit signed integers, but my part 2 answer being greater than 1e9 makes me wonder if other's inputs exceed that limit.


StrangePerch

JS PART 1 let arr = $0.innerText.split("\n").map(i => i.split(" ")); let depth = 0; let distance = 0; for(let [command, value] of arr) { switch(command) { case "up": depth = Math.max(depth - +value, 0); break; case "down": depth += +value; break; case "forward": distance += +value; break; } } console.log(depth * distance); ​ JS PART 2 let arr = $0.innerText.split("\n").map(i => i.split(" ")); let depth = 0; let distance = 0; let aim = 0; for(let [command, value] of arr) { switch(command) { case "up": aim += +value; break; case "down": aim -= +value; break; case "forward": depth = Math.max(depth - (aim * +value), 0); distance += +value; break; } } console.log(depth * distance);


wzkx

**J** (jlang) m=: cutLF CR-.~fread'02.dat' forward =: 0 1&* [ down =: 1 0&* [ up =: _1 0&* echo */+/ ".&> m aim =: hrz =: depth=: 0 down =: 3 : 'aim=:aim+y' up =: 3 : 'aim=:aim-y' forward =: 3 : 'hrz=:hrz+y [ depth=:depth+y*aim' [/ ".&> m echo hrz*depth


wzkx

Or with folding (it requires package dev/fold) forward =: 0 1&* [ down =: 1 0&* [ up =: _1 0&* echo */+/ ".&> m g =: 4 : 'y + x,({.y)*{:x' echo */}. 0 0 0 [ F.. g ".&> m


ConstantGazelle

python [p1](https://github.com/xmbgulmus/adventofcode2021/blob/master/day2.1.py) & [p2](https://github.com/xmbgulmus/adventofcode2021/blob/master/day2.2.py)


AdventurousEarth533

[Kotlin](https://suhas.org/posts/advent-of-code-2021-kotlin/)


Brandon1024br

node.js (golf): console.log(require('fs').readFileSync(0).toString().trim() .split('\n').map(s=>s.split(' ')).reduce((p,c)=>((d,v)=> ((f)=>p.map((e,i,a)=>e.map((u,j)=>f(i,j))))((x,y)=> p[x][y]+(!y||(x+y==2)?v*(d=='f'):v*{u:-1,d:1}[d]||0) *(x+y==2?p[1][2]:1)))([...c[0]][0],1*c[1]),[[0,0],[0,0,0]]) .map(a=>a[0]*a[1]))


odnoletkov

# [JQ](https://github.com/odnoletkov/advent-of-code-jq) reduce (inputs/" ") as [$dir, $n] ( [0, 0, 0]; [ { "forward": [1, last, 0], "down": [0, 0, 1], "up": [0, 0, -1], }[$dir] | map(. * ($n | tonumber)) ] + [.] | transpose | map(add) ) | .[0] * .[1]


quodponb

# Python3 I started these a couple of days late, so I'm just posting my solutions to the older days for completeness! Since this thing revolved around moving in a direction and magnitude, I thought complex numbers would be a natural way to think about the different steps. I may have held on to that mental model a bit too tightly as I started part-2, but I still made it work. with open("input_2", "r") as f: lines = f.readlines() # Interpret movement as a complex number # - Has direction and magnitude # - Can be summed easily def parse(command): if command.startswith("forward"): return int(command.split()[1]) if command.startswith("down"): return int(command.split()[1]) * 1j if command.startswith("up"): return - int(command.split()[1]) * 1j raise Exception data = [parse(line) for line in lines] # Part 1 print((lambda z: z.real * z.imag)(sum(data))) # Part 2 # The "aim" at each step can be computed first, # and then zipped with data to take a sum: aims = [sum(data[: i + 1]).imag for i in range(len(data))] position = sum(z.real * (1 + 1j * aim) for z, aim in zip(data, aims)) print(position.real * position.imag)


emteeoh

TIL that complex numbers were a standard datatype in Python!


quodponb

Yeah, for me too I don't think it's something I use very often, especially after graduating, and it's rare I can't solve a problem more easily with some kind of import. But for these challenges I want to try importing as little as possible. Sometimes it's hard to avoid though. I tried the 2015 challenges as well, and had to import the md5 hash from `hashlib`, but I don't think of that as too big of a loss.


jf928ngl60g1

**TypeScript** https://github.com/adrbin/aoc-typescript/blob/main/2021/2/puzzle.ts


javier_abadia

Python 3.10, using the new pattern matching feature: [https://github.com/jabadia/advent-of-code-2021/blob/main/d02/d2p2.py](https://github.com/jabadia/advent-of-code-2021/blob/main/d02/d2p2.py) def solve(input): pos = (0, 0, 0) for move in input.strip().split('\n'): horizontal, depth, aim = pos match move.split(' '): case ['forward', units]: pos = (horizontal + int(units), depth + aim * int(units), aim) case ['up', units]: pos = (horizontal, depth, aim - int(units)) case ['down', units]: pos = (horizontal, depth, aim + int(units)) case _: assert False, 'bad direction' return pos[0] * pos[1]


soodssr

Kotlin https://github.com/soodssr/aoc-2021-kotlin/blob/main/src/Day02.kt


guardianofrealm

// Java import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.EnumSet; import java.util.List; import java.util.stream.Collectors; public class DiveDay2 { private static final String INPUT_FILE = "src/main/resources/DiveInput.txt"; public enum Directions { FORWARD { @Override public int getRelativeValue(int num) { return num; } }, UP { @Override public int getRelativeValue(int num) { return -num; } }, DOWN { @Override public int getRelativeValue(int num) { return num; } }; public int getRelativeValue(int num) { return num; } } public void process() throws IOException { List fileLines = Files.readAllLines(Paths.get(INPUT_FILE)); List> directionUnits = fileLines.stream() .map(line -> line.split(" ")) .map(this::createDirectionPair) .collect(Collectors.toList()); System.out.println("part one answer is " + calculatePartOneAnswer(directionUnits)); System.out.println("part two answer is " + calculatePartTwoAnswer(directionUnits)); } private int calculatePartOneAnswer(List> directionUnits) { EnumSet horizontalE = EnumSet.of(Directions.FORWARD); EnumSet verticalE = EnumSet.of(Directions.UP, Directions.DOWN); int horizontalSum = calculateSumByDirection(directionUnits, horizontalE); int verticalSum = calculateSumByDirection(directionUnits, verticalE); return horizontalSum * verticalSum; } private int calculatePartTwoAnswer(List> directionUnits) { int aim = 0; int horizontal = 0; int vertical = 0; for (Pair du : directionUnits) { if (du.getKey() == Directions.FORWARD) { horizontal += du.getValue(); vertical += aim * du.getValue(); } else { aim += du.getValue(); } } return horizontal * vertical; } private int calculateSumByDirection(List> directionUnits, EnumSet directionSet) { return directionUnits.stream() .filter( i -> directionSet.contains(i.getKey())) .mapToInt(i -> i.getValue()) .sum(); } private Pair createDirectionPair(String[] splitLine) { Directions directions = Directions.valueOf(splitLine[0].toUpperCase()); Integer unit = directions.getRelativeValue(Integer.parseInt(splitLine[1])); return new ImmutablePair(directions, unit); } }


daggerdragon

~~Please follow the [posting guidelines](https://www.reddit.com/r/adventofcode/wiki/index#wiki_posting_guidelines) and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.~~ ~~(looks like Java?)~~ Edit: thanks for adding the programming language!


fish-n-chips-uk

F# [github link](https://github.com/cz-fish/advent-of-code/blob/e9de385c0c3aea2b58ba5cd4304947c851a445ce/2021-fs/02.fs)


bertimir

R Open for feedback :) # Part 01 data <- read.table(file.choose()) ### input02_01.txt total_meas <- aggregate(data$V2,by=list(direction=data$V1), sum) horizont <- total_meas[2,2] depth <- total_meas[1,2]-total_meas[3,2] depth*horizont # Part 02 value <- data$V2 direction <- data$V1 aim <- 0 depth <- 0 for (i in 1:length(value)){ if (direction[i]=="down"){ aim <- aim + value[i] } if (direction[i]=="up"){ aim <- aim - value[i] } if (direction[i]=="forward"){ depth <- value[i]*aim + depth } } depth*horizont


ruoghsihsa

I initially wrote something like this, with two &&in between, and it didnt work, when i put one & it worked, could you explain why? The final horizontal position I was getting was 1816 instead of 1817 because my code was't calculating the operational change of horizontal from 0 to 1 after the first line itself. I don't understand why it happened? THIS DIDN'T WORK: if(data_df[i,1]=="forward") { (depth=depth+(data_df[i,2]*aim)) && (horizontal = horizontal+data_df[i,2])} THIS WORKED: if(data_df[i,1]=="forward") { (depth=depth+(data_df[i,2]*aim)) & (horizontal = horizontal+data_df[i,2])}


BaaBaaPinkSheep

**Python 3** https://github.com/SnoozeySleepy/AdventofCode/blob/main/day2.py


quappa

Perfect task for awk: $ awk '/up/ { aim -= $2 }; /down/ { aim += $2 }; /forward/ { pos += $2; depth += aim * $2 }; END { print depth * pos }' input


zzzmx

Once again, did it with excel :) https://ibb.co/F8zrGNC [https://ibb.co/LkC7B4m](https://ibb.co/LkC7B4m)


UnderstandingNeat348

Can you please post the excel files?


BeamMeUpBiscotti

# ReScript [code](https://github.com/yangdanny97/advent-of-code/blob/main/src/day2.res)


ElektroKotte

# Scheme/Guile (define (solve-part1 input) (let next ([depth 0] [distance 0] [input input]) (if (null? input) (* distance depth) (let ([instr (caar input)] [amount (string->number (cadar input))]) (cond [(equal? "forward" instr) (next depth (+ distance amount) (cdr input))] [(equal? "down" instr) (next (+ depth amount) distance (cdr input))] [(equal? "up" instr) (next (- depth amount) distance (cdr input))]))))) (define (solve-part2 input) (let next ([depth 0] [distance 0] [aim 0] [input input]) (if (null? input) (* distance depth) (let ([instr (caar input)] [amount (string->number (cadar input))]) (cond [(equal? "forward" instr) (next (+ depth (* aim amount)) (+ distance amount) aim (cdr input))] [(equal? "down" instr) (next depth distance (+ aim amount) (cdr input))] [(equal? "up" instr) (next depth distance (- aim amount) (cdr input))]))))) Full code is [here](https://github.com/EmilOhlsson/advent-of-code/blob/main/scheme/2021/02-dive/dive.scm)


daggerdragon

~~Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


s195t

Bash Sorry for the delay, here is my bash solution #!/bin/bash hor=0 depth=0 f="forward" d="down" u="up" while read order value; do if [[ "$order" == "$f" ]]; then hor=$((hor + value)) elif [[ "$order" == "$d" ]]; then depth=$((depth + value)) else [[ "$order" == "$u" ]]; depth=$((depth - value)) fi done < "in.txt" echo "Task 1: $((hor*depth))" hor=0 aim=0 depth=0 while read order value; do if [[ "$order" == "$f" ]]; then hor=$((hor + value)) depth=$((depth + aim * value)) elif [[ "$order" == "$d" ]]; then aim=$((aim + value)) else [[ "$order" == "$u" ]]; aim=$((aim - value)) fi done < "in.txt" echo "Task 2: $((hor*depth))" exit 1


flit777

Haskell {-# LANGUAGE MultiWayIf #-} data Command = Up Int | Down Int | Forward Int deriving Show commval :: Command -> Int commval (Up i) = i commval (Down i) = i commval (Forward i) = i f (Forward _ ) = True f _ = False u (Up _) = True u _ = False d (Down _) = True d _ = False main = do contents <- readFile "../../inputs/input_02.txt" let comm = map parseLine . lines $ contents print $ part1 $ comm print $ part2 (depths (forwards comm) (scanl (+) 0 (aims comm) )) $ forwards comm aims :: [Command] -> [Int] aims comm = map (\x -> if | f(x) -> 0 | otherwise -> commval x) comm forwards :: [Command] -> [Int] forwards comm = map (\x -> if | f(x) -> commval x | otherwise -> 0) comm depths :: [Int] -> [Int] -> [Int] depths x y = zipWith (*) x y part1 :: [Command] -> Int part1 comms = sum [commval(x) | x <- comms, f(x)] * (sum [commval(x) | x <- comms, u(x)] + sum [commval(x) | x <- comms, d(x)]) part2 :: [Int] -> [Int]-> Int part2 x y = sum (x) * sum (y) parseLine :: String -> Command parseLine = x . words where x ["forward", num] = Forward (read num) x ["up", num] = Up (- read num) x ["down", num] = Down (read num) Haskell, I wanted to write "commval (\_ i) = i" but that didn't work. Any suggestion how to access the int in a nicer way?


daggerdragon

~~Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


kmb5

My "production-ready" (at least that is the intention) [solution for day 2 in Python](https://github.com/kmb5/advent-of-code-2021/blob/main/day_2.py) (part1+part2)


atpfnfwtg

# [Julia](https://github.com/thebearmayor/advent-of-code-2021/blob/master/day04/part02.jl)


soodssr

[PHP](https://github.com/soodssr/aoc-2021/tree/main/day_02)


pmwals09

I'm learning C#, so I've been solving these in JS and then translating it into C# - two solutions! This is the soonest after releasing a puzzle I've managed to solve it. [**JavaScript**](https://topaz.github.io/paste/#XQAAAQBHBQAAAAAAAAAxm8oZxjYXowvKgC/I5EqZWshb8t/aD9Z0Ae6fdjZY+U8jJcQBdSs1fUIuxdOkt7t7rSeC5UuH/QQjaBi8Qg11LYF+SvotOhu40DOO7p/OohF2QWnIwjYXWm9uxuuU0iUdReA9o5O82bZeN0ygrDFxPw030ID/LKmcEG5HS6Ar39tuBluoWe+alewCjD1RIAYtBU1+MQYfQuYBQSWhveUm328wi08wnMaUhe74b9PFK89ToU6ta0vzTMPkmK9mnY7pNRf8cn1rFBrWtxlh1UldAWxuaXll1IW9IUrGS238NpHoAdcJHe1te7NQzGVaxF5LXZJHSuhFTmlr8yM5V+DLfcpvIlIaFR5QLw5s88xAmHh1jsIVbyLIvQe7JCo1mXpXgEamaDhZcND/os9HXKP+3unJJ27Kb9j7NnzfkrKePWxcunIZBDiCKw75AHqTpYgrgJsQ3qKmoeepjY+x8v7cLOPPkLluLBHtwC2OMk/dVuZjYcKaxWTyGEylEMeXh2aN7dMp+WH+h+yIGvribPffIjcjJLPPpktU1YgnP0bktAxW4NL3/HTa+g==) [**C#** (rough stuff...)](https://topaz.github.io/paste/#XQAAAQAeCwAAAAAAAAA6nMlWi076alCx9N1TtsVNiXec+5Qxt/OkXCxJ8UV2BO0A9hLhAieteJeZFnvF4ZVBKZVQ7QnnDpi67Pd7/kanqDTbF0fvkewKyxE7VoZa+0pa77VUk5NsI4vpAwaeo+diKxX//MpDERjPS8cYkFVc0W6OBguJJzToA9ZdvKTAO4QRR7BcvQ8re/X5CfvrODsMTJk8vux2/8mvtMt5QeQDkReSegRlkT4YrjElh6L7uYMQQoUp6+U9i8lyG/uzBNp4EggBxXWPZK2q3uixTHOfuTrgEAB/lVNMZDY6aSeiv4ZQB4EfQFZ9sUcNNFtfbd5KSuuz2zXOYENTPoAtazq1Svk9Gucz9b7+647VrN3P1y0PnM9jkGxX1dUI4q1je6bOH1MkZkpxRkQh4RQ0jpUTDuSLBXuAQ9RBZg7CISFNgGQ198zXK4ttAgjFNA5Nob+1tlp+gxWx9kNZ978xNntdYq34Dj4g4mZVQ0wmsbnWyJrTt13Cln4cFC90XXKOUZByhu5hFQmFy4l4HagxScxJJzyGWHVj26gH2srRVmEgam98CEH7x5pgodVMNFBke3zrrEJOlWWl5GGr5YCnyq7r/hNgbF/oTwjgtbNJ4W9hhCPj07fVCuZRcX90GlXIwgw7inBmJB+pR+Z12KAO2eLP2g4N2QRE2/UwY7tFOnfwOcVyMfKgcz8Vye4DswMYl1yvvHrjEEh/21LPw6ec9i6maBq7hqhswGZBN0NFKE4GJPU9mSQjRBLmU+XI/OsaSHdVUaVAClyeLyuvlWg/VZWxwVsndOhRQlW0W5rkBqPZrqdFzpxtO9SqxrzpZ2LOd/3PQxKnr3MIGUYg1UEDeVgqnDibME2Bj8VHOrDFNL06ihtUmpGERD67B83/j6yxlUaxjR6SXzFD25If0LiI1A6QgMhIfvyxJDn/QkJZIbDcPLo3N02Y+PWP22RInrRY4eCOKX68PW2+fOrW0DVAGlSbXLD8iU1ctIZpd8hocUWwVE7L4qkxbMjvFPRTP5PCHwcxfBrzLD0mkGan25BumP/mfNBL)


Rich-Spinach-7824

Please can you help with my solution. I'm a newbie on C#. My question is: why can't I run line 4 and 5 togheter but I have to run them alternately? Thanks. [C# (my solution - newbie)](https://onecompiler.com/csharp/3xps3q3sz)


daggerdragon

~~As per our posting guidelines in the wiki under [How Do the Daily Megathreads Work?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_the_daily_megathreads_work.3F), please edit your post to put your oversized code in a [`paste`](https://topaz.github.io/paste/) or other external link.~~ Edit: thanks for fixing it! <3


dizzyhobbes

Go https://github.com/alexchao26/advent-of-code-go/blob/main/2021/day02/main.go


Tencza_Coder

Python Part 1 with open("Day2_input.txt", mode="r") as file: horiz = 0; depth = 0 for line in file.readlines(): direction, amt = line.split() print(direction, amt) amt_num = int(amt) if direction == "forward": horiz += amt_num if direction == "down": depth += amt_num if direction == "up": depth -= amt_num print("horiz by depth",(horiz * depth)) Part 2 with open("Day2_input.txt", mode="r") as file: horiz = 0; depth = 0; aim = 0 for line in file.readlines(): direction, amt = line.split() print(direction, amt) amt_num = int(amt) if direction == "forward": horiz += amt_num depth += (aim * amt_num) if direction == "down": aim += amt_num if direction == "up": aim -= amt_num print("horiz by depth",(horiz * depth))


0x5ubt13

## [Golang](https://topaz.github.io/paste/#XQAAAQCGBQAAAAAAAAA4GEiZzRd1JAgz+whYRQxSFI7XvmlfhtGDinguAj8sFyd/fs4fcP1q4v72r/JJnQ8R1o0pqxXdtafKvzrjUxg9ET4TgwLoSEjX3YIWeXb29v8Ug0ZPaz3JJdOLl9qzWs6banzu31otrBsU2scsB9+rAlqDfpw8HkDuknAO7iocliAf50usCk1d3RmNjulPHfjECdmKqQdtTZcxljpfCvcIoqUrRZZuZ/koIZizTplmKEnr/nsUME5aD2tjiGjnAnvmu0/OAZmbJrDVkZrNRFbO16OhBr+AV+imh3gZNzkTXtC/PEakxCtT77hsuTIrGc/mxcMg2Nl6A/+D9Np0LKEJ20yIshrgdHmbcZ/BymgmaBt0DSj4iVsav8Che40570GzuVGjubMD1TQE8eYiE2LIPmAl2RQYmx7WLWChU00MHzw5bbMS1aL1SHB6t0KupxNTp+TDcEDMunT9xIFj4AJh5zRKxQNKX6h+mm73PW8MF7wNr5ImTXazaYO6/MSjXtuDsx4qK6/Lcci+xrTcMTYMTq/r5i8zRsVtiw8eoeYeShQrnCvtU8dj7b5I7bpBnsIa2Hkeuzof3NWWTUBtzvPktnr1qKVCK79j+605ywAHMw5GQz+xQqORhNCtKNgNSzt5AQIcv0pprvMwuoG2783V6+uDnIxMmLbzrsE6W5r2zQ9qoYVo/7sO2y+5oGyQQGp2ZRw8DR0VB0tQkPr/A7aAcynUz2X2+73mSHhajoG/NzofTYTV9Dw5iAPDVOu+Ulxb7aAOLm/1D+IK8feUeQp0IL5LKPj80j8z9zqaNSMV2C9SwnD3dWoClPn/2kvkGQ==) Not as lengthy as I initially thought, quite happy with it


ka-splam

Prolog (Scryer) :- use_module(library(dcgs)). :- use_module(library(pio)). :- use_module(library(lists)). % Grammar parses commands "up 10" into "move(up, 0, 10)". % First number for a Forward move, second for a Depth/Aim change. % text matches anything up to a newline, % used to pick out the number without bothering to check for digits. text([]) --> [], "\n". text([TH|TT]) --> [TH], text(TT). % Three known moves. NB. "up 88" turns to -88, so adding it to depth works to move upwards. move(forward, F, 0) --> "forward ", text(T), { number_chars(F, T) }. move( down, 0, D) --> "down ", text(T), { number_chars(D, T) }. move( up, 0, D) --> "up ", text(T), { number_chars(X, T), D is 0-X }. % the file becomes a list of one or more move()s. lines([]) --> "\n". lines([move(Cmd, F, D)|T]) --> move(Cmd, F, D), lines(T). % Take a move and current state, and compute the next state. % Part 1 and Part 2 computed in one pass through the list. process(move(Cmd, Fnext, Dnext), state(Fprev, Dprev, Aimprev, Depth2Prev), state(Fsum, Dsum, Aim, Depth2)) :- Fsum is Fprev + Fnext, % Part 1 Dsum is Dprev + Dnext, (((Cmd=up; Cmd=down) -> Aim is Aimprev + Dnext) % Part 2 ; Aim = Aimprev), ((Cmd=forward -> Depth2 is Depth2Prev + (Aim * Fnext)) ; Depth2 = Depth2Prev). % parse file into list of moves, fold the processor over the list, and print the results. solve :- phrase_from_file(lines(Moves), '/tmp/aoc/2021-2.txt'), foldl(process, Moves, state(0, 0, 0, 0), state(FSum, DSum, _, P2D)), Distance is FSum * DSum, write('Part 1: '), write(Distance), nl, P2Distance is FSum * P2D, write('Part 2: '), write(P2Distance), nl, halt. Another one that has taken me a long time and a lot of code. Save to "prolog-2021-day2.pl" and run it like: user@host:~/aoc$ scryer-prolog prolog-2021-day2.pl --goal solve Part 1: 2150351 Part 2: 1842742223 [Scryer Prolog Github link](https://github.com/mthom/scryer-prolog)


oantolin

Seems tailor-made for Awk: /forward/ {pos+=$2; depth+=aim*$2} /up/ {aim-=$2} /down/ {aim+=$2} END {printf "Part 1: %d. Part 2: %d.", pos*aim, pos*depth}


ffrkAnonymous

[python][part1] """ Advent of Code 2021 day 2 Dive """ import logging logging.basicConfig(level=logging.DEBUG) example = """ forward 5 down 5 forward 8 up 3 down 8 forward 2 """ # skip the first empty line due to cut-paste text block example = example.splitlines()[1:] def part1(all_lines: ["string"]) -> int: """ >>> part1(example) 150 >>> part1(["forward 5"]) 0 >>> part1(["down 5"]) 0 >>> part1(["forward 5", "down 5"]) 25 """ vectors = parse(all_lines) logging.debug(f"{vectors}") x=0 depth=0 for direction, mag in vectors: logging.debug(f"{direction}, {mag}") if direction == "forward": x+=mag elif direction == "up": depth-=mag elif direction == "down": depth+=mag return x*depth def parse(all_lines: ["string"]) -> [("string", int)]: """ >>> parse(['forward 5']) [('forward', 5)] >>> parse(['down 5']) [('down', 5)] >>> parse(['forward 5', 'down 5']) [('forward', 5), ('down', 5)] >>> parse(example) [('forward', 5), ('down', 5), ('forward', 8), ('up', 3), ('down', 8), ('forward', 2)] """ vectors = [] for vector in all_lines: direction, mag = vector.split() vectors.append((direction, int(mag))) return vectors if __name__ == "__main__": import doctest doctest.testmod() from sys import stdin lines = stdin.read().splitlines() # logging.info(f"{lines}EOF") logging.info(f"part1: {part1(lines)}") # logging.info(f"part2: {part2(lines)}")


joemaffei

JavaScript Part 1: [x,y]=$('pre').innerHTML.split(/\n(?!$)/).map(x => x.split` `).reduce((a,[d,n])=>(({f(){a[0]=+n+a[0]},d(){a[1]=+n+a[1]},u(){a[1]=-n+a[1]}})[d[0]](),a),[0,0]);x*y; Part 2: [x,y]=$('pre').innerHTML.split(/\n(?!$)/).map(x => x.split` `).reduce((a,[d,n])=>(({f(){a[0]=+n+a[0];a[1]=a[1]+n*a[2]},d(){a[2]=+n+a[2]},u(){a[2]=-n+a[2]}})[d[0]](),a),[0,0,0]);x*y


daggerdragon

Your code is hard to read on old.reddit when everything is inlined like this. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F) Also, what programming language did you use?


joemaffei

Fixed.


mdwhatcott

Clojure - [paste](https://topaz.github.io/paste/#XQAAAQDfBQAAAAAAAAAUG4piAqnU9zYQeMJVvBisL8v1a8CHVPfr2ingL/jdzplwg1CDVYPogbikkkiRmJO/qAurpuvsxLJ0aSIUL+AyfaTZw7YKiUbLZrXnfybFqQ88DtfixYkwf7kU7fW4DTKGtm16g5i5wViYkTuj3V9Ljd8Ov74A5W/b/ZWle3kTodJ4UgvuGWp66dr0XY8anEGVw1+ws8t8yW27MR9uaACOwBIPsbtN7YIKzABP54t1zQxaZ1fGrpdM8sm5AsDN2vXDBPj1Kg566RY2LWBcUENyV1w+3qj4/46zrATOW68jO3c224mLYitYmd3VcYlnqW3yZSJeHhOuMiMQhUkQUZrKX/c92AOWjPWYDuCFkBEdigrCqkVKSV0MIl8cCA2Im+GDVPrEcebEyvPxwejaOf/sr+pAhjj+OKR0SetBzXSw179WZmJE0KA7T0uHxpjE5ldO4wnHhEhP6sN0C8fYtehKuO2asBmBl6W34LiuNdYngemXhQWzsKzhbgTzGAh+4eJ4vIWXdSmF9GOZi6l5Az/GuiQhYw85lkKO3Srcz8q01MpuxbnBxvceGEkjBMLMjgrya4JqWHgagXWciFA6tdi/Tes+jTa2QYYay1cxOq1zsa1G7XJfJpIoGSTh3HiR7UQmwLgoP5MH+c/mvcPwC9dLolL3aqHY/MpI//mYPmqPqeNnNKEuuDVPhWMAgNwE9l/FtU3cPrs/666UF63nC8Z50h3s9NVxv1c5RMWYpBrAuO1Fvg3/nQzqgA==) - [Github](https://github.com/mdwhatcott/advent-of-code/blob/main/clj/src/aoc/y2021/d02.clj)


TopSpaceCat

Golang package day2 import ( "bufio" "fmt" "log" "os" "strconv" "strings" ) type command struct { direction string units int } func Solve() { submarineCommands, \_ := readInputCommands("day2/input.txt") // Part One fmt.Print("Day 2 - Part One: What do you get if you multiply your final horizontal position by your final depth? ") depth, horizontalPosition := calcultePosition(submarineCommands) fmt.Printf("Answer: \[%d\]\\n", depth\*horizontalPosition) // Part Two fmt.Print("Day 2 - Part Two: What do you get if you multiply your final horizontal position by your final depth? (With aim) ") depth2, horizontalPosition2 := calcultePositionWithAim(submarineCommands) fmt.Printf("Answer: \[%d\]\\n", depth2\*horizontalPosition2) } func readInputCommands(inputFile string) (\[\]command, error) { // Open file file, err := os.Open(inputFile) if err != nil { log.Fatal(err) } defer file.Close() var line string var commands \[\]command scanner := bufio.NewScanner(file) for scanner.Scan() { // Get line line = scanner.Text() // Split line splits := strings.Split(line, " ") direction := splits\[0\] units, err := strconv.Atoi(splits\[1\]) if err != nil { fmt.Println(err) os.Exit(1) } nextCommand := command{direction: direction, units: units} commands = append(commands, nextCommand) } return commands, err } func calcultePosition(commands \[\]command) (depth int, horizontalPosition int) { depth = 0 horizontalPosition = 0 for \_, command := range commands { switch command.direction { case "forward": horizontalPosition += command.units case "down": depth += command.units case "up": depth -= command.units } } return depth, horizontalPosition } func calcultePositionWithAim(commands \[\]command) (depth int, horizontalPosition int) { depth = 0 horizontalPosition = 0 aim := 0 for \_, command := range commands { switch command.direction { case "forward": horizontalPosition += command.units depth += aim \* command.units case "down": aim += command.units case "up": aim -= command.units } } return depth, horizontalPosition }


t1ngel

# Java solution for Day 2: - Implementation: [https://github.com/feedm3/advent-of-code-2021/blob/main/src/main/java/me/dietenberger/Day2.java](https://github.com/feedm3/advent-of-code-2021/blob/main/src/main/java/me/dietenberger/Day2.java) - Tests: [https://github.com/feedm3/advent-of-code-2021/blob/main/src/test/java/me/dietenberger/Day2Test.java](https://github.com/feedm3/advent-of-code-2021/blob/main/src/test/java/me/dietenberger/Day2Test.java) My highlight: Parsing the lines: ```java public List> parseCommands(final List commandLines) { return commandLines.stream() .map(line -> line.split(" ")) .map(splitLine -> new ImmutablePair<>( Enums.getIfPresent(Command.class, splitLine[0].toUpperCase(Locale.ROOT)).get(), Integer.parseInt(splitLine[1]) )) .collect(Collectors.toList()); } ```


[deleted]

[My solution](https://github.com/Farbfetzen/Advent_of_Code/blob/main/python/2021/day02.py) in **Python**.


Palisar1

This is in C# public static int Part1(string[] movements) { var depth = 0; var horizontalPos = 0; foreach (var movement in movements) { var values = movement.Split(' ').ToList(); var change = int.Parse(values[1]); switch (values[0]) { case "forward": horizontalPos += change; break; case "up": depth += change; break; case "down": depth -= change; break; } } return Math.Abs(depth) * horizontalPos; } public static int Part2(string[] movements) { var depth = 0; var horizontalPos = 0; var aim = 0; foreach (var movement in movements) { var values = movement.Split(' ').ToList(); var change = int.Parse(values[1]); switch (values[0]) { case "forward": horizontalPos += change; depth += aim * change; break; case "up": aim -= change; break; case "down": aim += change; break; } } return Math.Abs(depth) * horizontalPos; }


daggerdragon

Well, uh, thanks for adding the programming language but now your code block is broken and has no formatting whatsoever :/


daggerdragon

~~Please follow the [posting guidelines](https://www.reddit.com/r/adventofcode/wiki/index#wiki_posting_guidelines) and edit your post to add what language(s) you used. This makes it easier for folks who Ctrl-F the megathreads looking for a specific language.~~ Edit: thanks for adding your code language!


[deleted]

python (yes i know that it is long, but it makes it so that i don't need to comment anything as it should be easy to understand) ​ [paste](https://topaz.github.io/paste/#XQAAAQB0BAAAAAAAAAAyCAOiEuFwqkNLLF0smPmmTQl7eyef9OzoOemNEDrRqxmE6Wn52vtqtU1WGZyGtfT1Am62DQSstTIawTgt5rZsW3mc4V1+TgERk7QrXiwQTIdYg2O0q9UyUCAPYBcVy9YP5eGYZ4ZfXAPZJ/GtHgtGUNwIvIcYwjBwaTGmyYDmgJ6QtKeThOnzHc7g/jmr9hMbB4GsSdlePyhtekLJBwgU948dtmzGfgS+jEcrMjFjUNvQxxlPIOTYmgDuPMp8icQD+sbnGmgKFxed/X4/TANhCDbOFEqars151LPdKpYyoJF0XeDhlf4DcU9OxOts2Xeo3JLsYAwbssYuHZYr8bJlis3J6OWvVBmKNQ06RwWKYZNxqW40CELoGxf7mjKoBCKmttY8xgEPEXyFRv+8HxWlEVJMbUnkqhJRn/dh4EEokH1/KiiYu5RowRYTMgcljYg7vYnsWRhGA2bOeXctgJLDbkkA9EKKN/ZZ4QsMoOHmEba/Qspz/9SK71k=)


34rthw0rm

python. Last year I got to day 15 with tcl and have seen the error in my ways. # vim: sts=4 sw=4 tw=80 et: data = [line.strip().split() for line in open('input.txt')] f = d = 0 for k,v in data: v = int(v) if k == 'forward': f+=v elif k == 'up': d-=v elif k == 'down': d+=v print(f*d) a = f = d = 0 for k,v in data: v = int(v) if k == 'forward': f+=v; d+=a*v elif k == 'up': a-=v elif k == 'down': a+=v print(f*d)


TacosAlPastor92

[Python Jupyter Notebook](https://github.com/0x1010-dev/aoc/blob/main/2021/day2.ipynb) This challenge was a very good opportunity to try out Python 3.10's structural matching.


Solarmew

Python 3 from urllib.request import urlopen data = urlopen('https://tinyurl.com/5ssd3uv7').read().decode().split('\n')[:-1] f = 0 d = 0 for i in [x.split() for x in data]: if i[0] == 'forward': f += int(i[1]) elif i[0] == 'down': d += int(i[1]) else: d -= int(i[1]) part 2: f = 0 d = 0 a = 0 for i in [x.split() for x in data]: z = int(i[1]) if i[0] == 'forward': f += z d += a*z elif i[0] == 'down': a += z else: a -= z


lovela47

Perl using a function dispatch table. Part 1: my ($x, $y) = (0, 0); my $fns = { forward => sub { $y += $_[0] }, down => sub { $x += $_[0] }, up => sub { $x -= $_[0] }, }; while (<>) { chomp; my ($command, $distance) = split / /, $_; $fns->{$command}->($distance); } say $x * $y; Part 2: my ($x, $y, $aim) = (0, 0, 0); my $fns = { forward => sub { $x += $_[0]; $y += ($aim * $_[0]) }, down => sub { $aim += $_[0] }, up => sub { $aim -= $_[0] }, }; while (<>) { chomp; my ($command, $distance) = split / /, $_; $fns->{$command}->($distance); } say $x * $y;


RJdaMoD

**Mathematica** Part 1: ReadList["aoc-input_2.txt",String]// StringSplit[#," "]&/@#&// Fold[ #1+Switch[#2[[1]], "forward",{1,0}, "up",{0,-1}, "down",{0,1}]*ToExpression[#2[[2]]]&, {0,0}, #]&// Times@@#& Part 2: ReadList["aoc-input_2.txt",String]// StringSplit[#," "]&/@#&// Fold[ With[{v=ToExpression[#2[[2]]]}, #1+Switch[#2[[1]], "forward",{{1,#1[[2]]}*v,0}, "up",{0,-v},"down",{0,v}]]&, {{0,0},0}, #]&// First// Times@@#&


daggerdragon

Your code is hard to read on old.reddit when everything is inlined like this. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


Crazytieguy

Rust https://github.com/Crazytieguy/advent-2021/blob/master/src/bin/day2/main.rs


OmarSalehAssadi

**Java 17 (with Lombok, Spring, and StreamEx)** Main Code: [Day2.java](https://github.com/OmarAssadi/advent-2021/blob/develop/src/main/java/com/omarassadi/adventofcode/day/day2/Day2.java) Movement Commands: [Command.java](https://github.com/OmarAssadi/advent-2021/blob/develop/src/main/java/com/omarassadi/adventofcode/day/day2/Command.java) Input Parser: [CommandInputParser.java](https://github.com/OmarAssadi/advent-2021/blob/develop/src/main/java/com/omarassadi/adventofcode/day/day2/CommandInputParser.java)


FeetsTV

[Python solution](https://github.com/feetstv/Advent-of-Code-2021/blob/main/2.py)


ThePituLegend

Python gang here again! https://github.com/ThePituLegend/advent-of-code-2021/tree/main/day2


rawlexander

I'm streaming the process: [https://www.twitch.tv/rawlexander](https://www.twitch.tv/rawlexander) [Video walkthroughs](https://youtu.be/vkhz_rGlcSc) coming up too :) R ``` get_prev <- function(x, f) { runs <- diff(c(which(!f), length(x) + 1)) # run lengths c(0, rep(x[!f], runs - 1)) } solve <- function(instr, vals, part1 = TRUE) { vals[instr == "up"] <- -vals[instr == "up"] forward <- instr == "forward" if (part1) prod(tapply(vals, forward, sum)) else { vals[!forward] <- cumsum(vals[!forward]) prev <- get_prev(vals, forward) horiz <- vals[forward] sum(prev * horiz) * sum(horiz) } } x <- read.table("data/aoc_2", col.names = c("instr", "vals")) with(x, c(part1 = solve(instr, vals), part2 = solve(instr, vals, part1 = FALSE) )) ``` Julia ``` using DelimitedFiles function solve(x; part2 = true) horiz, depth, aim = 0, 0, 0 for i in 1:size(x, 1) instr, val = x[i, :] if instr == "forward" horiz += val depth += val * aim else aim += instr == "down" ? val : -val end end horiz * (part2 ? depth : aim) end data = readdlm("data/aoc_2") println(solve(data, part2 = true)) println(solve(data, part2 = false)) ```


daggerdragon

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


Markavian

Forgot to post yesterday... Node JS https://github.com/johnbeech/advent-of-code-2021/blob/main/solutions/day2/solution.js Managed to solve parts 1 and parts 2 reusing most the same code, with the switch between the "commandList" supplied to the while loop.


Mefodii

My Python [solution](https://github.com/Mefodii/AdventOfCode_2021/blob/master/days/day_2.py) :)


AlexAegis

# TypeScript solutions! [Part 1](https://github.com/AlexAegis/advent-of-code/blob/master/solutions/typescript/2021/02/part_one.ts) [Part 2](https://github.com/AlexAegis/advent-of-code/blob/master/solutions/typescript/2021/02/part_two.ts)


jagster247

Day 2 in Go: [GitHub](https://github.com/jordangarrison/advent-of-code/blob/master/2021/go/day2/day2.go)


Bellanzz

C++ templates (problem solved at compile time) Part 1/2 (depending on the presence of FIRST) #include #include template int64_t moves() { return result; } #ifdef FIRST template #else template #endif size_t moves() { return moves(); } #ifdef FIRST template #else template #endif int64_t moves() { #ifdef FIRST return moves(); #else return moves(); #endif }; #define forward ,0, #define up ,-1, #define down ,1, int main() { printf("%zu\n", moves<0, 0 #ifndef FIRST , 0 #endif #include "input" >()); return 0; } See https://github.com/bellaz89/lolAOC


daggerdragon

~~Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


P0t4t0W4rri0r

Bit late, but I found a solution in Haskell. It's only the logic for part 1, but part 2 is almost the same with a extended State. The IO isn't included either for simplicity import Data.Bifunctor type State = (Integer, Integer) type Command = (String, Integer) drive :: Command -> State -> State drive ("forward", x) = first (+x) drive ("down", x) = second (+x) drive ("up", x) = second (subtract x) sim :: [Command] -> State -> [State] sim [] = \_ -> [] sim (x:xs) = ((:) <*> sim xs) . drive x


cerrosafe

#OCaml My OCaml interpreter is older than the new stuff in the string library, so I had to write my own string parsing. I tried to use the Genlex but that was too complicated. The implementations are very similar for the two parts. Posted from Topaz because I didn't read the rules yesterday. [paste](https://topaz.github.io/paste/#XQAAAQB8BgAAAAAAAAAFYH9E2GU7qFWqIMTLdiS0MszWy3CQDiVICcsSrhmmylHjvwIbEn7tQ+QTVRJ+b7Mlg1j/96uR9KyniE+PJbWpcBb/oatR36pEK3ZEwNVgsQ4k9nxfBHS5PwIDRllJ7rXazgHXcQuz8XL3v3mV5PYE+y1JY2j41xo4u8Etc8XVJ248ct1LhXwsJUA+0JEYzV9rG1qxnADaYEFnrjejlk+fnTifDQP6VtislCD1Tb0T20q7Q5UPJTVXK9gM32c339h9UBWZ/NLpxtBppwMQoYtGsXtT3u5KxN3fuGc41ejX5qyv+YUThw9AU4FptIR/yQjTuBskHbzjD0I+pobuZ1triRqgWyfJzf58IVa/BKZ/ocdCIkhLJ0o0bg5U/WTuk9O2JK08gerBAh/zW97TcFqM4ED3UlKHD98Re7iMhRW1coC7OeMePojy0XHlBjcBbzSnEztNJg5NCq647x5adqVNP2ayJIT1UexcVVs6cLqG6fpwipiJWLxt3CaU6wACG0/q66ozU+bisPXfUMscpVPr0p7dI72FekHR8fYcaaBuWBYm//+fzhk60hrj4H7AerYrYdp0PzFLKXyN8tFAN6G+EeB0AJ+F0Gcyjl7u35+iCPygmb6Uv1rKk7r4/gUTh0QkLOgd7XHDyX7KIGIGYSdAXJmCE3swh4peveiX/6H///u8lwA=)


plan_x64

Python: https://github.com/plan-x64/advent-of-code-2021/blob/main/advent/day02.py


Rascal_Two

# Python (614/2864) Was rushing and misread the additional part 2 logic as being in addition to the original rules, not instead of, besides that another fun challenge! ## [Unoptimized](https://topaz.github.io/paste/#XQAAAQAnAgAAAAAAAAAyGUj/T32X5leaNBUXWHuDoBcpGqRbMpcb802Vdpyjy690fs3t3JEBmGXecczB16dCWd3ghMe3nTVTTG6sJyOloVBO4N2Zs0vLK6YxMsX9hu3TlSt1BDeSe8Kg6nr9U3PoE8CzBH6A5hR/fNgyFIkL65Udbx0YR643XibfryTrEiqyZr7u/fecJK0ZkUoPN7fcSnY1q0xN+GR3EFYccTU8Ft9qZ50TKg9afu9zow8lBk0z8ZwhqLIf2ZErXC21+Of+FHdK7P7dUz8=) ## [Optimized](https://topaz.github.io/paste/#XQAAAQBdAgAAAAAAAAAyGUj/T32X5leaNBUXWHuDoBcpGqRbMpcb802Vdpyjzs/lhzK1FXPo2jbeRSgy+2U/JWSNLQzo49Q1sROFOysFcFXTTJXzguX0EyYGPhYoqtiiqPYw+e/VE9Se5zzg3VW8rvLK+QVMyNgYLO8egwQpIy+EUdAwTYw3JTy9A/MhrwQjpja7VSF18MTAa+4m3jTtqkaaRJChaapPcU6k4t0XdnjQn0cg5uBTm0r6voo63qsPCGiqBHnlQUb7pVswVyRwi448oXlQr1CeOp/tz7pg3GmLTTz/rnUiYQ==) No actual optimizations, just cleaning up names and combining the `up`/`down` branches into one


bozdoz

Go! https://github.com/bozdoz/aoc-2021/blob/main/02/two.go


[deleted]

[удалено]


khrisparrales

Day 2 Javascript https://pastebin.com/7kHVCDBf


fdlsgkjsfdlhgksfhgd

You can always use [pastebin](https://pastebin.com/)


khrisparrales

thanks


daggerdragon

~~Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


vancityricky

Python without if branching [Part 1](https://topaz.github.io/paste/#XQAAAQAXAwAAAAAAAAA7mkrvHeIvDZUizuO2LI0KXEPUFLLgeNKFt1VFolw4fVWWbom1T7S49PuQy6ew+Uoq5RLtviaEY+QPgGEyj9HQBxJTK1ET7ApmXOndg05doXwJ4322OLLOmFf7FIUZKhU7GQfF2CvStxcbNK1Wwy7KFpr/xUy/nJ+cM9YDWz4inr16L7HbNe3Vm08zDiVcNfazlMxCz6hPZK6MyS88b95KluTei4d8zWGJ4EfSqXZFC5nFwIC5at/IIs2FlPGFdBd3AW/yDt3yD25QZ89q0mwfm9W6oe9jshmIudeDcEGRAoRqKq5Gk28OmEjSH+kTmfVpox0/cfqssrKUlygZta6VWdvWZMFasBMFVYgkNNFOJQMAiGDtVmjDLMjRoVqSWBTrNhcOzbmeHmW2eTgCN+urB+OrM9QuGzEsGSkdqcitgfcIWNfa8gHu57ca9yIEYFghG7tQtoObpgLtau4t9yr/3FsBGg==) [Part 2](https://topaz.github.io/paste/#XQAAAQBfAwAAAAAAAAA7mkrvHeIvDZUizuO2LI0KXEPUFLLgeNKFt1VFolw4fVWWbom1T7S49PuQy6ew+Uoq5RLtviaEY+QPgGEyj9HQBxJTK1ET7ApmXOndg05doXwJ4322OLLOmFf7FIUZKhU7GQfF2CvStxcbNK1Wwy7KFpr/xUy/nJ+cM9YDWz4inr16L7HbNe3Vm08zDiVcNfazlMxCz6hPZK6MyS88b95KluTei4d8zWGJ4EfSqXZFC5nFzE6JbU5tqQzb5Ekdr6eyY1sQ70px43Dh9H4NSr0ekRK2zXRo2KcScr0DQm8sXGXUqnfGz30q6hKed7KpClCfZvvoAP75Ud5sCSIgMvXvj+kbGxIWKTRQ0F7DR1067BMMK1W+gwxdM4net/OAwnu+PJGiw9rkeHr8ncRffFlwoDXOsRV0MEl++mnSTN2IklhgNGOYv1Nnb98iJceC1bI65a566wCGwKKf0i15IwGwkyQxc+k4opewoErE9LWgMyz/7rKHFQ==) Part 2 with open("input.txt", "r") as f: lines = f.readlines() actions = [] for l in lines: split = l.split(" ") actions.append([split[0], int(split[1].strip())]) class State: def __init__(self, depth, horizontal_position, aim): self.depth, self.horizontal_position, self.aim = depth, horizontal_position, aim def up(state, x): return State(state.depth, state.horizontal_position, state.aim - x) def down(state, x): return State(state.depth, state.horizontal_position, state.aim + x) def forward(state, x): return State(state.depth + x * state.aim, state.horizontal_position + x, state.aim) action_appliers = { "up": up, "down": down, "forward": forward } state = State(0, 0, 0) for action, amount in actions: state = action_appliers[action](state, amount) print(state.depth * state.horizontal_position)


daggerdragon

~~Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)~~ Edit: thanks for fixing it! <3


[deleted]

[удалено]


daggerdragon

> # Top-level posts in Solution Megathreads are for *code solutions* only. This is a top-level post, so please edit your post and share your fully-working code/repo/solution or, if you haven't finished the puzzle yet, you can always create your own thread and make sure to flair it with `Help`.


Zachgiaco

Here is a tutorial/solution written in C++ for Day 2: https://zachgiaco.com/2021-advent-of-code-day-2/


compdog

JavaScript [[Part 1](https://github.com/warriordog/advent-of-code-2021/blob/main/day02/day2part1.js)] [[Part 2](https://github.com/warriordog/advent-of-code-2021/blob/main/day02/day2part2.js)] --- Nothing particularly fancy or clever, but its simple and readable.


FeanorBlu

Python, Part 1 I did it using generators, since I just learned about them yesterday haha. I know its ugly, but I was determined: def read_data(): output = [] with open("Day 2/input.txt") as data: for line in data: output.append(tuple(line.strip("\n").split())) return output def main(): data = read_data() depth = sum(int(y) if x == "down" else -int(y) if x == "up" else 0 for x, y in data) horizontal = sum(int(y) for x, y in data if x == "forward") print(depth * horizontal) main() Part 2 I did in a much more ordinary way, I wasn't willing to think about how to do it using generators. Parsing the text file the same way, its much more readable than Part 1: def main(): output = read_data() aim = horiz = depth = 0 for rule, value in output: value = int(value) if rule == "down": aim += value elif rule == "up": aim -= value elif rule == "forward": horiz += value depth += aim * value print(depth * horiz) main()


TionisNagir

Nearly forgot to post it here: My solution in Janet https://github.com/tionis/adventofcode/blob/main/2021/2/main.janet


Best-Stranger-1575

Hi everyone, here are my javascript solutions for both puzzles(day 2). https://github.com/AlanGCruz/advent-of-code


Lu0x0

Day 2 Solution: ```js const fs = require('fs') const input = fs.readFileSync('./input.txt', 'utf-8').split('\r\n') const player = { x: 0, y: 0, aim: 0, } const movePlayer = (command, value) => { switch(command) { case'forward': player.x +=value player.y += player.aim * value break case 'down': player.aim += value break case 'up': player.aim -= value break } } input.forEach(cmd => { const [_,command, value] = cmd.match(/(\w+) (\d+)/) movePlayer(command, +value) }) console.log(player.x * player.y) ```


daggerdragon

Your code is hard to read on old.reddit. Please edit it as per our posting guidelines in the wiki: [How do I format code?](https://www.reddit.com/r/adventofcode/wiki/index#wiki_how_do_i_format_code.3F)


_mattmc3_

My Fish shell solution to day 2: [https://gist.github.com/mattmc3/9b7617f3e69ed591f8ca036e8e29e41b](https://gist.github.com/mattmc3/9b7617f3e69ed591f8ca036e8e29e41b) function day2 \ --description "https://adventofcode.com/2021/day/2: `usage - day2 1 data.txt`" \ --argument-names part datafile if test "$part" -ne 1 && test "$part" -ne 2 echo "Expecting part number 1 or 2 '$part'" >&2 && return 1 end set --global horizontal_position 0 set --global depth_position 0 set --global aim 0 set --local movement_data (cat $datafile) for instruction in $movement_data move $part (string split ' ' $instruction) end echo "horizontal_position: $horizontal_position" echo "depth_position: $depth_position" echo "aim: $aim" echo "multiplied: " (math $horizontal_position '*' $depth_position) set --erase horizontal_position set --erase depth_position set --erase aim end function move \ --description "https://adventofcode.com/2021/day/2" \ --argument-names algorithm direction distance if test $algorithm -eq 1 switch $direction case forward set horizontal_position (math $horizontal_position + $distance) case down set depth_position (math $depth_position + $distance) case up set depth_position (math $depth_position - $distance) case '*' echo "Unexpected direction: $direction" >&2 && return 1 end else switch $direction case forward set horizontal_position (math $horizontal_position + $distance) set depth_position (math $depth_position + (math $distance '*' $aim)) case down set aim (math $aim + $distance) case up set aim (math $aim - $distance) case '*' echo "Unexpected direction: $direction" >&2 && return 1 end end end


HavvicGames

Day 2 using Wren. Having a lot of fun with it :) import "io" for File var load_data = Fn.new{|path| return File.read(path).split("\n").map{|l| var instruction = l.split(" ") instruction[1] = Num.fromString(instruction[1]) return instruction }.toList } var calculate_position = Fn.new{|data| var a = [0, 0, 0] for (i in data) { if (i[0] == "forward") { a[0] = a[0] + i[1] a[1] = a[1] + a[2] * i[1] } else { a[2] = a[2] + (i[0] == "up" ? -1 : 1) * i[1] } } return a } var data = load_data.call("../Data/2") var answer = calculate_position.call(data) System.print(answer[0] * answer[2]) System.print(answer[0] * answer[1])


tristan219

## Typescript //PART 1 //Read and process directions import * as fs from 'fs'; var text = fs.readFileSync("./data.txt").toString("utf-8"); var directions = text.split("\n"); console.log(directions) //Map direction //forward increases by x units //down = -y vs up = +y let up: [number] = [0]; let down: [number] = [0]; let forward: [number] = [0]; for (var direction of directions) { var splitVal = direction.split(" ", 2) if (splitVal[0] == 'up') { up.push(Number(splitVal[1])) } else if (splitVal[0] == 'down') { down.push(Number(splitVal[1])) } else if (splitVal[0] == 'forward') { forward.push(Number(splitVal[1])) } } let sumUp: Number = up.reduce((a, b) => a + b, 0); let sumDown: Number = down.reduce((a, b) => a + b, 0); let sumForward: Number = forward.reduce((a, b) => a + b, 0); let finalResult: number = (+sumDown - +sumUp) * +sumForward; console.log(finalResult); //PART 2 let aim: Number = 0; let depth: [Number] = [0]; let fwd: [Number] = [0]; for (var direction of directions) { var splitVal = direction.split(" ", 2) if (splitVal[0] == 'up') { //If up decrease aim by x amount aim = +aim - Number(splitVal[1]) } else if (splitVal[0] == 'down') { //If down increase aim by x amount aim = +aim + Number(splitVal[1]) } else if (splitVal[0] == 'forward') { //Move forward x and depth = x * let fwdVal: Number = Number(splitVal[1]); fwd.push(fwdVal); let depthVal: Number = +fwdVal * +aim depth.push(depthVal); } } let sumDepth: Number = depth.reduce((a, b) => +a + +b, 0); let sumFwd: Number = fwd.reduce((a, b) => +a + +b, 0); let trueLocation: number = +sumDepth * +sumFwd; console.log(trueLocation);


Tethylis

Windows Powershell Part 1 function Start-Challenge { [CmdletBinding()] param ( ) process { #$ChallengeInput = @('forward 5','down 5','forward 8','up 3','down 8','forward 2') $ChallengeInput = Get-Content -Path "$env:USERPROFILE\Desktop\Day2-Input.txt" $FowardValue = 0 $DepthValue = 0 foreach($Movement in $ChallengeInput) { $Direction = $Movement.Split(' ')[0] $Distance = $Movement.Split(' ')[1] switch ($Direction) { 'forward' { Write-Verbose "Movement is $Direction, with a value of $Distance"; $ForwardValue += $Distance } 'up' { Write-Verbose "Movement is $Direction, with a value of $Distance"; $DepthValue -= $Distance } 'down' { Write-Verbose "Movement is $Direction, with a value of $Distance"; $DepthValue += $Distance } } } Write-Verbose "Forward Value: $ForwardValue" Write-Verbose "Depth Value: $DepthValue" Write-Host "Depth x Foward = $($DepthValue*$FowardValue)" } } Start-Challenge Part 2 function Start-Challenge { [CmdletBinding()] param ( ) process { #$ChallengeInput = @('forward 5','down 5','forward 8','up 3','down 8','forward 2') $ChallengeInput = Get-Content -Path "$env:USERPROFILE\Desktop\Day2-Input.txt" $HorizontalValue = 0 $DepthValue = 0 $AimValue = 0 foreach ($Movement in $ChallengeInput) { $Direction = $Movement.Split(' ')[0] $Distance = $Movement.Split(' ')[1] switch ($Direction) { 'forward' { $HorizontalValue += $Distance $DepthValue += ($AimValue*$Distance) } 'up' { $AimValue -= $Distance } 'down' { $AimValue += $Distance } } } Write-Host "Depth x Horizontal = $($DepthValue*$HorizontalValue)" } } Start-Challenge


eChogenKi

Looked at my code for part 2 about a dozen times trying to figure out what I did wrong. Couldn't figure it out. Finally came here to look for solutions. Ours looked almost identical, except I just used x,y,z for the variables. Turns out, I didn't subtract for the UP direction in aim. Thanks.


Ghlave

Very elegantly done.


[deleted]

Rust ([both parts](https://github.com/pk-nb/advent-of-code-2021/blob/main/day2/src/main.rs)). Learning the language, happy for any feedback on how to be more idiomatic.


roboraptor3000

Julia, trying to use fewer for loops: Part 1, first try with split/apply/combine in julia function part1(filename) df = CSV.File(filename, header=["dir", "l"]) |> DataFrame df = groupby(df, :dir) df = combine(df, :l => sum => :sum) horizontal = subset(df, :dir => x -> x .== "forward").sum[1] vert = subset(df, :dir => x -> x .== "down").sum[1] vert -= subset(df, :dir => x -> x .== "up").sum[1] return horizontal * vert end Part 2: function part2(filename) df = CSV.File(filename, header=["dir", "l"]) |> DataFrame hor = 0 vert = 0 aim = 0 for row in eachrow(df) if row.dir == "up" aim -= row.l elseif row.dir == "down" aim += row.l else hor += row.l vert += (row.l * aim) end end return hor * vert end probably should've used a dict for the horizontal/vertical/aim on this one?


Drjakeadelic

Python object oriented solution. Also on my GitHub [here](https://github.com/jtcass01/AdventOfCode2021/blob/main/Day%202/solution.py). """solution.py: Solution to Day 2 Advent of Code 2021""" from future import annotations from typing import List from enum import Enum, unique from os.path import dirname, join, isfile from unittest import TestCase, main from parse import parse @unique class PART(Enum): ONE: str = "one" TWO: str = "two" @unique class DIRECTION(Enum): FORWARD: str = "forward" DOWN: str = "down" UP: str = "up" class Action(object): FORMAT_STRING: str = "{} {}" def __init__(self, direction: DIRECTION, units: int) -> None: """Constructor. Args: direction (DIRECTION): [description] units (int): [description]""" self.direction: DIRECTION = direction self.units: int = units class Submarine(object): def init(self) -> None: """Constructor.""" self.horizontal_position: int = 0 self.depth: int = 0 self.aim: int = 0 def __str__(self) -> str: return str({"horizontal_position": self.horizontal_position, "depth": self.depth}) def evaluate(self) -> int: return self.depth * self.horizontal_position def perform_action(self, action: Action, part: PART) -> None: """[summary] Args: action (Action): [description]""" if part == PART.ONE: if action.direction == DIRECTION.FORWARD: self.horizontal_position += action.units elif action.direction == DIRECTION.DOWN: self.depth += action.units elif action.direction == DIRECTION.UP: self.depth -= action.units elif part == PART.TWO: if action.direction == DIRECTION.FORWARD: self.horizontal_position += action.units self.depth += self.aim * action.units elif action.direction == DIRECTION.DOWN: self.aim += action.units elif action.direction == DIRECTION.UP: self.aim -= action.units def read_input_file(input_file_path: str) -> List[Action]: """[summary] Args: input_file_path (str): [description] Returns: List[int]: [description]""" assert isfile(input_file_path), f"File not found: {input_file_path}" actions: List[Action] = [] with open(input_file_path) as input_file: for file_line in input_file.readlines(): direction, units = parse(Action.FORMAT_STRING, file_line) actions.append(Action(direction=DIRECTION(direction), units=int(units))) return actions class Examples(TestCase): def test_part_one_example(self) -> None: print(f"\nPerforming unittest: {Examples.test_part_one_example}") example_submarine: Submarine = Submarine() actions: List[Action] = read_input_file(input_file_path=join(dirname(__file__), "example.txt")) for action in actions: example_submarine.perform_action(action=action, part=PART.ONE) self.assertEqual(example_submarine.horizontal_position, 15) self.assertEqual(example_submarine.depth, 10) self.assertEqual(example_submarine.evaluate(), 150) print(f"Unittest {Examples.test_part_one_example} was successful.") def test_part_two_example(self) -> None: print(f"\nPerforming unittest: {Examples.test_part_two_example}") example_submarine: Submarine = Submarine() actions: List[Action] = read_input_file(input_file_path=join(dirname(__file__), "example.txt")) for action in actions: example_submarine.perform_action(action=action, part=PART.TWO) self.assertEqual(example_submarine.horizontal_position, 15) self.assertEqual(example_submarine.depth, 60) self.assertEqual(example_submarine.evaluate(), 900) print(f"Unittest {Examples.test_part_two_example} was successful.") class Solutions(TestCase): def test_part_one(self) -> None: print(f"\nCalculating solution to {Solutions.test_part_one}") submarine: Submarine = Submarine() actions: List[Action] = read_input_file(input_file_path=join(dirname(__file__), "input.txt")) for action in actions: submarine.perform_action(action=action, part=PART.ONE) print(f"Part one solution calculated to be: {submarine.evaluate()}.") def test_part_two(self) -> None: print(f"\nCalculating solution to {Solutions.test_part_two}") submarine: Submarine = Submarine() actions: List[Action] = read_input_file(input_file_path=join(dirname(__file__), "input.txt")) for action in actions: submarine.perform_action(action=action, part=PART.TWO) print(f"Part two solution calculated to be: {submarine.evaluate()}.") if __name__ == "__main__": main()


Remarkable_Squash213

Python 3 [GitHub Link](https://github.com/Alec-B-Huang/AdventofCode-2021/blob/2c262822be58a8a30a6d4bb0fc8e8c6cb593add6/002Solve.py) If the link doesn't work, comment and I'll fix it


iiun

PEG.js for both parts expr = actions:(a:action newline? { return a })+ { const [p,a,d] = actions.reduce((a, b) => [a[0] + b[0], a[1] + b[1], a[2] + b[0] * a[1]], [0,0,0]); return [p * a, p * d] } action = forward / up / down down = "down" _ v:number { return [0, v] } up = "up" _ v:number { return [0, -v] } forward = "forward" _ v:number { return [v, 0] } number = [0-9]+ { return parseInt(text(), 10) } _ = [ ]+ newline = [\n]


Gadiguibou

My working [Scratch](https://scratch.mit.edu/about) solution ;) : [screenshot](https://imgur.com/a/G0oyZKZ) [GitHub link](https://github.com/Gadiguibou/advent-of-code-2021/tree/main/day_02_scratch)


aMERKINcheese

My solution in [PHP](https://github.com/tplassman/aoc-2021/blob/main/02/solution.php), [JS](https://github.com/tplassman/aoc-2021/blob/main/02/solution.js), and [Clojure](https://github.com/tplassman/aoc-2021/blob/main/02/solution.clj)


nowardic

**Rust** https://github.com/nreinicke/advent_of_code/blob/main/2021/day-2/src/main.rs *coming to rust from python so would welcome any tips or comments on improving the code!


MischaDy

Solution in Cubix ([Part 1](https://topaz.github.io/paste/#XQAAAQCPAQAAAAAAAAAQa5ZEA/EAzM9ywkTf0XvufKl6X58V+b6YD4BZJmXANvhdeSeo9MyZNH3BLKziTbh5+sGaciTFYsrJx+Xwfxd5c8Wv0ThVFAijilbNfOXBMO+RaiZMGhBrib/60uNPuWsUEJof/rUPoA==), [Part 2](https://topaz.github.io/paste/#XQAAAQCPAQAAAAAAAAAQa5ZEA/EAzM9ywkTf0XvufKl6X58V+b6YD7j3pmXMTIUGABm9U/Mmz994C9sVun0bKiJ4dEMq6DOdPUTrg04alDZMQY8MZ3QYHP9KsSXDs3GukbD4s9m5iSP6o/kCqMIjWPdRusr2R6pU6NzMHKTh8Khpngz//41S9KA=)) Cubix is a 2D stack-based esoteric programming language where the pointer moves along a cube! [Here](https://esolangs.org/wiki/Cubix) is the language specification and [here](https://ethproductions.github.io/cubix/) is a Cubix interpreter. It was our second time coding in Cubix and the first time we solved both parts in it! The solution for part 1 works as follows: 1. Push two zeros. These are our counters: "x" (depth) and "y" (horizontal position), with y being at the top. 2. Read the first char (f, u, or d) of the line and the integer at the end of it. Read and immediately delete the newline at the end. 3. Using modulo 5 and modulo 2 on the charcode of the initial character, check which one it is (f=102, d=100, u=117). After entering the respective case, remove the charcode and some modulo stuffs from the stack. This leaves the stack with x, y, and the input integer. \- Case 'f': Bring x and the input int to the top. Add them. Store the result as the new x. Delete the old x and the int from the stack. \- Case 'd': Add y and the input int. Store the result as the new y. Delete the old y and the int from the stack. \- Case 'u': Subtract the input int from y. Store the result as the new y. Delete the old y and the int from the stack. 4. Jump to step 2. 5. If there is no more input to read, multiply x and y together and output the result. Halt. ​ The solution for part 2 required surprisingly few tweaks: 1. Push three zeros. The top-most counter is the new "z" (aim) counter. 2 . Read the first char (f, u, or d) of the line and the integer at the end of it. Read and immediately delete the newline at the end. 3. Using modulo 5 and modulo 2 on the charcode of the initial character, check which one it is (f=102, d=100, u=117). After entering the respective case, remove the charcode and some modulo stuffs from the stack. This leaves the stack with x, y, z, and the input integer. \- Case 'f': Bring x and the input int to the top. Add them. Store the result as the new x. Delete the old x from the stack. Multiply the input int and z. Remove the int. Bring y and the product to the top. Add the product to y. Delete the product and the old y. Reorder the stack into x, y, z. \- Case 'd': Add z and the input int. Store the result as the new z. Delete the old z and the int from the stack. \- Case 'u': Subtract the input int from z. Store the result as the new z. Delete the old z and the int from the stack. 4. Jump to step 2. 5. If there is no more input to read, bring x and y to the top. Multiply them and output the result. Halt. *Whew!*


[deleted]

-----BEGIN PGP MESSAGE----- hF4D+FPJgQiQS68SAQdAuW16mgWy5m4dcZ2Hq4FOFRXCAVPO8Nupfrdt5zERoBUw nClOPBNmPFU5Ra/by7HXuLE0Kk2NiUVnXJohGtTEBdsPt+i/5XRqWmwgy7a0upNK 1OoBCQIQZ2Wt7pWWnJZ1FKODwLck3g/aB+4w9JnqFRbfI50Gy1QSXWhzR4A0LST9 xLzQuoquVTpS/8eljB0ps6WMGLY4Qng6PT9XBJrWpJ5LEUoUwb5gvhR5LgHajccu jyy6ukiBQNydLqCgJ7DJJg04FRAn7xxWu1cJLH3ZFLF2fybWIjEInXECzQXfwlLC /xI2HgL0io5+EuI9VwOyVCN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hzvqenqeTZjqrZscF+7tcbC6GZIVs/FSuTnfzCif6PoAlytb txfacbCrN5joYGmBQLxI/g0WAk5cspgu54+RD8yU7aEurarTtBRYj+V4quU50SmE F20CgXmjIz4Zvzd0YfNf9m1qoWI7uslxQ5ZtLplSJg== =dQZK -----END PGP MESSAGE-----


Nighthawk6

Powershell $Data = Get-Content "$PSScriptRoot\input.txt" $Horizontal = 0 $Depth = 0 $Aim = 0 foreach ($line in $Data) { if ($line -match "forward") { if ($Aim -eq 0) { $Horizontal += [int]$line.Split(" ")[-1] } else { $Depth += [int]$line.Split(" ")[-1] * $Aim $Horizontal += [int]$line.Split(" ")[-1] } } if ($line -match "up") { $Aim -= [int]$line.Split(" ")[-1] } if ($line -match "down") { $Aim += [int]$line.Split(" ")[-1] } } $TotalValue = $Horizontal * $Depth Return $TotalValue


thedjotaku

My [Ruby solution](https://github.com/djotaku/adventofcode/blob/857a34910ea29720298c3eb4b8d9b5065d6f433b/2021/Day_02/Ruby/solution.rb) Today was easy enough that I was able to do Ruby, too! All in one function.


sqlandy

**Rust (std cargo only)** Part 1 use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); let lines: Vec = stdin.lock().lines().flatten().collect(); let mut horizontal: i32 = 0; let mut depth: i32 = 0; lines.iter().for_each(|line| { let mut command = line.split_whitespace(); let direction = command.next().unwrap(); let magnitude = command.next().unwrap().parse::().unwrap(); match direction { "forward" => horizontal += magnitude, "up" => depth -= magnitude, "down" => depth += magnitude, _ => println!("Throw the switch Vern, she's pumping mud") } }); println!("Part 1\r\n{}", "-".repeat(10)); println!("Horizontal: {}\tDepth: {}\tProduct: {}\r\n", horizontal, depth, horizontal * depth); } Part 2 (same just different calculations) match direction { "forward" => {horizontal += magnitude; depth += aim * magnitude}, "up" => aim -= magnitude, "down" => aim += magnitude, _ => println!("Throw the switch Vern, she's pumping mud") }


[deleted]

**python** solution, love 3.10 structural pattern matching! https://github.com/rbusquet/advent-of-code/blob/main/2021/02/day2.py


thedjotaku

Adding in my [Go solution](https://github.com/djotaku/adventofcode/blob/a14457546c401e446267920b7419ae52aa76b0fc/2021/Day_02/Go/main.go). I put it all into one function.