惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
I
InfoQ
The Register - Security
The Register - Security
L
LangChain Blog
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
Schneier on Security
博客园 - 【当耐特】
W
WeLiveSecurity
Attack and Defense Labs
Attack and Defense Labs
IT之家
IT之家
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
H
Heimdal Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Y
Y Combinator Blog
雷峰网
雷峰网
N
Netflix TechBlog - Medium
Security Archives - TechRepublic
Security Archives - TechRepublic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
Lohrmann on Cybersecurity
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
The Exploit Database - CXSecurity.com
P
Privacy & Cybersecurity Law Blog
G
GRAHAM CLULEY
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
V
Visual Studio Blog
博客园 - 聂微东
PCI Perspectives
PCI Perspectives
Last Week in AI
Last Week in AI
A
Arctic Wolf
宝玉的分享
宝玉的分享
T
The Blog of Author Tim Ferriss
S
Secure Thoughts
T
Threat Research - Cisco Blogs
GbyAI
GbyAI
云风的 BLOG
云风的 BLOG
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
SecWiki News
SecWiki News
月光博客
月光博客
大猫的无限游戏
大猫的无限游戏
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AI
AI
Engineering at Meta
Engineering at Meta

ABB00717

medusa 找不到 ssh module 中文文案排版指北 BugBounty Playbook 小知識 透過 Ubuntu 26 設定 Windows 11 雙系統並使用 Image Recovery 之踩坑全紀錄 清除 git history 中的機敏資料 編譯器筆記 部落格聚集地 HTB - Busqueda 788. Rotated Digits 396. Rotate Function 幫耳機補血! 紀錄/週刊/ 做過的夢(旅行) 做過的夢 週刊 Vol.18 週刊 Vol.17 週刊 Vol.16 做過的夢(火箭推進器和追蹤導彈) 在 Ubuntu 24.04 中安裝 python2 和 pip2 動態牆 紀錄/音樂/ 用 miniflux 和 Cloudflare Tunnel 自架 RSS Reader 週記 Vol.15 關於用了 GCC 擴充功能,而被批評不夠 Clean Code 這檔事 GDB Makefile HTB - TwoMillion 週記 Vol.14 紀錄/Hack-The-Box/ 週記 Vol.13 ABB00717's Blog 1980. Find Unique Binary String 週記 Vol.12 紀錄/Leetcode/ 在互聯網上,什麼該說,什麼又不該說? 週記 Vol.8 週記 Vol.7 週記 Vol.6 天才之於一種義務 就算 LLM 能解答所有問題,你也不該放棄學習 Stack-Based Buffer Overflow 筆記/書籍/ 《絕佳時間》 週記 Vol.5 偽深刻的自我解構 筆記/Linux-雜項筆記/ 解決 Ubuntu 待機後喚醒異常的問題 將應用程式新增到 GNOME 的 Activities Overview 週記 Vol.4 Assembly Language 週記 Vol.3 筆記/ 文章/ 紀錄/ 資源/ 挑戰 週記 Vol.2 部落格該有的東西 週記 Vol.1 數學符號表 Advent of Code Day 8 Advent of Code Day 7 Obsidian 無痛轉成 Blog
Advent of Code Day 6
2025-12-06 · via ABB00717

Part 1

挺單純的,大致流程如下:

填滿 `numbers` 的陣列
並且紀錄每個 column 對應的運算子
遍歷每個 column 針對不同運算子作不同操作並求最終值
把每個 column 的最終值加起來

寫成程式碼就是

#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main() {
    std::ifstream inputFile("input-day6");
 
    std::string line;
    std::vector<std::vector<int>> numbers;
    std::vector<char> operators;
    while (std::getline(inputFile, line)) {
        int number;
        char oper;
        std::vector<int> line_numbers;
 
        // Read numbers in line
        if (std::isdigit(line[0])) {
            std::stringstream ss(line);
            while (ss >> number) {
                line_numbers.push_back(number);
            }
 
            numbers.push_back(line_numbers);
        } else {
            // Read operators in line
            std::stringstream ss(line);
            while (ss >> oper) {
                operators.push_back(oper);
            }
        }
    }
 
    int n = operators.size();
    long long result = 0;
    // Performing operations;
    for (int i = 0; i < n; i++) {
        long long temp = numbers[0][i];
        for (int row = 1; row < numbers.size(); row++) {
            switch (operators[i]) {
                case '+':
                    temp += numbers[row][i];
                    break;
                case '*':
                    temp *= numbers[row][i];
                    break;
            }
        }
 
        result += temp;
    }
 
    std::cout << result << std::endl;
}

Part 2

幾乎要把上面的程式碼重寫。

result = 0
for ch : 從右到左一個一個字元讀取
	cur_number : 由上到下組合起來
	if 最後一個是運算子
		result = 針對不同運算子操作,算出最終值

寫成程式碼就是

#include <cctype>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
int main() {
    std::ifstream inputFile("input-day6");
 
    std::string line;
    std::vector<std::string> inputLines;
    while (std::getline(inputFile, line)) {
        inputLines.push_back(line);
    }
 
    int lineSize = inputLines[0].size();
    int inputSize = inputLines.size();
    std::vector<long long> cur_line_numbers;
    long long result = 0;
    for (int col = lineSize-1; col >= 0; col--) {
        char dig = ' ';
 
        long long cur_number = 0;
        for (int row = 0; row < inputLines.size(); row++) {
            if (isdigit(inputLines[row][col])) {
                cur_number *= 10;
                cur_number += inputLines[row][col] - '0';
            }
        }
 
        if (cur_number != 0)
            cur_line_numbers.push_back(cur_number);
 
        if (inputLines[inputSize-1][col] != ' ') {
            char oper = inputLines[inputSize-1][col];
            long long temp = cur_line_numbers[0];
            switch (oper) {
                case '+':
                    for (int i = 1; i < cur_line_numbers.size(); i++) {
                        temp += cur_line_numbers[i];
                    }
                    break;
                case '*':
                    for (int i = 1; i < cur_line_numbers.size(); i++) {
                        temp *= cur_line_numbers[i];
                    }
                    break;
                default:
                    std::cout << "Unknown Operations!\n";
                    exit(-1);
            }
 
            result += temp;
            cur_line_numbers.clear();
            std::cout << result << std::endl;
        }
    }
 
    std::cout << result << std::endl;
}